pastis-popup-option.component.ts 8.66 KB
Newer Older
descamps's avatar
descamps committed
1
2
import {Component, Input, OnDestroy, OnInit} from '@angular/core';
import {MatDialog} from '@angular/material/dialog';
3
import {ActivatedRoute, Router} from '@angular/router';
descamps's avatar
descamps committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import {LangChangeEvent, TranslateService} from '@ngx-translate/core';
import {FileUploader} from 'ng2-file-upload';
import {NgxUiLoaderService} from 'ngx-ui-loader';
import {environment} from 'projects/pastis/src/environments/environment';
import {Subscription} from 'rxjs';
import {NoticeService} from '../../core/services/notice.service';
import {NotificationService} from '../../core/services/notification.service';
import {ProfileService} from '../../core/services/profile.service';
import {ArchivalProfileUnit} from '../../models/archival-profile-unit';
import {FileNode} from '../../models/file-node';
import {Profile} from '../../models/profile';
import {ProfileDescription} from '../../models/profile-description.model';
import {CreateNoticeComponent} from '../../user-actions/create-notice/create-notice.component';
import {PastisDialogDataCreate} from '../../user-actions/save-profile/save-profile.component';
import {PastisDialogData} from '../pastis-dialog/classes/pastis-dialog-data';
import {CreateNoticeChoiceComponent} from "../../profile/create-notice-choice/create-notice-choice.component";
naji's avatar
naji committed
20
21
22


function constantToTranslate(edit: boolean) {
naji's avatar
naji committed
23
  if (edit) {
naji's avatar
naji committed
24
25
    this.popupSaveCreateNoticeTitleDialog = this.translated('PROFILE.POP_UP_SAVE.SAVE_PROFILE.POPUP_CREATE_NOTICE_TITLE_DIALOG_EDIT');
    this.popupSaveCreateNoticeSubTitleDialog = this.translated('PROFILE.POP_UP_SAVE.SAVE_PROFILE.POPUP_CREATE_NOTICE_SUBTITLE_DIALOG_EDIT');
naji's avatar
naji committed
26
  } else {
naji's avatar
naji committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    this.popupSaveCreateNoticeTitleDialog = this.translated('PROFILE.POP_UP_SAVE.SAVE_PROFILE.POPUP_CREATE_NOTICE_TITLE_DIALOG');
    this.popupSaveCreateNoticeSubTitleDialog = this.translated('PROFILE.POP_UP_SAVE.SAVE_PROFILE.POPUP_CREATE_NOTICE_SUBTITLE_DIALOG');
  }
  this.popupSaveCancelLabel = this.translated('PROFILE.POP_UP_SAVE.SAVE_PROFILE.POPUP_SAVE_CANCEL_LABEL');
  this.popupSaveTitleDialog = this.translated('PROFILE.POP_UP_SAVE.SAVE_PROFILE.POPUP_SAVE_TITLE_DIALOG');
  this.popupSaveSubTitleDialog = this.translated('PROFILE.POP_UP_SAVE.SAVE_PROFILE.POPUP_SAVE_SUBTITLE_DIALOG');
  this.popupSaveOkLabel = this.translated('PROFILE.POP_UP_SAVE.SAVE_PROFILE.POPUP_SAVE_OK_LABEL');
  this.popupSaveCreateNoticeCancelLabel = this.translated('PROFILE.POP_UP_SAVE.SAVE_PROFILE.POPUP_CREATE_NOTICE_CANCEL_LABEL');
  this.popupSaveCreateNoticeOkLabel = this.translated('PROFILE.POP_UP_SAVE.SAVE_PROFILE.POPUP_CREATE_NOTICE_OK_LABEL');
  this.popupCreationCancelLabel = this.translated('PROFILE.POP_UP_CREATION.POPUP_CREATION_CANCEL_LABEL');
  this.popupCreationTitleDialog = this.translated('PROFILE.POP_UP_CREATION.POPUP_CREATION_TITLE_DIALOG');
  this.popupCreationSubTitleDialog = this.translated('PROFILE.POP_UP_CREATION.POPUP_CREATION_SUBTITLE_DIALOG');
  this.popupCreationOkLabel = this.translated('PROFILE.POP_UP_CREATION.POPUP_CREATION_OK_LABEL');
}
@Component({
  selector: 'pastis-popup-option',
  templateUrl: './pastis-popup-option.component.html',
  styleUrls: ['./pastis-popup-option.component.scss']
})
export class PastisPopupOptionComponent implements OnInit, OnDestroy {

naji's avatar
naji committed
48
49
50
51
  popupSaveCancelLabel: string;
  popupSaveTitleDialog: string;
  popupSaveSubTitleDialog: string;
  popupSaveOkLabel: string;
naji's avatar
naji committed
52

naji's avatar
naji committed
53
54
55
56
  popupSaveCreateNoticeCancelLabel: string;
  popupSaveCreateNoticeTitleDialog: string;
  popupSaveCreateNoticeSubTitleDialog: string;
  popupSaveCreateNoticeOkLabel: string;
naji's avatar
naji committed
57
58
59
60
61
62
63
64
65
66
67
68
  editProfile: boolean;

