2019-12-18 16:14:21 +01:00
import { Component , ViewChild , OnInit , ElementRef } from '@angular/core' ;
import { ActivatedRoute , Router } from '@angular/router' ;
import { HelpContentService } from '../../services/help-content.service' ;
import { FormBuilder , FormControl , FormGroup , Validators } from '@angular/forms' ;
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 { HelperFunctions } from "../../utils/HelperFunctions.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" ;
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
2019-12-18 16:14:21 +01:00
@ViewChild ( 'AlertModalSaveEntity' ) alertModalSaveEntity ;
@ViewChild ( 'AlertModalDeleteEntities' ) alertModalDeleteEntities ;
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
2019-12-18 16:14:21 +01:00
public myForm : 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
@ViewChild ( 'AlertModalRelatedPages' ) alertModalRelatedPages ;
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-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 ]
} ) ;
2019-12-18 16:14:21 +01:00
this . myForm = this . _fb . group ( {
pid : [ '' , Validators . required ] ,
name : [ '' , Validators . required ] ,
isEnabled : '' ,
_id : ''
} ) ;
2021-01-11 10:21:42 +01:00
this . subscriptions . push ( this . filterForm . get ( 'keyword' ) . valueChanges . subscribe ( value = > {
2019-12-18 16:14:21 +01:00
this . filterBySearch ( value ) ;
} ) ) ;
2021-01-11 10:21:42 +01:00
this . subscriptions . push ( this . filterForm . get ( 'status' ) . valueChanges . subscribe ( value = > {
this . applyStatusFilter ( ) ;
} ) ) ;
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 ] ;
if ( this . portal === 'connect' || this . portal === 'explore' ) {
ConnectHelper . setPortalTypeFromPid ( this . portal ) ;
}
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 ) {
const i = this . checkboxes . findIndex ( _ = > _ . entity . _id === id ) ;
this . checkboxes . splice ( i , 1 ) ;
}
}
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 {
this . alertModalDeleteEntities . cancelButton = true ;
this . alertModalDeleteEntities . okButton = true ;
this . alertModalDeleteEntities . alertTitle = 'Delete Confirmation' ;
this . alertModalDeleteEntities . message = 'Are you sure you want to delete the selected entity(-ies)?' ;
this . alertModalDeleteEntities . okButtonText = 'Yes' ;
this . alertModalDeleteEntities . open ( ) ;
}
}
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 ) ;
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 ;
this . myForm = this . _fb . group ( {
2019-12-20 12:38:13 +01:00
name : [ entity . name , Validators . required ] ,
2019-12-18 16:14:21 +01:00
_id : entity._id ,
2019-12-20 12:38:13 +01:00
pid : [ entity . pid , Validators . required ] ,
2019-12-18 16:14:21 +01:00
} ) ;
this . modalErrorMessage = '' ;
this . entitiesModalOpen ( this . alertModalSaveEntity , '' , 'Save Changes' ) ;
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public newEntity() {
this . myForm = this . _fb . group ( {
pid : [ '' , Validators . required ] ,
name : [ '' , Validators . required ] ,
isEnabled : '' ,
_id : ''
} ) ;
this . modalErrorMessage = '' ;
this . entitiesModalOpen ( this . alertModalSaveEntity , '' , 'Save' ) ;
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
private entitiesModalOpen ( modal : any , title : string , yesBtn : string ) {
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
} else {
modal . cancelButton = true ;
modal . okButton = true ;
modal . alertTitle = title ;
modal . okButtonText = yesBtn ;
modal . open ( ) ;
}
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public entitySaveConfirmed ( data : any ) {
if ( ! Session . isLoggedIn ( ) ) {
this . _router . navigate ( [ '/user-info' ] ,
{ queryParams : { 'errorCode' : LoginErrorCodes . NOT_VALID , 'redirectUrl' : this . _router . url } } ) ;
} else {
this . modalErrorMessage = '' ;
if ( this . myForm . getRawValue ( ) [ '_id' ] . length > 0 ) {
this . _helpContentService . updateEntity (
< Entity > this . myForm . value , this . properties . adminToolsAPIURL ) . subscribe (
entity = > {
this . entityUpdatedSuccessfully ( entity ) ;
} ,
error = > this . handleUpdateError ( 'System error updating entity' , error )
) ;
} else {
this . _helpContentService . saveEntity ( < Entity > this . myForm . value , this . properties . adminToolsAPIURL ) . subscribe (
entity = > {
this . entitySavedSuccessfully ( entity ) ;
} ,
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 ) {
this . checkboxes . push ( < CheckEntity > { entity : entity , checked : false } ) ;
this . applyCheck ( false ) ;
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public entityUpdatedSuccessfully ( entity : Entity ) {
this . checkboxes . find ( checkItem = > checkItem . entity . _id === entity . _id ) . entity = entity ;
this . applyCheck ( false ) ;
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public filterBySearch ( text : string ) {
this . searchText = new RegExp ( text , 'i' ) ;
this . applyFilter ( ) ;
}
2021-01-26 17:21:55 +01:00
2019-12-18 16:14:21 +01:00
public applyFilter() {
this . checkboxes = [ ] ;
this . entities . filter ( item = > this . filterEntities ( item ) ) . forEach (
_ = > this . checkboxes . push ( < CheckEntity > { entity : _ , checked : false } )
) ;
}
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 applyStatusFilter() {
this . checkboxes = [ ] ;
this . entities . filter ( item = > this . filterEntitiesByStatus ( item ) ) . forEach (
_ = > this . checkboxes . push ( < CheckEntity > { entity : _ , checked : false } )
) ;
}
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 ) {
this . myForm = this . _fb . group ( {
pid : [ '' , Validators . required ] ,
name : [ '' , Validators . required ] ,
isEnabled : '' ,
_id : ''
} ) ;
} else {
this . updateErrorMessage = 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
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 {
this . alertModalRelatedPages . cancelButton = true ;
this . alertModalRelatedPages . okButton = true ;
this . alertModalRelatedPages . alertTitle = 'Warning' ;
this . alertModalRelatedPages . 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 . alertModalRelatedPages . okButtonText = 'Yes' ;
this . alertModalRelatedPages . open ( ) ;
}
}
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 )
) ;
}
}
}