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' ;
2021-01-28 17:33:16 +01:00
import { FormBuilder , FormGroup , 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 { LoginErrorCodes } from '../../login/utils/guardHelper.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-26 17:21:55 +01:00
import { ConnectHelper } from "../../connect/connectHelper" ;
2021-01-28 17:33:16 +01:00
import { AlertModal } from "../../utils/modal/alert" ;
2021-02-02 13:29:18 +01:00
import { SearchInputComponent } from "../../sharedComponents/search-input/search-input.component" ;
2019-12-18 16:14:21 +01:00
2021-02-01 20:17:55 +01:00
declare var UIkit ;
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
2021-01-28 17:33:16 +01:00
public entityForm : FormGroup ;
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 ;
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 errorMessage = '' ;
public updateErrorMessage = '' ;
public modalErrorMessage = '' ;
public isPortalAdministrator = null ;
2021-01-11 10:21:42 +01:00
public filterForm : FormGroup ;
2019-12-18 16:14:21 +01:00
private subscriptions : any [ ] = [ ] ;
2021-01-28 17:33:16 +01:00
private index : number ;
2021-02-02 13:29:18 +01:00
public selectedKeyword : string ;
@ViewChild ( 'searchInputComponent' ) searchInputComponent : SearchInputComponent ;
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
constructor ( private element : ElementRef , private route : ActivatedRoute ,
private _router : Router ,
private _helpContentService : HelpContentService ,
private userManagementService : UserManagementService , private _fb : FormBuilder ) {
}
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-02 16:52:09 +01:00
ConnectHelper . setPortalTypeFromPid ( this . portal ) ;
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 ) {
2019-12-18 16:14:21 +01:00
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
} else {
this . showLoading = true ;
this . updateErrorMessage = '' ;
this . errorMessage = '' ;
2021-01-26 18:00:40 +01:00
if ( portal ) {
this . _helpContentService . getCommunityEntities ( portal , this . properties . adminToolsAPIURL ) . subscribe (
2019-12-18 16:14:21 +01:00
entities = > {
this . entities = entities ;
this . checkboxes = [ ] ;
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
let self = this ;
entities . forEach ( _ = > {
self . checkboxes . push ( < CheckEntity > { entity : _ , checked : false } ) ;
} ) ;
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
this . showLoading = false ;
} ,
error = > this . handleError ( 'System error retrieving entities' , error ) ) ;
} else {
this . _helpContentService . getEntities ( this . properties . adminToolsAPIURL ) . subscribe (
entities = > {
this . entities = entities ;
this . checkboxes = [ ] ;
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
let self = this ;
entities . forEach ( _ = > {
self . checkboxes . push ( < CheckEntity > { entity : _ , checked : false } ) ;
} ) ;
this . showLoading = false ;
} ,
2021-01-11 10:21:42 +01:00
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() {
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
} else {
2021-01-28 17:33:16 +01:00
this . deleteModal . cancelButton = true ;
this . deleteModal . okButton = true ;
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 ) {
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
} else {
this . showLoading = true ;
this . updateErrorMessage = '' ;
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
this . _helpContentService . deleteEntities ( this . selectedEntities , this . properties . adminToolsAPIURL ) . subscribe (
_ = > {
this . deleteEntitiesFromArray ( this . selectedEntities ) ;
2021-02-01 20:17:55 +01:00
UIkit . notification ( 'Entities have been <b>successfully deleted</b>' , {
status : 'success' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
2019-12-18 16:14:21 +01:00
this . showLoading = false ;
} ,
error = > this . handleUpdateError ( 'System error deleting the selected entities' , error )
) ;
}
}
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
} ) ;
this . modalErrorMessage = '' ;
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
} ) ;
this . modalErrorMessage = '' ;
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 ) {
2019-12-18 16:14:21 +01:00
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
} else {
2021-01-28 17:33:16 +01:00
this . editModal . cancelButton = true ;
this . editModal . okButton = true ;
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 ;
2019-12-18 16:14:21 +01:00
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
} else {
this . modalErrorMessage = '' ;
2021-01-28 17:33:16 +01:00
if ( this . entityForm . value . _id ) {
2019-12-18 16:14:21 +01:00
this . _helpContentService . updateEntity (
2021-01-28 17:33:16 +01:00
< Entity > this . entityForm . value , this . properties . adminToolsAPIURL ) . subscribe (
2019-12-18 16:14:21 +01:00
entity = > {
this . entityUpdatedSuccessfully ( entity ) ;
2021-02-01 20:17:55 +01:00
UIkit . notification ( 'Entity <b>' + entity . name + '</b> has been <b>successfully updated</b>' , {
status : 'success' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
2019-12-18 16:14:21 +01:00
} ,
error = > this . handleUpdateError ( 'System error updating entity' , error )
) ;
} else {
2021-01-28 17:33:16 +01:00
this . _helpContentService . saveEntity ( < Entity > this . entityForm . value , this . properties . adminToolsAPIURL ) . subscribe (
2019-12-18 16:14:21 +01:00
entity = > {
this . entitySavedSuccessfully ( entity ) ;
2021-02-01 20:17:55 +01:00
UIkit . notification ( 'Entity <b>' + entity . name + '</b> has been <b>successfully created</b>' , {
status : 'success' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
2019-12-18 16:14:21 +01:00
} ,
error = > this . handleUpdateError ( 'System error creating entity' , error )
) ;
}
}
}
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
2019-12-18 16:14:21 +01:00
handleError ( message : string , error ) {
this . errorMessage = message ;
console . log ( 'Server responded: ' + error ) ;
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
this . showLoading = false ;
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
handleUpdateError ( message : string , error ) {
if ( error == null ) {
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 {
this . updateErrorMessage = message ;
console . log ( 'Server responded: ' + error ) ;
}
2021-02-01 20:17:55 +01:00
UIkit . notification ( 'An error has occurred. Please try again later' , {
status : 'danger' ,
timeout : 6000 ,
pos : 'bottom-right'
} ) ;
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() {
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
} else {
2021-01-28 17:33:16 +01:00
this . relatedPages . cancelButton = true ;
this . relatedPages . okButton = true ;
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 ) {
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
} else {
this . updateErrorMessage = '' ;
this . _helpContentService . toggleEntities (
2021-01-26 18:00:40 +01:00
this . portal , this . toggleIds , this . toggleStatus , this . properties . adminToolsAPIURL ) . subscribe (
2019-12-18 16:14:21 +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 ) ;
} ,
error = > this . handleUpdateError ( 'System error changing the status of the selected entity(-ies)' , error )
) ;
}
}
2021-02-02 13:29:18 +01:00
public onSearchClose() {
this . selectedKeyword = this . filterForm . get ( 'keyword' ) . value ;
}
public reset() {
this . selectedKeyword = null ;
this . searchInputComponent . reset ( )
}
2021-02-25 16:55:56 +01:00
selectAll ( ) {
let checked = ! ! ( this . getSelectedEntities ( ) . length != this . checkboxes . length ) ;
for ( let check of this . checkboxes ) {
check . checked = checked ;
}
}
2019-12-18 16:14:21 +01:00
}