293 lines
9.1 KiB
TypeScript
293 lines
9.1 KiB
TypeScript
|
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} from '@angular/forms';
|
||
|
import {CheckCommunity, Community} from '../../utils/entities/adminTool/community';
|
||
|
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 {Subscriber} from "rxjs";
|
||
|
|
||
|
@Component({
|
||
|
selector: 'communities',
|
||
|
templateUrl: './communities.component.html',
|
||
|
})
|
||
|
|
||
|
export class CommunitiesComponent implements OnInit {
|
||
|
|
||
|
@ViewChild('AlertModalSaveCommunity') alertModalSaveCommunity;
|
||
|
@ViewChild('AlertModalDeleteCommunities') alertModalDeleteCommunities;
|
||
|
private selectedCommunities: string[] = [];
|
||
|
|
||
|
public checkboxes: CheckCommunity[] = [];
|
||
|
public communities: Community[] = [];
|
||
|
|
||
|
public portalFG: FormGroup;
|
||
|
public formControl: FormControl;
|
||
|
private subscriptions: any[] = [];
|
||
|
|
||
|
private searchText: RegExp = new RegExp('');
|
||
|
public keyword = '';
|
||
|
|
||
|
public properties: EnvProperties = null;
|
||
|
|
||
|
public showLoading = true;
|
||
|
public errorMessage = '';
|
||
|
public updateErrorMessage = '';
|
||
|
public modalErrorMessage = '';
|
||
|
|
||
|
ngOnInit() {
|
||
|
this.portalFG = this._fb.group({
|
||
|
name: '',
|
||
|
_id: '',
|
||
|
pid: ''
|
||
|
});
|
||
|
this.formControl = this._fb.control('');
|
||
|
this.subscriptions.push(this.formControl.valueChanges.subscribe(value => {
|
||
|
this.filterBySearch(value);
|
||
|
}));
|
||
|
|
||
|
this.route.data
|
||
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
||
|
HelperFunctions.scroll();
|
||
|
|
||
|
this.properties = data.envSpecific;
|
||
|
this.getCommunities();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
constructor(private element: ElementRef, private route: ActivatedRoute,
|
||
|
private _router: Router, private _helpContentService: HelpContentService, private _fb: FormBuilder) {
|
||
|
}
|
||
|
|
||
|
|
||
|
ngOnDestroy(): void {
|
||
|
this.subscriptions.forEach(value => {
|
||
|
if (value instanceof Subscriber) {
|
||
|
value.unsubscribe();
|
||
|
} else if (value instanceof Function) {
|
||
|
value();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
getCommunities() {
|
||
|
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 = '';
|
||
|
|
||
|
|
||
|
this._helpContentService.getCommunitiesFull(this.properties.adminToolsAPIURL).subscribe(
|
||
|
communities => {
|
||
|
this.communities = communities;
|
||
|
communities.forEach(_ => {
|
||
|
this.checkboxes.push(<CheckCommunity>{community: _, checked: false});
|
||
|
});
|
||
|
this.showLoading = false;
|
||
|
},
|
||
|
error => this.handleError('System error retrieving communities', error));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public toggleCheckBoxes(event) {
|
||
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
||
|
}
|
||
|
|
||
|
public applyCheck(flag: boolean) {
|
||
|
this.checkboxes.forEach(_ => _.checked = flag);
|
||
|
}
|
||
|
|
||
|
public getSelectedCommunities(): string[] {
|
||
|
return this.checkboxes.filter(community => community.checked === true).map(checkedCommunity => checkedCommunity.community).map(res => res._id);
|
||
|
}
|
||
|
|
||
|
private deleteCommunitiesFromArray(ids: string[]): void {
|
||
|
for (let id of ids) {
|
||
|
let i = this.checkboxes.findIndex(_ => _.community._id === id);
|
||
|
this.checkboxes.splice(i, 1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public confirmDeleteCommunity(id: string) {
|
||
|
// this.deleteConfirmationModal.ids = [id];
|
||
|
// this.deleteConfirmationModal.showModal();
|
||
|
this.selectedCommunities = [id];
|
||
|
this.confirmModalOpen();
|
||
|
}
|
||
|
|
||
|
public confirmDeleteSelectedCommunities() {
|
||
|
this.selectedCommunities = this.getSelectedCommunities();
|
||
|
this.confirmModalOpen();
|
||
|
}
|
||
|
|
||
|
private confirmModalOpen() {
|
||
|
if (!Session.isLoggedIn()) {
|
||
|
this._router.navigate(['/user-info'], {
|
||
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
||
|
});
|
||
|
} else {
|
||
|
this.alertModalDeleteCommunities.cancelButton = true;
|
||
|
this.alertModalDeleteCommunities.okButton = true;
|
||
|
this.alertModalDeleteCommunities.alertTitle = 'Delete Confirmation';
|
||
|
this.alertModalDeleteCommunities.message = 'Are you sure you want to delete the selected portal(-ies)?';
|
||
|
this.alertModalDeleteCommunities.okButtonText = 'Yes';
|
||
|
this.alertModalDeleteCommunities.open();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public confirmedDeleteCommunities(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 = '';
|
||
|
|
||
|
this._helpContentService.deleteCommunities(this.selectedCommunities, this.properties.adminToolsAPIURL).subscribe(
|
||
|
_ => {
|
||
|
this.deleteCommunitiesFromArray(this.selectedCommunities);
|
||
|
this.showLoading = false;
|
||
|
},
|
||
|
error => this.handleUpdateError('System error deleting the selected communities', error)
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public editCommunity(i: number) {
|
||
|
const community: Community = this.checkboxes[i].community;
|
||
|
this.portalFG = this._fb.group({
|
||
|
name: community.name,
|
||
|
_id: community._id,
|
||
|
pid: community.pid
|
||
|
});
|
||
|
this.modalErrorMessage = '';
|
||
|
this.communitiesModalOpen(this.alertModalSaveCommunity, 'Update', 'Update Community');
|
||
|
}
|
||
|
|
||
|
public newCommunity() {
|
||
|
this.portalFG = this._fb.group({
|
||
|
name: '',
|
||
|
_id: '',
|
||
|
pid: ''
|
||
|
});
|
||
|
this.modalErrorMessage = '';
|
||
|
this.communitiesModalOpen(this.alertModalSaveCommunity, '', 'Save');
|
||
|
}
|
||
|
|
||
|
private communitiesModalOpen(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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public communitySaveConfirmed(data: any) {
|
||
|
if (!Session.isLoggedIn()) {
|
||
|
this._router.navigate(['/user-info'], {
|
||
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
||
|
});
|
||
|
} else {
|
||
|
|
||
|
this.modalErrorMessage = '';
|
||
|
if (this.portalFG.getRawValue()['_id'].length > 0) {
|
||
|
this._helpContentService.updateCommunity(<Community>this.portalFG.value,
|
||
|
this.properties.adminToolsAPIURL).subscribe(
|
||
|
community => {
|
||
|
this.communityUpdatedSuccessfully(community);
|
||
|
},
|
||
|
error => this.handleUpdateError('System error updating portal', error)
|
||
|
);
|
||
|
}else{
|
||
|
this._helpContentService.saveCommunity(<Community>this.portalFG.value,
|
||
|
this.properties.adminToolsAPIURL).subscribe(
|
||
|
community => {
|
||
|
this.communitySavedSuccessfully(community);
|
||
|
},
|
||
|
error => this.handleUpdateError('System error creating portal', error)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public communityUpdateConfirmed(data: any) {
|
||
|
if (!Session.isLoggedIn()) {
|
||
|
this._router.navigate(['/user-info'], {
|
||
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
||
|
});
|
||
|
} else {
|
||
|
this._helpContentService.updateCommunity(<Community>this.portalFG.value,
|
||
|
this.properties.adminToolsAPIURL).subscribe(
|
||
|
community => {
|
||
|
this.communityUpdatedSuccessfully(community);
|
||
|
},
|
||
|
error => this.handleUpdateError('System error updating portal', error)
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public communitySavedSuccessfully(community: Community) {
|
||
|
this.checkboxes.push(<CheckCommunity>{community: community, checked: false});
|
||
|
this.applyCheck(false);
|
||
|
}
|
||
|
|
||
|
public communityUpdatedSuccessfully(community: Community) {
|
||
|
this.checkboxes.find(checkItem => checkItem.community._id === community._id).community = community;
|
||
|
this.applyCheck(false);
|
||
|
}
|
||
|
|
||
|
public filterBySearch(text: string) {
|
||
|
this.searchText = new RegExp(text, 'i');
|
||
|
this.applyFilter();
|
||
|
}
|
||
|
|
||
|
public applyFilter() {
|
||
|
this.checkboxes = [];
|
||
|
this.communities.filter(item => this.filterCommunities(item)).forEach(
|
||
|
_ => this.checkboxes.push(<CheckCommunity>{community: _, checked: false})
|
||
|
);
|
||
|
}
|
||
|
|
||
|
public filterCommunities(community: Community): boolean {
|
||
|
const textFlag = this.searchText.toString() === '' || (community.name).match(this.searchText) != null;
|
||
|
return textFlag;
|
||
|
}
|
||
|
|
||
|
handleUpdateError(message: string, error) {
|
||
|
if (error == null) {
|
||
|
this.portalFG = this._fb.group({
|
||
|
name: '',
|
||
|
_id: '',
|
||
|
pid: ''
|
||
|
});
|
||
|
} else {
|
||
|
this.updateErrorMessage = message;
|
||
|
console.log('Server responded: ' + error);
|
||
|
}
|
||
|
|
||
|
this.showLoading = false;
|
||
|
}
|
||
|
|
||
|
handleError(message: string, error) {
|
||
|
this.errorMessage = message;
|
||
|
console.log('Server responded: ' + error);
|
||
|
|
||
|
this.showLoading = false;
|
||
|
}
|
||
|
}
|