  subscription1$: Subscription;
  subscription2$: Subscription;
  _uploadProfileSub: Subscription;
  subscriptions: Subscription[] = [];
  isStandalone: boolean = environment.standalone;
  popupCreationCancelLabel: string;
  popupCreationTitleDialog: string;
  popupCreationSubTitleDialog: string;
  popupCreationOkLabel: string;

naji's avatar
naji committed
69
  donnees: string[];
naji's avatar
naji committed
70
71
72
73
74
75
76
77
78
79
80
81
82

  data: FileNode[] = [];

  archivalProfileUnit: ArchivalProfileUnit;
  profile: Profile;

  profileDescription: ProfileDescription;

  @Input()
  sedaUrl: string;
  @Input()
  newProfileUrl: string;
  @Input()
naji's avatar
naji committed
83
  uploader: FileUploader = new FileUploader({url: ''});
naji's avatar
naji committed
84

naji's avatar
naji committed
85
  expanded = false;
naji's avatar
naji committed
86
87


naji's avatar
naji committed
88
  constructor(private router: Router, private profileService: ProfileService,
naji's avatar
naji committed
89
              public dialog: MatDialog, private noticeService: NoticeService,
naji's avatar
naji committed
90
              private translateService: TranslateService, private loaderService: NgxUiLoaderService,
91
              private notificationService: NotificationService, private route: ActivatedRoute) { }
naji's avatar
naji committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

  ngOnInit(): void {
    constantToTranslate.call(this, this.editProfile);
    this.translatedOnChange();
  }

  translatedOnChange(): void {
    this.translateService.onLangChange
      .subscribe((event: LangChangeEvent) => {
        constantToTranslate.call(this);
        console.log(event.lang);
      });
  }

