single staff ts
unknown
plain_text
2 years ago
9.7 kB
3
Indexable
Never
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout'; import { Component, OnInit } from '@angular/core'; import { MatDialog } from '@angular/material/dialog'; import { MatSnackBar } from '@angular/material/snack-bar'; import { Sort } from '@angular/material/sort'; import { ActivatedRoute, Router } from '@angular/router'; import { EMPTY, iif, Observable, throwError } from 'rxjs'; import { exhaustMap, map, mergeMap, switchMap, tap } from 'rxjs/operators'; import { CustomSort, Filters, TableWrapperComponent } from 'src/app/components/table-wrapper/table-wrapper.component'; import { NoPermissionError } from 'src/core/errors/no-permission.error'; import { PartnershipManagerInvitation, SaveSponsorStaff, SponsorStaffData } from 'src/core/models/partners-contracts.model'; import { Result, ResultBase } from 'src/core/models/result.model'; import { PartnershipService } from 'src/core/services/partnership.service'; import { SponsorService } from 'src/core/services/partnership/sponsors/sponsor.service'; import { SnackbarService } from 'src/core/services/snackbar.service'; import { DeleteStaffDialogComponent } from '../../single-sponsorship-dialogs/delete-staff-dialog/delete-staff-dialog.component'; import { ImportStaffDialogComponent } from '../../single-sponsorship-dialogs/import-staff-dialog/import-staff-dialog.component'; import { InviteUserDialogComponent } from '../../single-sponsorship-dialogs/invite-user-dialog/invite-user-dialog.component'; import { SaveStaffDialogComponent } from '../../single-sponsorship-dialogs/save-staff-dialog/save-staff-dialog.component'; import { SingleSponsorshipComponent } from '../../single-sponsorship.component'; @Component({ selector: 'app-single-sponsorship-staff', templateUrl: './single-sponsorship-staff.component.html' }) export class SingleSponsorshipStaffComponent extends TableWrapperComponent implements OnInit { private readonly sponsorService: SponsorService; displayedColumns: string[] = []; partnershipId: number; constructor( private breakpointObserver: BreakpointObserver, readonly snackbar: MatSnackBar, readonly dialog: MatDialog, readonly snackbarService: SnackbarService, readonly partnershipService: PartnershipService, readonly parentComponent: SingleSponsorshipComponent, private route: ActivatedRoute, private router: Router ) { super(); this.partnershipId = Number(this.route.snapshot.paramMap.get('id')); this.sponsorService = this.partnershipService.generateSponsorsService(this.partnershipId); } ngOnInit() { this.breakpointObserver .observe([Breakpoints.Small, Breakpoints.HandsetPortrait]) .pipe( map(value => { value.matches ? this.displayedColumns = ['statusId', 'isEventRepresentative', 'name', 'delete'] : this.displayedColumns = ['statusId', 'isEventRepresentative', 'isUsedForMailings', 'name', 'email', 'phone', 'jobRole', 'manager', 'delete']; }) ) .subscribe(); } onFilterChange(event: Event) { const filterValue = (event.target as HTMLInputElement).value; this.onSearchChange.next(filterValue); } onSortChanged(sort: Sort) { this.onSortChange.next(new CustomSort(sort)); } onImportSponsorStaffData() { this.dialog .open(ImportStaffDialogComponent, { data: { partnershipId: this.partnershipId }, disableClose: true, autoFocus: false, panelClass: ['mat-dialog', 'big-mat-dialog', 'full-height-mat-dialog'], maxHeight: '100vh' }).afterClosed() .pipe( switchMap((newStaff: SponsorStaffData[]) => { if (!newStaff || !newStaff.length) { return EMPTY; }; return this.sponsorService.saveImportedStaffData(newStaff); }), tap((response: ResultBase) => { if (response.successful) { this.onDataChange.next(); this.snackbarService.open('PartnersContracts.SingleSponsorship.Staff.SuccessfulImport', 'Staff imported successfully'); } else { this.snackbarService.open('PartnersContracts.SingleSponsorship.Staff.ErrorImport', 'Error while importing staff!'); } }) ).subscribe(); } onAddSponsorStaffData() { this.dialog .open(SaveStaffDialogComponent, { data: { staffData: null, isEdit: false }, disableClose: true, autoFocus: false, panelClass: ['mat-dialog', 'big-mat-dialog', 'full-height-mat-dialog'], maxHeight: '100vh' }).afterClosed() .pipe( mergeMap((newStaffData: SaveSponsorStaff) => { if (!newStaffData) { return EMPTY; } return this.sponsorService.saveStaffData(newStaffData); }), tap((response: Result<number>) => { if (response.data && response.successful) { this.onDataChange.next(); this.parentComponent.partnershipStaffChanged.next(true); this.snackbarService.open('PartnersContracts.SingleSponsorship.Staff.SuccessfulSave', 'Staff saved successfully'); } }), ).subscribe(); } onEditSponsorStaffData(element: SponsorStaffData) { this.dialog .open(SaveStaffDialogComponent, { data: { staffData: element, isEdit: true }, disableClose: true, autoFocus: false, panelClass: ['mat-dialog', 'big-mat-dialog', 'full-height-mat-dialog'], maxHeight: '100vh' }).afterClosed() .pipe( mergeMap((editedStaffData: SaveSponsorStaff) => { if (!editedStaffData) { return EMPTY; } return this.sponsorService.saveStaffData(editedStaffData); }), tap((response: Result<number>) => { if (response.data && response.successful) { this.onDataChange.next(); this.parentComponent.partnershipStaffChanged.next(true); this.snackbarService.open('PartnersContracts.SingleSponsorship.Staff.SuccessfulSave', 'Staff saved successfully'); } else { this.snackbarService.open('PartnersContracts.SingleSponsorship.Staff.ErrorSave', 'Error while saving staff!'); } }), ).subscribe(); } onRemoveSponsorStaffData(element: SponsorStaffData) { this.dialog .open(DeleteStaffDialogComponent, { data: { name: element.name }, disableClose: true, autoFocus: false, panelClass: "mat-dialog", maxHeight: '100vh' }).afterClosed() .pipe( mergeMap(isDelete => iif(() => isDelete, this.sponsorService.deleteStaff(element.id)) ), tap((response: ResultBase) => { if (response.successful) { this.onDataChange.next(); this.parentComponent.partnershipStaffChanged.next(true); this.snackbarService.open('PartnersContracts.SingleSponsorship.Staff.SuccessfulDelete', 'Sponsor staff deleted successfully'); } else { this.snackbarService.open('PartnersContracts.SingleSponsorship.Staff.DeleteError', 'Error while deleting sponsor staff!'); } }) ).subscribe(); } onInviteUser(element: SponsorStaffData) { this.dialog .open(InviteUserDialogComponent, { data: element, disableClose: true, autoFocus: false, panelClass: "mat-dialog", maxHeight: '100vh' }).afterClosed() .pipe( exhaustMap((data: PartnershipManagerInvitation) => data ? this.partnershipService.sendPartnershipManagerInvitation({ ...data, partnershipId: this.partnershipId }) : EMPTY ), tap((response: ResultBase) => { response.successful ? this.snackbarService.open('PartnersContracts.SingleSponsorship.Staff.SuccessfulInvitation', 'Invitation sent successfully') : null; }) ) .subscribe(); } filter(data: Filters): Observable<any[]> { return this.sponsorService.staff .pipe( tap({ error: error => error instanceof NoPermissionError ? this.router.navigate(['..'], { relativeTo: this.route }) : throwError(error) }) ); } }