Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 30989178

Přidáno uživatelem Hung Hoang před téměř 6 roky(ů)

Re#7527 Implemented import and export files on front end

Zobrazit rozdíly:

webapp/src/app/employees/employees-list.component.html
1 1
<div class="employee-component">
2
  <h3>Zaměstnanci
2
  <h3> <span class="align-baseline">Zaměstnanci</span>
3

  
3 4
    <button
4 5
      id="settings-button"
5 6
      mat-icon-button
6 7
      (click)="openDefaultSettingsDialog()"
8
      class="align-baseline"
7 9
    >
8 10
      <i class="material-icons">settings</i>
9 11
    </button>
12

  
13
    <input
14
      type="file"
15
      [multiple]="false"
16
      #fileInput
17
      class="input-file"
18
      id="input-file"
19
      (change)="uploadXlsxFile(fileInput.files); fileInput.value = null;"
20
    />
21

  
22
    <label for="input-file" class="btn btn-primary align-baseline">Import</label>
23
    <button class="btn btn-danger align-baseline" (click)="downloadPdf()">
24
      Export
25
    </button>
26

  
10 27
  </h3>
11 28

  
12 29
  <table class="table text-center table-hover">
webapp/src/app/employees/employees-list.component.sass
25 25
  background-color: #68b040
26 26
  border-radius: 40%
27 27
  justify-content: center
28

  
29

  
30
.input-file
31
  width: 0.1px
32
  height: 0.1px
33
  opacity: 0
34
  overflow: hidden
35
  position: absolute
36
  z-index: -1
37

  
webapp/src/app/employees/employees-list.component.ts
11 11
import {UserService} from '../services/api/user.service';
12 12
import {LocalizationService} from '../localization/localization.service';
13 13
import {DateFormatterService} from '../services/util/date-formatter.service';
14
import {FileService} from '../services/api/file.service';
14 15

  
15 16
const daysOfWeek: string[] = [
16 17
  'po',
......
40 41
    private settingsService: SettingsService,
41 42
    private localizationService: LocalizationService,
42 43
    private dateFormatterService: DateFormatterService,
44
    private fileService: FileService,
43 45
    public dialog: MatDialog,
44 46
    private snackBar: MatSnackBar) {
45 47
    this.generateDays();
......
47 49
    this.editDates();
48 50
  }
49 51

  
52

  
53
  /**
54
   * Sends a request to the GET /api/export/pdf end point
55
   * After receiving the answer downloads the pdf file to user's download folder
56
   */
57
  downloadPdf(): void {
58
    // https://stackoverflow.com/a/52687792/6204336
59
    this.fileService.getExportedPdf()
60
      .subscribe((data: any) => {
61
        console.log(data);
62
        const blob = new Blob([data], {type: 'application/pdf'});
63
        const link = window.URL.createObjectURL(blob);
64
        const linkElement = document.createElement('a');
65
        linkElement.href = link;
66
        linkElement.download = 'super.pdf';
67
        linkElement.click();
68
      });
69
  }
70

  
71
  /**
72
   * Uploads first file from the file list to the
73
   * endpoint /api/import/xlsx
74
   * @param files file list, uses only files.item(0)
75
   */
76
  uploadXlsxFile(files: FileList): void {
77
    this.fileService.uploadXlsFile(files)
78
      .subscribe(() => this.snackBar.open('Import souboru se provedl', 'Zavřít', {duration: 5000}));
79
  }
80

  
81
  /**
82
   * Opens a dialog to edit users settings after closing
83
   * the dialog if user clicked to confirm new settings
84
   * new settings are sent to the PUT /api/user/settings end point
85
   * @param user user information
86
   */
50 87
  openEditUserDialog(user: User): void {
51 88
    this.userService.getUserProfile(user.id)
52 89
      .subscribe((userProfile: UserProfile) => {
......
56 93

  
57 94
        dialogRef.componentInstance.postUserSettings.subscribe((emittedData) => {
58 95
          this.userService.putUserSettings(emittedData)
59
            .subscribe(() => this.snackBar.open('Povedlo se', 'Zavrit'));
96
            .subscribe(() => this.snackBar.open('Povedlo se', 'Zavrit', {duration: 5000}));
60 97
        });
61 98
      });
62 99
  }
webapp/src/app/services/api/basic.service.ts
23 23
      'Something bad happened; please try again later.');
24 24
  }
25 25

  
26
  /**
27
   * Creates http parameters (query for request) for given
28
   * object (parameter - value), if the value is null
29
   * it's not added into the query
30
   * @param params object from which the query is created
31
   */
26 32
  protected createParams(params: any) {
27 33
    let httpParams = new HttpParams();
28 34
    for (const key in params) {
webapp/src/app/services/api/file.service.ts
1
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpParams} from '@angular/common/http';
3
import {BasicService} from './basic.service';
4
import {catchError} from 'rxjs/operators';
5
import {Languages} from '../../enums/common.enum';
6

  
7
@Injectable({
8
  providedIn: 'root'
9
})
10
export class FileService extends BasicService {
11

  
12
  constructor(protected http: HttpClient) {
13
    super(http);
14
  }
15

  
16
  /**
17
   * Uploads xlsx file which will be parsed on the server
18
   * @param file xlsx file to be parsed
19
   */
20
  uploadXlsFile(file: FileList) {
21
    return this.makeImportXlsxAPiCall(file, null);
22
  }
23

  
24
  /**
25
   * Uploads xlsx file which will be parsed on the server
26
   * @param language specify error message language
27
   * @param file xlsx file to be parsed
28
   */
29
  uploadXlsFileWithLanguage(file: FileList, language: Languages) {
30
    return this.makeImportXlsxAPiCall(file, language);
31
  }
32

  
33
  /**
34
   * Exports a pdf file from the imported xlsx file
35
   */
36
  getExportedPdf() {
37
    return this.makeExportPdfApiCall(null);
38
  }
39

  
40
  /**
41
   * Exports a pdf file from the imported xlsx file
42
   * @param language specify error message language
43
   */
44
  getExportedPdfWithLanguage(language: Languages) {
45
    return this.makeExportPdfApiCall(language);
46
  }
47

  
48
  /**
49
   * Testovaci endpoint pro export PDF souboru
50
   * GET /export/pdf? [lang=<CZ,EN>]
51
   * @param language specify error message language
52
   */
53
  private makeExportPdfApiCall(language: Languages) {
54
    const httpParams: HttpParams = this.createParams({lang: language});
55

  
56
    return this.http.get(this.baseUrl + '/api/export/pdf', {responseType: 'blob', params: httpParams})
57
      .pipe(
58
        catchError(err => this.handleError(err))
59
      );
60
  }
61

  
62
  /**
63
   * Testovaci endpoint pro import XLS souboru
64
   * POST /import/xls?[lang=<CZ,EN>]&file=<binarni_soubor>
65
   * @param file xlsx file to import and parse
66
   * @param language specify error message langauge
67
   */
68
  private makeImportXlsxAPiCall(file: FileList, language: Languages) {
69
    const fileCount: number = file.length;
70
    const formData = new FormData();
71

  
72
    const httpParams: HttpParams = this.createParams({lang: language});
73
    const options = {params: httpParams};
74

  
75
    if (fileCount > 0) {
76

  
77
      formData.append('file', file.item(0));
78
      console.log('posilam data');
79
      return this.http.post(this.baseUrl + '/api/import/xls', formData, options);
80
    }
81
  }
82
}

Také k dispozici: Unified diff