  translated(nameOfFieldToTranslate: string): string {
    return this.translateService.instant(nameOfFieldToTranslate);
  }

naji's avatar
naji committed
110
  goToSedaView() {
111
112
    const url = this.router.serializeUrl(this.router.createUrlTree(['sedaview'], {relativeTo: this.route}));
    window.open(url, '_blank');
naji's avatar
naji committed
113
114
  }

naji's avatar
naji committed
115
116
  uploadProfile(files: File[]): void {
    const fileToUpload: File = files[0];
naji's avatar
naji committed
117
118
119
120
121
122
123
124

    if (fileToUpload) {
      const formData = new FormData();
      formData.append('file', fileToUpload, fileToUpload.name);
      this._uploadProfileSub = this.profileService.uploadProfile(formData).subscribe( (response: any) => {
        if (response) {
          console.log('File submited! Reponse is : ', response);

125
          this.router.navigate([this.newProfileUrl], { state: response, relativeTo: this.route});
naji's avatar
naji committed
126
127
128
129
130
        }
      });
    }
  }

naji's avatar
naji committed
131
  changeExpand() {
naji's avatar
naji committed
132
133
    this.expanded = !this.expanded;
  }
descamps's avatar
descamps committed
134
  async createNotice() {
naji's avatar
naji committed
135
    this.loaderService.start();
naji's avatar
naji committed
136
    const dataToSendToPopUp = {} as PastisDialogData;
naji's avatar
naji committed
137
138
139
140
141
142
    dataToSendToPopUp.titleDialog = this.popupCreationTitleDialog;
    dataToSendToPopUp.subTitleDialog = this.popupCreationSubTitleDialog;
    dataToSendToPopUp.width = '800px';
    dataToSendToPopUp.height = '800px';
    dataToSendToPopUp.okLabel = this.popupCreationOkLabel;
    dataToSendToPopUp.cancelLabel = this.popupCreationCancelLabel;
descamps's avatar
descamps committed
143
    const dialogRef = this.dialog.open(CreateNoticeChoiceComponent, {
naji's avatar
naji committed
144
145
146
147
148
        width: '800px',
        panelClass: 'pastis-popup-modal-box',
        data: dataToSendToPopUp
      }
    );
naji's avatar
naji committed
149
150
151
152
153
    this.subscription2$ = dialogRef.afterClosed().subscribe((result) => {
      if (result.success) {
        console.log(result.action + ' PA ou PUA ?');
        if (result.action === 'PA' || result.action === 'PUA') {
          const dataToSendToPopUp = {} as PastisDialogDataCreate;
naji's avatar
naji committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
          dataToSendToPopUp.titleDialog = this.popupSaveCreateNoticeTitleDialog;
          dataToSendToPopUp.subTitleDialog = this.popupSaveCreateNoticeSubTitleDialog;
          dataToSendToPopUp.okLabel = this.popupSaveCreateNoticeOkLabel;
          dataToSendToPopUp.cancelLabel = this.popupSaveCreateNoticeCancelLabel;
          dataToSendToPopUp.modeProfile = result.action;
          const dialogRef = this.dialog.open(CreateNoticeComponent, {
              width: '800px',
              panelClass: 'pastis-popup-modal-box',
              data: dataToSendToPopUp
            }
          );
          dialogRef.afterClosed().subscribe((result) => {
            let retour;
            if (result.success) {
naji's avatar
naji committed
168
169
170
              retour = result.data;
              if (result.mode === 'PUA') {
                console.log('je suis sur un enregistrement d\'un PUA');
naji's avatar
naji committed
171

naji's avatar
naji committed
172
                const profileDescription = this.noticeService.puaNotice(retour);
naji's avatar
naji committed
173
174
                this.profileService.createArchivalUnitProfile(profileDescription).subscribe(() => {
                  this.changeExpand();
naji's avatar
naji committed
175
                  this.notificationService.showSuccess('La création de notice a bien été effectué');
naji's avatar
naji committed
176
                  this.profileService.refreshListProfiles();
naji's avatar
naji committed
177
178
                  console.log('ok create');
              });
naji's avatar
naji committed
179

naji's avatar
naji committed
180
181
              } else if (result.mode === 'PA') {
                console.log(retour.identifier);
naji's avatar
naji committed
182

naji's avatar
naji committed
183
184
                const profile: Profile = this.noticeService.paNotice(retour, true);
                // STEP 1 : Create Notice
naji's avatar
naji committed
185
                this.profileService.createProfilePa(profile).subscribe(() => {
naji's avatar
naji committed
186
                  // console.log("ok create" + createdProfile)
naji's avatar
naji committed
187
                  this.changeExpand();
naji's avatar
naji committed
188
                  this.notificationService.showSuccess('La création de notice a bien été effectué');
naji's avatar
naji committed
189
                  this.profileService.refreshListProfiles();
naji's avatar
naji committed
190
                });
naji's avatar
naji committed
191
192
193
194
195
196
197

              }
            }
          });
        }
      }
      });
naji's avatar
naji committed
198
    this.loaderService.stop();
naji's avatar
naji committed
199
200
201
202
    this.subscriptions.push(this.subscription2$);
  }

  ngOnDestroy(): void {
naji's avatar
naji committed
203
    if (this._uploadProfileSub) {
naji's avatar
naji committed
204
205
206
207
208
      this._uploadProfileSub.unsubscribe();
    }
  }

}