import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { SpinnerService } from '@core/services/spinner.service'; import { StandardProductConfigurationResources } from '@features/platform-admin/standard-product-configuration/standard-product-configuration.resources'; import { StandardProductConfigurationService } from '@features/platform-admin/standard-product-configuration/standard-product-configuration.service'; import { StandardProductDashboard, StandardProductTypes } from '@features/platform-admin/standard-product-configuration/standard-product-configuration.typing'; import { I18nService } from '@yourcause/common/i18n'; import { LogService } from '@yourcause/common/logging'; import { ConfirmationModalComponent, ModalFactory } from '@yourcause/common/modals'; import { NotifierService } from '@yourcause/common/notifier'; import { AddWidgetModalComponent } from './add-widget-modal/add-widget-modal.component'; import { DashboardNameModalComponent } from './dashboard-name-modal/dashboard-name-modal.component'; import { DashboardsResources } from './dashboards.resources'; import { DashboardsService } from './dashboards.service'; import { GCDashboards } from './dashboards.typing'; import { ManageWidgetModalComponent } from './manage-widget-modal/manage-widget-modal.component'; import { ShareDashboardModalComponent } from './share-dashboard-modal/share-dashboard-modal.component'; @Injectable() export class DashboardTabActionsService { constructor ( private i18n: I18nService, private modalFactory: ModalFactory, private dashboardsService: DashboardsService, private spinnerService: SpinnerService, private router: Router, private standardProductConfigService: StandardProductConfigurationService, private dashboardResources: DashboardsResources, private standardProductResources: StandardProductConfigurationResources, private notifier: NotifierService, private logger: LogService ) { } async shareDashboard (tab: GCDashboards.DashboardTab) { const users = await this.dashboardsService.fetchDashboardSharedUsers(tab.dashboardId); const result = await this.modalFactory.open( ShareDashboardModalComponent, { dashboard: tab, members: users }, { class: 'modal-xl '} ); if (result) { this.spinnerService.startSpinner(); await this.dashboardsService.handleShareDashboardModalResult( tab.dashboardId, result ); this.spinnerService.stopSpinner(); } } async hideDashboard (tab: GCDashboards.DashboardTab) { const deps = { confirmButtonText: this.i18n.translate( 'common:textHide', {}, 'Hide' ), confirmText: this.i18n.translate( 'DASHBOARD:textAreYouSureHide', {}, 'Hiding the dashboard will remove the tab from your current dashboard list. ' + 'You can continue to manage sharing permissions, rename, and delete the dashboard in Dashboard Manager. ' + 'In order to edit the hidden dashboard you will need to show the dashboard again. Would you like to hide the dashboard?' ), modalHeader: this.i18n.translate( 'DASHBOARD:hdrHideDashboard', {}, 'Hide Dashboard' ), modalSubHeader: tab.name }; const result = await this.modalFactory.open( ConfirmationModalComponent, deps ); if (result) { this.spinnerService.startSpinner(); await this.dashboardsService.hideDashboard(tab.dashboardId); this.spinnerService.stopSpinner(); } } async showDashboard (tab: GCDashboards.DashboardTab) { const deps = { confirmButtonText: this.i18n.translate( 'common:textShow', {}, 'Show' ), confirmText: this.i18n.translate( 'DASHBOARD:textAreYouSureShow', {}, 'Showing the dashboard will add the tab to your current dashboard list and give you the ability to view and edit. Would you like to show the dashboard?' ), modalHeader: this.i18n.translate( 'DASHBOARD:hdrShowDashboard', {}, 'Show Dashboard' ), modalSubHeader: tab.name }; const result = await this.modalFactory.open( ConfirmationModalComponent, deps ); if (result) { this.spinnerService.startSpinner(); await this.dashboardsService.showDashboard(tab.dashboardId); this.spinnerService.stopSpinner(); } } async addWidget (id: number) { if (this.dashboardsService.allWidgets.length) { const response = await this.modalFactory.open( AddWidgetModalComponent, {} ); if (response) { return this.openManageWidgetModal( id, response.template ); } } else { return this.openManageWidgetModal(id); } return false; } async openManageWidgetModal ( dashboardId: number, existingWidget?: GCDashboards.SimpleWidgetConfig ) { let widgetDetail: GCDashboards.WidgetConfig; if (existingWidget) { this.spinnerService.startSpinner(); widgetDetail = await this.dashboardsService.getWidgetDetail(existingWidget.id); this.spinnerService.stopSpinner(); } const response = await this.modalFactory.open( ManageWidgetModalComponent, { existingWidget: widgetDetail, dashboardId }, { class: 'modal-full-size' } ); if (response) { this.activateEditing(dashboardId); return true; } return false; } activateEditing (id: number) { this.dashboardsService.setEditing(true, id); } async openManageDashboardNameModal ( dashboard?: GCDashboards.DashboardTab ): Promise { const name = await this.modalFactory.open( DashboardNameModalComponent, { name: dashboard?.name } ); if (name) { this.spinnerService.startSpinner(); let dashboardId: number; if (dashboard) { await this.dashboardsService.handleRenameDashboard( dashboard.dashboardId, name, dashboard.order ); } else { dashboardId = await this.dashboardsService.handleCreateDashboard(name); } this.spinnerService.stopSpinner(); return dashboardId; } return null; } async deleteDashboard ( dashboard: GCDashboards.DashboardTab ) { const deps = { confirmButtonText: this.i18n.translate( 'common:btnDelete', {}, 'Delete' ), confirmText: this.i18n.translate( 'DASHBOARD:textAreYouSureDelete', {}, '
Deleting the dashboard has the following impact:
Are you sure you want to delete the dashboard?
' ), modalHeader: this.i18n.translate( 'DASHBOARD:hdrDeleteDashboard2', {}, 'Delete Dashboard' ), modalSubHeader: dashboard.name }; const response = await this.modalFactory.open( ConfirmationModalComponent, deps ); if (response) { this.spinnerService.startSpinner(); await this.dashboardsService.handleDeleteDashboard( dashboard.dashboardId ); this.dashboardsService.setEditing(false, null); this.spinnerService.stopSpinner(); return true; } return false; } navigateToDashboard (dashboardId: number) { this.router.navigate([`/management/home/${dashboardId}`]); } async publishStandardDB (dashboardId: number) { this.spinnerService.startSpinner(); await this.standardProductConfigService.handlePublishStandardComponents( [dashboardId], StandardProductTypes.DASHBOARDS, false ); await this.dashboardsService.resetMyDashboards(); this.spinnerService.stopSpinner(); } async unpublishStandardProductDB (dashboardId: number) { this.spinnerService.startSpinner(); await this.standardProductConfigService.handleUnpublishStandardComponents( dashboardId, StandardProductTypes.DASHBOARDS ); await this.dashboardsService.resetMyDashboards(); this.spinnerService.stopSpinner(); } async copyStandardDashboard ( standardProductDashboard: StandardProductDashboard, name: string ) { this.spinnerService.startSpinner(); const copyText = this.i18n.translate( 'common:textCopy', {}, 'Copy' ); const dashboardName = name ?? standardProductDashboard.name + ` - ${copyText}`; const dashboardId = standardProductDashboard.dashboardId; const dashboardDetail = await this.dashboardResources.getDashboardDetail(dashboardId); const order = this.dashboardsService.allDashboards.length + 1; const payload = this.standardProductConfigService.adaptForCopyStandardDashboard( dashboardDetail, dashboardId, order, dashboardName ); try { await this.standardProductResources.copyStandardDashboard(payload); await this.dashboardsService.resetMyDashboards(); this.notifier.success( this.i18n.translate( 'DASHBOARD:textSuccessfullyCopiedDashboardTemplate', {}, 'Successfully copied dashboard template' ) ); } catch (e) { this.logger.error(e); this.notifier.error( this.i18n.translate( 'DASHBOARD:textErrorCopyingDashboardTemplate', {}, 'There was an error copying the dashboard template' ) ); } this.spinnerService.stopSpinner(); } }