2021-01-28 17:33:16 +01:00
import { Component , ElementRef , OnInit , ViewChild } from '@angular/core' ;
2019-12-18 16:14:21 +01:00
import { ActivatedRoute , Router } from '@angular/router' ;
import { HelpContentService } from '../../services/help-content.service' ;
2022-09-23 12:25:08 +02:00
import { UntypedFormBuilder , UntypedFormGroup , Validators } from '@angular/forms' ;
2019-12-18 16:14:21 +01:00
import { CheckEntity , Entity } from '../../utils/entities/adminTool/entity' ;
2020-09-24 13:18:24 +02:00
import { Portal } from '../../utils/entities/adminTool/portal' ;
2019-12-18 16:14:21 +01:00
import { EnvProperties } from '../../utils/properties/env-properties' ;
import { Session } from '../../login/utils/helper.class' ;
import { UserManagementService } from '../../services/user-management.service' ;
import { Subscriber } from "rxjs" ;
2021-01-11 10:21:42 +01:00
import { properties } from "../../../../environments/environment" ;
2021-01-28 17:33:16 +01:00
import { AlertModal } from "../../utils/modal/alert" ;
2021-03-19 17:51:02 +01:00
import { StringUtils } from "../../utils/string-utils.class" ;
import { Title } from "@angular/platform-browser" ;
2022-03-22 16:47:38 +01:00
import { StakeholderService } from '../../monitor/services/stakeholder.service' ;
2022-06-26 22:30:54 +02:00
import { CommunityService } from "../../connect/community/community.service" ;
import { CommunityInfo } from "../../connect/community/communityInfo" ;
2022-07-14 10:29:29 +02:00
import { Stakeholder } from "../../monitor/entities/stakeholder" ;
2022-07-12 15:50:56 +02:00
import { ClearCacheService } from "../../services/clear-cache.service" ;
2022-07-13 19:25:19 +02:00
import { NotificationHandler } from "../../utils/notification-handler" ;
2021-02-01 20:17:55 +01:00
2019-12-18 16:14:21 +01:00
@Component ( {
selector : 'entities' ,
templateUrl : './entities.component.html' ,
} )
export class EntitiesComponent implements OnInit {
2021-01-26 17:21:55 +01:00
2021-01-28 17:33:16 +01:00
@ViewChild ( 'editModal' ) editModal : AlertModal ;
@ViewChild ( 'deleteModal' ) deleteModal : AlertModal ;
@ViewChild ( 'relatedPages' ) relatedPages : AlertModal ;
2019-12-18 16:14:21 +01:00
private selectedEntities : string [ ] = [ ] ;
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public checkboxes : CheckEntity [ ] = [ ] ;
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public entities : Entity [ ] = [ ] ;
2021-01-26 17:21:55 +01:00
2022-09-23 12:25:08 +02:00
public entityForm : UntypedFormGroup ;
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
private searchText : RegExp = new RegExp ( '' ) ;
public keyword = '' ;
2021-01-26 17:21:55 +01:00
2020-09-24 13:18:24 +02:00
public communities : Portal [ ] = [ ] ;
2021-01-26 18:00:40 +01:00
public portal : string ;
2022-07-14 10:29:29 +02:00
public type : any ;
2022-03-22 16:47:38 +01:00
public name : string ;
2022-06-26 22:30:54 +02:00
public entity : CommunityInfo | Stakeholder ;
public showLogo : boolean = true ;
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public toggleIds : string [ ] ;
public toggleStatus : boolean ;
2021-01-26 17:21:55 +01:00
public properties : EnvProperties = properties ;
2019-12-18 16:14:21 +01:00
public showLoading = true ;
public isPortalAdministrator = null ;
2022-09-23 12:25:08 +02:00
public filterForm : UntypedFormGroup ;
2019-12-18 16:14:21 +01:00
private subscriptions : any [ ] = [ ] ;
2021-01-28 17:33:16 +01:00
private index : number ;
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
constructor ( private element : ElementRef , private route : ActivatedRoute ,
2021-03-19 17:51:02 +01:00
private _router : Router , private title : Title ,
2019-12-18 16:14:21 +01:00
private _helpContentService : HelpContentService ,
2022-09-23 12:25:08 +02:00
private userManagementService : UserManagementService , private _fb : UntypedFormBuilder ,
2022-06-26 22:30:54 +02:00
private communityService : CommunityService ,
2022-07-12 15:50:56 +02:00
private stakeholderService : StakeholderService ,
private _clearCacheService : ClearCacheService ) {
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
ngOnInit() {
2021-01-11 10:21:42 +01:00
this . filterForm = this . _fb . group ( {
keyword : [ '' ] ,
2021-01-26 17:21:55 +01:00
status : [ 'all' , Validators . required ]
} ) ;
2021-01-11 10:21:42 +01:00
this . subscriptions . push ( this . filterForm . get ( 'keyword' ) . valueChanges . subscribe ( value = > {
2021-01-29 11:47:37 +01:00
this . searchText = new RegExp ( value , 'i' ) ;
this . applyFilters ( ) ;
2019-12-18 16:14:21 +01:00
} ) ) ;
2021-01-11 10:21:42 +01:00
this . subscriptions . push ( this . filterForm . get ( 'status' ) . valueChanges . subscribe ( value = > {
2021-01-29 11:47:37 +01:00
this . applyFilters ( ) ;
2021-01-11 10:21:42 +01:00
} ) ) ;
2021-01-26 17:21:55 +01:00
this . userManagementService . getUserInfo ( ) . subscribe ( user = > {
2021-01-26 18:00:40 +01:00
this . portal = ( this . route . snapshot . data . portal ) ? this . route.snapshot.data.portal : this.route.snapshot.params [ this . route . snapshot . data . param ] ;
2021-03-19 17:51:02 +01:00
if ( this . route . snapshot . data . portal ) {
2022-06-24 17:20:40 +02:00
this . name = StringUtils . capitalize ( this . portal ) ;
2021-03-19 17:51:02 +01:00
this . title . setTitle ( StringUtils . capitalize ( this . portal ) + ' | Entities' ) ;
} else if ( this . route . snapshot . params [ this . route . snapshot . data . param ] ) {
2022-07-14 10:29:29 +02:00
this . type = this . route . snapshot . data . param ;
2022-03-22 16:47:38 +01:00
if ( this . route . snapshot . data . param === 'stakeholder' ) {
this . subscriptions . push ( this . stakeholderService . getStakeholderAsObservable ( ) . subscribe ( stakeholder = > {
if ( stakeholder ) {
this . name = stakeholder . name ;
2022-06-26 22:30:54 +02:00
this . entity = stakeholder ;
this . title . setTitle ( this . name + ' | Entities' ) ;
2022-03-22 16:47:38 +01:00
}
} ) ) ;
} else {
2022-06-26 22:30:54 +02:00
this . subscriptions . push ( this . communityService . getCommunityAsObservable ( ) . subscribe ( community = > {
if ( community ) {
this . showLogo = false ;
this . name = community . shortTitle ;
this . entity = community ;
this . title . setTitle ( this . name + ' | Entities' ) ;
}
} ) ) ;
2022-03-22 16:47:38 +01:00
}
2021-03-19 17:51:02 +01:00
} else {
this . title . setTitle ( 'Administrator Dashboard | Entities' ) ;
}
2021-01-26 18:00:40 +01:00
this . applyPortalFilter ( this . portal ) ;
this . isPortalAdministrator = Session . isPortalAdministrator ( user ) && ! this . portal ;
2021-01-26 17:21:55 +01:00
} ) ;
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
ngOnDestroy ( ) : void {
this . subscriptions . forEach ( value = > {
if ( value instanceof Subscriber ) {
value . unsubscribe ( ) ;
} else if ( value instanceof Function ) {
value ( ) ;
}
} ) ;
}
2021-01-26 17:21:55 +01:00
2021-01-26 18:00:40 +01:00
getEntities ( portal : string ) {
2021-03-19 17:51:02 +01:00
this . showLoading = true ;
if ( portal ) {
2024-09-12 09:49:03 +02:00
this . _helpContentService . getCommunityEntities ( portal ) . subscribe (
2021-03-19 17:51:02 +01:00
entities = > {
this . entities = entities ;
this . checkboxes = [ ] ;
let self = this ;
entities . forEach ( _ = > {
self . checkboxes . push ( < CheckEntity > { entity : _ , checked : false } ) ;
} ) ;
this . showLoading = false ;
} ,
error = > this . handleError ( 'System error retrieving entities' , error ) ) ;
2019-12-18 16:14:21 +01:00
} else {
2024-09-12 09:49:03 +02:00
this . _helpContentService . getEntities ( ) . subscribe (
2021-03-19 17:51:02 +01:00
entities = > {
this . entities = entities ;
this . checkboxes = [ ] ;
let self = this ;
entities . forEach ( _ = > {
self . checkboxes . push ( < CheckEntity > { entity : _ , checked : false } ) ;
} ) ;
this . showLoading = false ;
} ,
error = > this . handleError ( 'System error retrieving entities' , error ) ) ;
2019-12-18 16:14:21 +01:00
}
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public toggleCheckBoxes ( event ) {
this . checkboxes . forEach ( _ = > _ . checked = event . target . checked ) ;
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public applyCheck ( flag : boolean ) {
this . checkboxes . forEach ( _ = > _ . checked = flag ) ;
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public getSelectedEntities ( ) : string [ ] {
return this . checkboxes . filter ( entity = > entity . checked === true ) . map ( checkedEntity = > checkedEntity . entity ) . map ( res = > res . _id ) ;
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
private deleteEntitiesFromArray ( ids : string [ ] ) : void {
for ( let id of ids ) {
2021-01-28 17:33:16 +01:00
let i = this . entities . findIndex ( _ = > _ . _id == id ) ;
this . entities . splice ( i , 1 ) ;
2019-12-18 16:14:21 +01:00
}
2021-01-29 11:47:37 +01:00
this . applyFilters ( ) ;
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public confirmDeleteEntity ( id : string ) {
// this.deleteConfirmationModal.ids = [id];
// this.deleteConfirmationModal.showModal();
this . selectedEntities = [ id ] ;
this . confirmDeleteEntitiesModalOpen ( ) ;
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public confirmDeleteSelectedEntities() {
// this.deleteConfirmationModal.ids = this.getSelectedEntities();
// this.deleteConfirmationModal.showModal();
this . selectedEntities = this . getSelectedEntities ( ) ;
this . confirmDeleteEntitiesModalOpen ( ) ;
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
private confirmDeleteEntitiesModalOpen() {
2021-03-19 17:51:02 +01:00
this . deleteModal . alertTitle = 'Delete Confirmation' ;
this . deleteModal . message = 'Are you sure you want to delete the selected entity(-ies)?' ;
this . deleteModal . okButtonText = 'Yes' ;
this . deleteModal . open ( ) ;
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public confirmedDeleteEntities ( data : any ) {
2021-03-19 17:51:02 +01:00
this . showLoading = true ;
2024-09-12 09:49:03 +02:00
this . _helpContentService . deleteEntities ( this . selectedEntities ) . subscribe (
2021-03-19 17:51:02 +01:00
_ = > {
this . deleteEntitiesFromArray ( this . selectedEntities ) ;
2022-07-13 19:25:19 +02:00
NotificationHandler . rise ( 'Entities have been <b>successfully deleted</b>' ) ;
2021-03-19 17:51:02 +01:00
this . showLoading = false ;
2024-04-03 09:26:29 +02:00
// this._clearCacheService.clearCache("entities deleted");
// this._clearCacheService.purgeBrowserCache("entities deleted", this.portal);
2021-03-19 17:51:02 +01:00
} ,
error = > this . handleUpdateError ( 'System error deleting the selected entities' , error )
) ;
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public editEntity ( i : number ) {
const entity : Entity = this . checkboxes [ i ] . entity ;
2021-01-28 17:33:16 +01:00
this . index = this . entities . findIndex ( value = > value . _id === entity . _id ) ;
this . entityForm = this . _fb . group ( {
name : this._fb.control ( entity . name , Validators . required ) ,
_id : this._fb.control ( entity . _id ) ,
pid : this._fb.control ( entity . pid , Validators . required )
2019-12-18 16:14:21 +01:00
} ) ;
2021-01-28 17:33:16 +01:00
this . entitiesModalOpen ( 'Edit Entity' , 'Save Changes' ) ;
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public newEntity() {
2021-01-28 17:33:16 +01:00
this . entityForm = this . _fb . group ( {
_id : this._fb.control ( null ) ,
name : this._fb.control ( '' , Validators . required ) ,
pid : this._fb.control ( '' , Validators . required )
2019-12-18 16:14:21 +01:00
} ) ;
2021-01-28 17:33:16 +01:00
this . entitiesModalOpen ( 'Create Entity' , 'Create' ) ;
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2021-01-28 17:33:16 +01:00
private entitiesModalOpen ( title : string , yesBtn : string ) {
2021-03-19 17:51:02 +01:00
this . editModal . okButtonLeft = false ;
this . editModal . alertTitle = title ;
this . editModal . okButtonText = yesBtn ;
this . editModal . open ( ) ;
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public entitySaveConfirmed ( data : any ) {
2021-02-01 20:17:55 +01:00
this . showLoading = true ;
2022-07-13 19:25:19 +02:00
if ( this . entityForm . getRawValue ( ) . _id ) {
2021-03-19 17:51:02 +01:00
this . _helpContentService . updateEntity (
2024-09-12 09:49:03 +02:00
< Entity > this . entityForm . getRawValue ( ) ) . subscribe (
2021-03-19 17:51:02 +01:00
entity = > {
this . entityUpdatedSuccessfully ( entity ) ;
2022-07-13 19:25:19 +02:00
NotificationHandler . rise ( 'Entity <b>' + entity . name + '</b> has been <b>successfully updated</b>' ) ;
2024-04-03 09:26:29 +02:00
// this._clearCacheService.clearCache("entity updated");
// this._clearCacheService.purgeBrowserCache("entity updated", this.portal);
2021-03-19 17:51:02 +01:00
} ,
error = > this . handleUpdateError ( 'System error updating entity' , error )
) ;
2019-12-18 16:14:21 +01:00
} else {
2024-09-12 09:49:03 +02:00
this . _helpContentService . saveEntity ( < Entity > this . entityForm . getRawValue ( ) ) . subscribe (
2021-03-19 17:51:02 +01:00
entity = > {
this . entitySavedSuccessfully ( entity ) ;
2022-07-13 19:25:19 +02:00
NotificationHandler . rise ( 'Entity <b>' + entity . name + '</b> has been <b>successfully created</b>' ) ;
2024-04-03 09:26:29 +02:00
// this._clearCacheService.clearCache("entity saved");
// this._clearCacheService.purgeBrowserCache("entity saved", this.portal)
2021-03-19 17:51:02 +01:00
} ,
error = > this . handleUpdateError ( 'System error creating entity' , error )
) ;
2019-12-18 16:14:21 +01:00
}
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public entitySavedSuccessfully ( entity : Entity ) {
2021-01-28 17:33:16 +01:00
this . entities . push ( entity ) ;
2021-01-29 11:47:37 +01:00
this . applyFilters ( ) ;
2019-12-18 16:14:21 +01:00
this . applyCheck ( false ) ;
2021-02-01 20:17:55 +01:00
this . showLoading = false ;
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public entityUpdatedSuccessfully ( entity : Entity ) {
2021-01-28 17:33:16 +01:00
this . entities [ this . index ] = entity ;
2021-01-29 11:47:37 +01:00
this . applyFilters ( ) ;
2019-12-18 16:14:21 +01:00
this . applyCheck ( false ) ;
2021-02-01 20:17:55 +01:00
this . showLoading = false ;
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2021-01-29 11:47:37 +01:00
public applyFilters() {
2019-12-18 16:14:21 +01:00
this . checkboxes = [ ] ;
2021-01-29 11:47:37 +01:00
this . entities . filter ( item = > this . filterEntitiesByStatus ( item ) ) . forEach (
2019-12-18 16:14:21 +01:00
_ = > this . checkboxes . push ( < CheckEntity > { entity : _ , checked : false } )
) ;
2021-01-29 11:47:37 +01:00
this . checkboxes = this . checkboxes . filter ( item = > this . filterEntities ( item . entity ) ) ;
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public filterEntities ( entity : Entity ) : boolean {
const textFlag = this . searchText . toString ( ) === '' || ( entity . name ) . match ( this . searchText ) != null ;
return textFlag ;
}
2021-01-26 17:21:55 +01:00
2021-01-11 10:21:42 +01:00
public filterEntitiesByStatus ( entity : Entity ) : boolean {
let status = this . filterForm . get ( "status" ) . value ;
2021-01-26 17:21:55 +01:00
return status == "all" || ( status == "disabled" && ! entity . isEnabled ) || ( status == "enabled" && entity . isEnabled ) ;
2021-01-11 10:21:42 +01:00
}
2021-01-26 17:21:55 +01:00
2022-07-13 19:25:19 +02:00
handleError ( message : string , error = null ) {
if ( error ) {
console . log ( 'Server responded: ' + error ) ;
}
NotificationHandler . rise ( message , 'danger' ) ;
2019-12-18 16:14:21 +01:00
this . showLoading = false ;
}
2021-01-26 17:21:55 +01:00
2022-07-13 19:25:19 +02:00
handleUpdateError ( message : string , error = null ) {
if ( ! error ) {
2021-01-28 17:33:16 +01:00
this . entityForm = this . _fb . group ( {
2019-12-18 16:14:21 +01:00
pid : [ '' , Validators . required ] ,
name : [ '' , Validators . required ] ,
isEnabled : '' ,
_id : ''
} ) ;
} else {
console . log ( 'Server responded: ' + error ) ;
}
2022-07-13 19:25:19 +02:00
NotificationHandler . rise ( message , 'danger' ) ;
2019-12-18 16:14:21 +01:00
this . showLoading = false ;
}
2021-01-26 17:21:55 +01:00
2021-01-26 18:00:40 +01:00
public applyPortalFilter ( portal : string ) {
this . getEntities ( portal ) ;
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public toggleEntities ( status : boolean , ids : string [ ] ) {
// this.okModal.showModal();
this . toggleIds = ids ;
this . toggleStatus = status ;
this . confirmRelatedPagesModalOpen ( ) ;
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
private confirmRelatedPagesModalOpen() {
2021-03-19 17:51:02 +01:00
this . relatedPages . alertTitle = 'Warning' ;
this . relatedPages . message = "This action will affect all search pages related to this entity! Pages' status will change to entity's status! Do you want to continue?" ;
this . relatedPages . okButtonText = 'Yes' ;
this . relatedPages . open ( ) ;
2019-12-18 16:14:21 +01:00
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public continueToggling ( event : any ) {
2021-03-19 17:51:02 +01:00
this . _helpContentService . toggleEntities (
2024-09-12 09:49:03 +02:00
this . portal , this . toggleIds , this . toggleStatus ) . subscribe (
2021-03-19 17:51:02 +01:00
( ) = > {
for ( let id of this . toggleIds ) {
const i = this . checkboxes . findIndex ( _ = > _ . entity . _id === id ) ;
this . checkboxes [ i ] . entity . isEnabled = this . toggleStatus ;
}
this . applyCheck ( false ) ;
2024-07-01 15:48:10 +02:00
this . _clearCacheService . purgeBrowserCache ( "entity's status changed" , this . portal ) ;
2022-08-11 11:58:20 +02:00
this . _clearCacheService . purgeBrowserCache ( "entity's status changed" , this . portal ) ;
2021-03-19 17:51:02 +01:00
} ,
error = > this . handleUpdateError ( 'System error changing the status of the selected entity(-ies)' , error )
) ;
2019-12-18 16:14:21 +01:00
}
2021-02-02 13:29:18 +01:00
2021-03-19 17:51:02 +01:00
selectAll() {
let checked = ( this . getSelectedEntities ( ) . length != this . checkboxes . length ) ;
2021-02-25 16:55:56 +01:00
for ( let check of this . checkboxes ) {
check . checked = checked ;
}
}
2019-12-18 16:14:21 +01:00
}