import { Component, Injector, OnInit, Output, EventEmitter, AfterViewInit, ViewChild, Input } from '@angular/core'; import { appModuleAnimation } from '@shared/animations/routerTransition'; import { AppComponentBase } from '@shared/common/app-component-base'; import { MobileClientServiceProxy, GetMobileUserPreferenceCodesListItemDto, UserPreferencesServiceProxy, GetForMobileClientUserListItemDto, CreateOrUpdateForMobileClientUserInputDto } from '@shared/service-proxies/service-proxies'; import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'; @Component({ selector: 'drivers-permissions', templateUrl: './drivers-permissions.component.html', animations: [appModuleAnimation()] }) export class DriversPermissionsComponent extends AppComponentBase implements OnInit, AfterViewInit { mobileUserPreferenceCodes: Array = []; userPreferences: Map; permissionsTableItems: Array = []; @Input() userId: number; constructor( injector: Injector, private _mobileClientServiceProxy: MobileClientServiceProxy, private _userPreferencesServiceProxy: UserPreferencesServiceProxy ) { super(injector); } ngOnInit(): void { } ngAfterViewInit(): void { this.loadDriverPermissions(); } loadDriverPermissions(event?: LazyLoadEvent): void { this.getMobilePreferenceCodesFromApi(() => { this.permissionsTableItems = []; this.mobileUserPreferenceCodes.forEach((item) => { let mapValue = this.userPreferences[item]; if (mapValue != undefined) { this.permissionsTableItems.push( new DriverPermissionDisplayItem(this.userId, item, mapValue) ); } else { this.permissionsTableItems.push( new DriverPermissionDisplayItem(this.userId, item, false) ); } }); }); } getMobilePreferenceCodesFromApi(onFinish: () => void): void { this._mobileClientServiceProxy.getMobileUserPreferenceCodes("SprintHub") .subscribe(result => { this.mobileUserPreferenceCodes = []; if (result.items) { result.items.forEach((item, index) => { this.mobileUserPreferenceCodes.push(item.userPreferenceCode); }); } this.getUserPreferencesFromApi(() => { onFinish(); }); }); } getUserPreferencesFromApi(onFinish: () => void): void { this.userPreferences = new Map(); this._userPreferencesServiceProxy.getForMobileClientUser(this.userId) .subscribe(result => { if (result.items) { result.items.forEach((item, index) => { this.userPreferences[item.code] = item.value == "true"; }); } onFinish(); }); } onPermissionStateChange(record: DriverPermissionDisplayItem): void { let newValue = !record.value if (newValue) { let inputData = new CreateOrUpdateForMobileClientUserInputDto(); inputData.userId = this.userId; inputData.preferenceCode = record.code; inputData.value = newValue.toString(); this._userPreferencesServiceProxy.createOrUpdateForMobileClientUser(inputData) .subscribe(result => { }); } else { this._userPreferencesServiceProxy.deleteForMobileClientUser(this.userId, record.code) .subscribe(result => { }); } } } class DriverPermissionDisplayItem { userId: number; code: string; value: boolean; constructor(userId: number, code: string, value: boolean) { this.userId = userId; this.code = code; this.value = value; } }