Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { OnInit, OnDestroy, Component, Output, EventEmitter } from '@angular/core';
- import { FormGroup, FormBuilder, Validators, FormArray } from '@angular/forms';
- import { Subject, of } from 'rxjs';
- import { takeUntil, switchMap, map } from 'rxjs/operators';
- import { CompleteReminder } from 'src/app/gaji-id-resources/resources/master/reminder/complete-reminder.model';
- import { Reminder } from '../../../../../../gaji-id-resources/resources/master/reminder/reminder.model';
- import { ReminderDetailUser } from 'src/app/gaji-id-resources/resources/master/reminder-detail-user/reminder-detail-user.model';
- import { ReminderDetailEmail } from 'src/app/gaji-id-resources/resources/master/reminder-detail-email/reminder-detail-email.model';
- import { ReminderType } from 'src/app/gaji-id-resources/combo-constants/reminder-type.constant';
- import { ContentAlertService } from '../../../../../../components/messages/alerts/content-alert/content-alert.service';
- import { ReminderService } from '../../../../../../gaji-id-resources/resources/master/reminder/reminder.service';
- import { ReminderDetailEmailService } from 'src/app/gaji-id-resources/resources/master/reminder-detail-email/reminder-detail-email.service';
- import { ReminderDetailUserService } from 'src/app/gaji-id-resources/resources/master/reminder-detail-user/reminder-detail-user.service';
- import { SnackBarService } from '../../../../../../components/messages/snackbar/snackbar.service';
- import { UiBlockService } from 'src/app/components/ui-block/ui-block.service';
- import * as _ from 'lodash';
- @Component({
- selector: 'app-setting-reminder-browse',
- templateUrl: './reminder-browse.component.html'
- })
- export class ReminderBrowseComponent implements OnInit, OnDestroy {
- @Output() public onAdd: EventEmitter<CompleteReminder> = new EventEmitter();
- @Output() public onEdit: EventEmitter<CompleteReminder> = new EventEmitter();
- public contractReminder: Reminder = null;
- public contractReminderDetails: (ReminderDetailEmail | ReminderDetailUser)[] = [];
- public contractLoadingResult: boolean = false;
- public probationReminder: Reminder = null;
- public probationReminderDetails: (ReminderDetailEmail | ReminderDetailUser)[] = [];
- public probationLoadingResult: boolean = false;
- public selectedContractReminderDetail: ReminderDetailUser | ReminderDetailEmail = null;
- public selectedProbationReminderDetail: ReminderDetailUser | ReminderDetailEmail = null;
- public inputForm: FormGroup;
- public reminderFormArray: FormArray;
- private ngUnsubscribe: Subject<boolean> = new Subject();
- constructor(
- private fb: FormBuilder,
- private contentAlertService: ContentAlertService,
- private reminderService: ReminderService,
- private reminderDetailUserService: ReminderDetailUserService,
- private reminderDetailEmailService: ReminderDetailEmailService,
- private snackbarService: SnackBarService,
- private uiBlockService: UiBlockService
- ) {
- this.initForm();
- }
- ngOnInit() {
- this.doInstantFilter();
- }
- ngOnDestroy() {
- this.ngUnsubscribe.next(true);
- this.ngUnsubscribe.complete();
- }
- public initForm() {
- this.inputForm = this.fb.group({
- contractDashboardReminder: [null, Validators.required],
- contractEmailReminder: [null, Validators.required],
- probationDashboardReminder: [null, Validators.required],
- probationEmailReminder: [null, Validators.required],
- });
- this.reminderFormArray = this.inputForm.get('reminder') as FormArray;
- }
- public doInstantFilter() {
- this.selectedContractReminderDetail = null;
- this.selectedProbationReminderDetail = null;
- this.uiBlockService.showUiBlock();
- this.reminderService.search({}, { type: 'asc' })
- .pipe(takeUntil(this.ngUnsubscribe))
- .subscribe(
- (result) => {
- this.uiBlockService.hideUiBlock();
- result.data.forEach(reminder => {
- if (reminder.type === ReminderType.Contract.value) {
- this.contractReminder = _.cloneDeep(reminder);
- this.inputForm.patchValue({
- contractDashboardReminder: this.contractReminder.dashboardReminder,
- contractEmailReminder: this.contractReminder.emailReminder
- });
- this.searchContractReminderDetail();
- } else if (reminder.type === ReminderType.Probation.value) {
- this.probationReminder = _.cloneDeep(reminder);
- this.inputForm.patchValue({
- probationDashboardReminder: this.probationReminder.dashboardReminder,
- probationEmailReminder: this.probationReminder.emailReminder
- });
- this.searchProbationReminderDetail();
- }
- });
- },
- (error) => {
- this.uiBlockService.hideUiBlock();
- this.contentAlertService.error(error.errors);
- },
- () => { this.uiBlockService.hideUiBlock(); }
- );
- }
- private searchContractReminderDetail() {
- let detailUsers = [];
- this.contractReminderDetails = [];
- this.contractLoadingResult = true;
- this.reminderDetailUserService.search({ reminderId: this.contractReminder.id })
- .pipe(
- switchMap((result) => {
- detailUsers = result.data;
- return this.reminderDetailEmailService.search({ reminderId: this.contractReminder.id });
- }),
- switchMap((result) => {
- const data: (ReminderDetailEmail | ReminderDetailUser)[] = [];
- data.push(...detailUsers);
- data.push(...result.data);
- data.sort((a, b) => {
- if (a.reminderDetail.workplace === null || b.reminderDetail.workplace === null) {
- return -1;
- } else {
- return a.reminderDetail.workplace.name.localeCompare(b.reminderDetail.workplace.name);
- }
- });
- return of(data);
- }),
- takeUntil(this.ngUnsubscribe)
- )
- .subscribe(
- (result) => {
- this.contractLoadingResult = false;
- this.contractReminderDetails = result;
- },
- (error) => {
- this.contractLoadingResult = false;
- this.contentAlertService.error(error.errors);
- },
- () => { this.contractLoadingResult = false; }
- );
- }
- private searchProbationReminderDetail() {
- this.probationReminderDetails = [];
- // this.probationLoadingResult = true;
- // this.reminderDetailUserService.search({ reminderId: this.probationReminder.id })
- // .pipe(
- // switchMap((result) => {
- // this.probationReminderDetails.push(...result.data);
- // return this.reminderDetailEmailService.search({ reminderId: this.probationReminder.id });
- // }),
- // takeUntil(this.ngUnsubscribe)
- // )
- // .subscribe(
- // (result) => {
- // this.probationLoadingResult = false;
- // this.probationReminderDetails.push(...result.data);
- // this.probationReminderDetails.sort((a, b) => {
- // return a.reminderDetail.workplace.name.localeCompare(b.reminderDetail.workplace.name);
- // });
- // },
- // (error) => {
- // this.probationLoadingResult = false;
- // this.contentAlertService.error(error.errors);
- // },
- // () => { this.probationLoadingResult = false; }
- // );
- }
- public addContractReminder() {
- this.onAdd.emit(new CompleteReminder({
- reminder: this.contractReminder,
- reminderDetail: null,
- detailUsers: [],
- detailEmails: []
- }));
- }
- public addProbationReminder() {
- this.onAdd.emit(new CompleteReminder({
- reminder: this.probationReminder,
- reminderDetail: null,
- detailUsers: [],
- detailEmails: []
- }));
- }
- public saveReminder() {
- const contractReminder = _.cloneDeep(this.contractReminder);
- contractReminder.dashboardReminder = this.inputForm.value.contractDashboardReminder;
- contractReminder.emailReminder = this.inputForm.value.contractEmailReminder;
- const probationReminder = _.cloneDeep(this.probationReminder);
- probationReminder.dashboardReminder = this.inputForm.value.probationDashboardReminder;
- probationReminder.emailReminder = this.inputForm.value.probationEmailReminder;
- const reminders: Reminder[] = [];
- reminders.push(contractReminder);
- reminders.push(probationReminder);
- this.uiBlockService.showUiBlock();
- this.reminderService.editMulti(reminders)
- .pipe(takeUntil(this.ngUnsubscribe))
- .subscribe(
- (result) => {
- this.uiBlockService.hideUiBlock();
- this.snackbarService.show('reminderSetting.updated');
- },
- (error) => {
- this.uiBlockService.hideUiBlock();
- this.contentAlertService.error(error.errors);
- },
- () => { this.uiBlockService.hideUiBlock(); }
- );
- }
- public onSelectContractReminderDetail(selectedData: ReminderDetailUser | ReminderDetailEmail) {
- this.onEdit.emit(new CompleteReminder({
- reminder: selectedData.reminderDetail.reminder,
- reminderDetail: selectedData.reminderDetail,
- detailUsers: this.contractReminderDetails.filter(item => item.reminderDetail.id === selectedData.reminderDetail.id && item instanceof ReminderDetailUser) as ReminderDetailUser[],
- detailEmails: this.contractReminderDetails.filter(item => item.reminderDetail.id === selectedData.reminderDetail.id && item instanceof ReminderDetailEmail) as ReminderDetailEmail[]
- }));
- }
- public onSelectProbationReminderDetail(selectedData: ReminderDetailUser | ReminderDetailEmail) {
- this.onEdit.emit(new CompleteReminder({
- reminder: selectedData.reminderDetail.reminder,
- reminderDetail: selectedData.reminderDetail,
- detailUsers: this.probationReminderDetails.filter(item => item.reminderDetail.id === selectedData.reminderDetail.id && item instanceof ReminderDetailUser) as ReminderDetailUser[],
- detailEmails: this.probationReminderDetails.filter(item => item.reminderDetail.id === selectedData.reminderDetail.id && item instanceof ReminderDetailEmail) as ReminderDetailEmail[]
- }));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment