Skip to content

Commit

Permalink
Merge pull request #7 from moevm/change-csv-to-json
Browse files Browse the repository at this point in the history
Изменил экспорт/импорт csv на json на фронте
  • Loading branch information
go1vs1noob authored Dec 19, 2024
2 parents 8f1ac6c + 3702ff3 commit 15288c4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
Импорт
</div>
<input
accept=".csv"
tuiInputFiles
[formControl]="control"
accept=".json"
tuiInputFiles
[formControl]="control"
/>
</label>

<tui-files class="tui-space_top-1">
<tui-file
*ngIf="control.value | tuiFileRejected: {accept: '.csv'} | async as file"
state="error"
[file]="file"
(remove)="removeFile()"
*ngIf="control.value | tuiFileRejected: {accept: '.json'} | async as file"
state="error"
[file]="file"
(remove)="removeFile()"
></tui-file>

<tui-file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { catchError, finalize, map, Observable, of, Subject, switchMap } from "r
import { FormControl, ReactiveFormsModule, Validators } from "@angular/forms";
import { AsyncPipe, NgIf } from "@angular/common";
import { ToponymsService } from "../../../services/toponyms.service";
import {TuiAppearance, TuiButton} from "@taiga-ui/core";
import { TuiAppearance, TuiButton } from "@taiga-ui/core";

@Component({
selector: 'app-import-export',
Expand All @@ -15,8 +15,7 @@ import {TuiAppearance, TuiButton} from "@taiga-ui/core";
})
export class ImportExportComponent {

constructor(private readonly toponymsService: ToponymsService) {
}
constructor(private readonly toponymsService: ToponymsService) { }

protected readonly control = new FormControl<TuiFileLike | null>(
null,
Expand All @@ -41,7 +40,6 @@ export class ImportExportComponent {
}

this.loadingFiles$.next(file);

return this.toponymsService.import(file as File).pipe(
map(() => {
return file;
Expand All @@ -61,7 +59,7 @@ export class ImportExportComponent {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'exported_data.csv';
a.download = 'exported_data.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
Expand Down
14 changes: 7 additions & 7 deletions frontent/src/app/services/toponyms.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ export class ToponymsService {
const formData = new FormData();
formData.append('file', file);

const uploadUrl = 'http://localhost:5001/api/import';
const uploadUrl = `http://localhost:5001/api/import`;

return this.http.post(uploadUrl, formData)
.pipe(
catchError(error => {
console.error('Error fetching toponyms:', error);
return throwError(error);
console.error('Error importing JSON:', error);
return throwError(() => error);
})
);
}

export(): Observable<Blob> {
const downloadUrl = 'http://localhost:5001/api/export';
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
const downloadUrl = `http://localhost:5001/api/export`;
const headers = new HttpHeaders({ 'Accept': 'application/json' });

return this.http.get(downloadUrl, { headers, responseType: 'blob' }).pipe(
catchError(error => {
console.error('Error downloading CSV:', error);
return throwError(error);
console.error('Error exporting JSON:', error);
return throwError(() => error);
})
);
}
Expand Down

0 comments on commit 15288c4

Please sign in to comment.