ronaldkwandy

Untitled

Jun 22nd, 2020
1,254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { OnInit, OnDestroy, Component, Output, EventEmitter } from '@angular/core';
  2. import { FormGroup, FormBuilder, Validators, FormArray } from '@angular/forms';
  3.  
  4. import { Subject, of } from 'rxjs';
  5. import { takeUntil, switchMap, map } from 'rxjs/operators';
  6.  
  7. import { CompleteReminder } from 'src/app/gaji-id-resources/resources/master/reminder/complete-reminder.model';
  8. import { Reminder } from '../../../../../../gaji-id-resources/resources/master/reminder/reminder.model';
  9. import { ReminderDetailUser } from 'src/app/gaji-id-resources/resources/master/reminder-detail-user/reminder-detail-user.model';
  10. import { ReminderDetailEmail } from 'src/app/gaji-id-resources/resources/master/reminder-detail-email/reminder-detail-email.model';
  11. import { ReminderType } from 'src/app/gaji-id-resources/combo-constants/reminder-type.constant';
  12.  
  13. import { ContentAlertService } from '../../../../../../components/messages/alerts/content-alert/content-alert.service';
  14. import { ReminderService } from '../../../../../../gaji-id-resources/resources/master/reminder/reminder.service';
  15. import { ReminderDetailEmailService } from 'src/app/gaji-id-resources/resources/master/reminder-detail-email/reminder-detail-email.service';
  16. import { ReminderDetailUserService } from 'src/app/gaji-id-resources/resources/master/reminder-detail-user/reminder-detail-user.service';
  17. import { SnackBarService } from '../../../../../../components/messages/snackbar/snackbar.service';
  18. import { UiBlockService } from 'src/app/components/ui-block/ui-block.service';
  19.  
  20. import * as _ from 'lodash';
  21.  
  22. @Component({
  23.   selector: 'app-setting-reminder-browse',
  24.   templateUrl: './reminder-browse.component.html'
  25. })
  26. export class ReminderBrowseComponent implements OnInit, OnDestroy {
  27.  
  28.   @Output() public onAdd: EventEmitter<CompleteReminder> = new EventEmitter();
  29.   @Output() public onEdit: EventEmitter<CompleteReminder> = new EventEmitter();
  30.  
  31.   public contractReminder: Reminder = null;
  32.   public contractReminderDetails: (ReminderDetailEmail | ReminderDetailUser)[] = [];
  33.   public contractLoadingResult: boolean = false;
  34.   public probationReminder: Reminder = null;
  35.   public probationReminderDetails: (ReminderDetailEmail | ReminderDetailUser)[] = [];
  36.   public probationLoadingResult: boolean = false;
  37.   public selectedContractReminderDetail: ReminderDetailUser | ReminderDetailEmail = null;
  38.   public selectedProbationReminderDetail: ReminderDetailUser | ReminderDetailEmail = null;
  39.  
  40.   public inputForm: FormGroup;
  41.   public reminderFormArray: FormArray;
  42.  
  43.   private ngUnsubscribe: Subject<boolean> = new Subject();
  44.  
  45.   constructor(
  46.     private fb: FormBuilder,
  47.     private contentAlertService: ContentAlertService,
  48.     private reminderService: ReminderService,
  49.     private reminderDetailUserService: ReminderDetailUserService,
  50.     private reminderDetailEmailService: ReminderDetailEmailService,
  51.     private snackbarService: SnackBarService,
  52.     private uiBlockService: UiBlockService
  53.   ) {
  54.     this.initForm();
  55.   }
  56.  
  57.   ngOnInit() {
  58.     this.doInstantFilter();
  59.   }
  60.  
  61.   ngOnDestroy() {
  62.     this.ngUnsubscribe.next(true);
  63.     this.ngUnsubscribe.complete();
  64.   }
  65.  
  66.   public initForm() {
  67.     this.inputForm = this.fb.group({
  68.       contractDashboardReminder: [null, Validators.required],
  69.       contractEmailReminder: [null, Validators.required],
  70.       probationDashboardReminder: [null, Validators.required],
  71.       probationEmailReminder: [null, Validators.required],
  72.     });
  73.  
  74.     this.reminderFormArray = this.inputForm.get('reminder') as FormArray;
  75.   }
  76.  
  77.   public doInstantFilter() {
  78.     this.selectedContractReminderDetail = null;
  79.     this.selectedProbationReminderDetail = null;
  80.  
  81.     this.uiBlockService.showUiBlock();
  82.     this.reminderService.search({}, { type: 'asc' })
  83.       .pipe(takeUntil(this.ngUnsubscribe))
  84.       .subscribe(
  85.         (result) => {
  86.           this.uiBlockService.hideUiBlock();
  87.           result.data.forEach(reminder => {
  88.             if (reminder.type === ReminderType.Contract.value) {
  89.               this.contractReminder = _.cloneDeep(reminder);
  90.  
  91.               this.inputForm.patchValue({
  92.                 contractDashboardReminder: this.contractReminder.dashboardReminder,
  93.                 contractEmailReminder: this.contractReminder.emailReminder
  94.               });
  95.  
  96.               this.searchContractReminderDetail();
  97.             } else if (reminder.type === ReminderType.Probation.value) {
  98.               this.probationReminder = _.cloneDeep(reminder);
  99.  
  100.               this.inputForm.patchValue({
  101.                 probationDashboardReminder: this.probationReminder.dashboardReminder,
  102.                 probationEmailReminder: this.probationReminder.emailReminder
  103.               });
  104.  
  105.               this.searchProbationReminderDetail();
  106.             }
  107.           });
  108.         },
  109.         (error) => {
  110.           this.uiBlockService.hideUiBlock();
  111.           this.contentAlertService.error(error.errors);
  112.         },
  113.         () => { this.uiBlockService.hideUiBlock(); }
  114.       );
  115.   }
  116.  
  117.   private searchContractReminderDetail() {
  118.     let detailUsers = [];
  119.     this.contractReminderDetails = [];
  120.  
  121.     this.contractLoadingResult = true;
  122.     this.reminderDetailUserService.search({ reminderId: this.contractReminder.id })
  123.       .pipe(
  124.         switchMap((result) => {
  125.           detailUsers = result.data;
  126.           return this.reminderDetailEmailService.search({ reminderId: this.contractReminder.id });
  127.         }),
  128.         switchMap((result) => {
  129.           const data: (ReminderDetailEmail | ReminderDetailUser)[] = [];
  130.           data.push(...detailUsers);
  131.           data.push(...result.data);
  132.           data.sort((a, b) => {
  133.             if (a.reminderDetail.workplace === null || b.reminderDetail.workplace === null) {
  134.               return -1;
  135.             } else {
  136.               return a.reminderDetail.workplace.name.localeCompare(b.reminderDetail.workplace.name);
  137.             }
  138.           });
  139.           return of(data);
  140.         }),
  141.         takeUntil(this.ngUnsubscribe)
  142.       )
  143.       .subscribe(
  144.         (result) => {
  145.           this.contractLoadingResult = false;
  146.           this.contractReminderDetails = result;
  147.         },
  148.         (error) => {
  149.           this.contractLoadingResult = false;
  150.           this.contentAlertService.error(error.errors);
  151.         },
  152.         () => { this.contractLoadingResult = false; }
  153.       );
  154.   }
  155.  
  156.   private searchProbationReminderDetail() {
  157.     this.probationReminderDetails = [];
  158.  
  159.     // this.probationLoadingResult = true;
  160.     // this.reminderDetailUserService.search({ reminderId: this.probationReminder.id })
  161.     //   .pipe(
  162.     //     switchMap((result) => {
  163.     //       this.probationReminderDetails.push(...result.data);
  164.     //       return this.reminderDetailEmailService.search({ reminderId: this.probationReminder.id });
  165.     //     }),
  166.     //     takeUntil(this.ngUnsubscribe)
  167.     //   )
  168.     //   .subscribe(
  169.     //     (result) => {
  170.     //       this.probationLoadingResult = false;
  171.     //       this.probationReminderDetails.push(...result.data);
  172.     //       this.probationReminderDetails.sort((a, b) => {
  173.     //         return a.reminderDetail.workplace.name.localeCompare(b.reminderDetail.workplace.name);
  174.     //       });
  175.     //     },
  176.     //     (error) => {
  177.     //       this.probationLoadingResult = false;
  178.     //       this.contentAlertService.error(error.errors);
  179.     //     },
  180.     //     () => { this.probationLoadingResult = false; }
  181.     //   );
  182.   }
  183.  
  184.   public addContractReminder() {
  185.     this.onAdd.emit(new CompleteReminder({
  186.       reminder: this.contractReminder,
  187.       reminderDetail: null,
  188.       detailUsers: [],
  189.       detailEmails: []
  190.     }));
  191.   }
  192.  
  193.   public addProbationReminder() {
  194.     this.onAdd.emit(new CompleteReminder({
  195.       reminder: this.probationReminder,
  196.       reminderDetail: null,
  197.       detailUsers: [],
  198.       detailEmails: []
  199.     }));
  200.   }
  201.  
  202.   public saveReminder() {
  203.     const contractReminder = _.cloneDeep(this.contractReminder);
  204.     contractReminder.dashboardReminder = this.inputForm.value.contractDashboardReminder;
  205.     contractReminder.emailReminder = this.inputForm.value.contractEmailReminder;
  206.  
  207.     const probationReminder = _.cloneDeep(this.probationReminder);
  208.     probationReminder.dashboardReminder = this.inputForm.value.probationDashboardReminder;
  209.     probationReminder.emailReminder = this.inputForm.value.probationEmailReminder;
  210.  
  211.     const reminders: Reminder[] = [];
  212.     reminders.push(contractReminder);
  213.     reminders.push(probationReminder);
  214.  
  215.     this.uiBlockService.showUiBlock();
  216.     this.reminderService.editMulti(reminders)
  217.       .pipe(takeUntil(this.ngUnsubscribe))
  218.       .subscribe(
  219.         (result) => {
  220.           this.uiBlockService.hideUiBlock();
  221.           this.snackbarService.show('reminderSetting.updated');
  222.         },
  223.         (error) => {
  224.           this.uiBlockService.hideUiBlock();
  225.           this.contentAlertService.error(error.errors);
  226.         },
  227.         () => { this.uiBlockService.hideUiBlock(); }
  228.       );
  229.   }
  230.  
  231.   public onSelectContractReminderDetail(selectedData: ReminderDetailUser | ReminderDetailEmail) {
  232.     this.onEdit.emit(new CompleteReminder({
  233.       reminder: selectedData.reminderDetail.reminder,
  234.       reminderDetail: selectedData.reminderDetail,
  235.       detailUsers: this.contractReminderDetails.filter(item => item.reminderDetail.id === selectedData.reminderDetail.id && item instanceof ReminderDetailUser) as ReminderDetailUser[],
  236.       detailEmails: this.contractReminderDetails.filter(item => item.reminderDetail.id === selectedData.reminderDetail.id && item instanceof ReminderDetailEmail) as ReminderDetailEmail[]
  237.     }));
  238.   }
  239.  
  240.   public onSelectProbationReminderDetail(selectedData: ReminderDetailUser | ReminderDetailEmail) {
  241.     this.onEdit.emit(new CompleteReminder({
  242.       reminder: selectedData.reminderDetail.reminder,
  243.       reminderDetail: selectedData.reminderDetail,
  244.       detailUsers: this.probationReminderDetails.filter(item => item.reminderDetail.id === selectedData.reminderDetail.id && item instanceof ReminderDetailUser) as ReminderDetailUser[],
  245.       detailEmails: this.probationReminderDetails.filter(item => item.reminderDetail.id === selectedData.reminderDetail.id && item instanceof ReminderDetailEmail) as ReminderDetailEmail[]
  246.     }));
  247.   }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment