import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { APIAdminClient } from '@core/typings/api/admin-client.typing'; import { LoginBehaviors } from '@core/typings/login-behaviors.typing'; import { AdminUserPermissions } from '@core/typings/user.typing'; import { UserService } from '@features/users/user.service'; import { DebounceFactory, FileService, PaginationOptions, TableCSVFactory, TopLevelFilter } from '@yourcause/common'; import { I18nService } from '@yourcause/common/i18n'; import { LogService } from '@yourcause/common/logging'; import { NotifierService } from '@yourcause/common/notifier'; import { AdminClientService } from '../admin-client.service'; @Component({ selector: 'gc-clients-page', templateUrl: './clients-page.component.html', styleUrls: ['./clients-page.component.scss'] }) export class ClientsPageComponent implements OnInit { topLevelFilters: TopLevelFilter[] = []; csrClients: APIAdminClient.CsrClientNotInGc[] = []; createOrEditClient = this.userService.adminPermissions.includes( AdminUserPermissions.CreateOrEditClient ); tableCSVFactory: TableCSVFactory; ready = false; rows = this.adminClientService.clients; constructor ( private logger: LogService, private adminClientService: AdminClientService, private i18n: I18nService, private userService: UserService, private notifier: NotifierService, private fileService: FileService, private router: Router ) { } async ngOnInit () { this.topLevelFilters = [ new TopLevelFilter( 'text', 'name', '', this.i18n.translate( 'GLOBAL:textSearchForAClient', {}, 'Search for a client' ) ) ]; if (this.createOrEditClient) { await this.getCsrClients(); } this.tableCSVFactory = DebounceFactory.createSimple( async (options: PaginationOptions) => { const result = await this.adminClientService.getClients(options); const records = result.records.map((r: any) => { r.rootUserEmail = r.rootUser?.email; r.rootUserJobTitle = r.rootUser?.jobTitle; r.rootUserName = r.rootUser?.firstName ? (r.rootUser?.firstName + ' ' + r.rootUser?.lastName as any) : null; r.rootUser = null; return r; }); return this.fileService.convertObjectArrayToCSVString(records); }); this.ready = true; } showLoginBehavior (loginBehavior: number) { switch (loginBehavior) { case LoginBehaviors.PlainLogin: return this.i18n.translate( 'ADMIN:textPlainLogin', {}, 'Plain login' ); case LoginBehaviors.SSO: return this.i18n.translate( 'ADMIN:textSSO', {}, 'SSO' ); case LoginBehaviors.SSOAndPlainLogin: return this.i18n.translate( 'ADMIN:textSSOAndPlainLogin', {}, 'SSO and plain login' ); default: return ''; } } async getCsrClients () { try { this.csrClients = await this.adminClientService.getCSRClientsNotInGC(); } catch (error) { this.logger.error(error); this.notifier.error(this.i18n.translate( 'ADMIN:notificationErrorGettingCSRClients', {}, 'There was an error getting the CSR client list.' )); } } goToCreateClient () { this.router.navigate(['/platform/client-management/clients/new']); } goToEditClient (id: number) { this.router.navigate([`/platform/client-management/clients/${id}/edit`]); } goToViewClient (id: number) { this.router.navigate([`/platform/client-management/clients/${id}/view`]); } }