308 lines
9.9 KiB
TypeScript
308 lines
9.9 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, Validators} from '@angular/forms';
|
|
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";
|
|
import {CheckPortal, Portal} from "../../utils/entities/adminTool/portal";
|
|
import {PortalUtils} from "./portalHelper";
|
|
import {properties} from "../../../../environments/environment";
|
|
import {CheckPage, Page} from "../../utils/entities/adminTool/page";
|
|
import {AlertModal} from "../../utils/modal/alert";
|
|
|
|
@Component({
|
|
selector: 'portals',
|
|
templateUrl: './portals.component.html',
|
|
})
|
|
|
|
export class PortalsComponent implements OnInit {
|
|
|
|
@ViewChild('portalModal') portalModal: AlertModal;
|
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
|
private selectedPortals: string[] = [];
|
|
|
|
public checkboxes: CheckPortal[] = [];
|
|
public portals: Portal[] = [];
|
|
|
|
public portalFG: FormGroup;
|
|
public filterForm: FormGroup;
|
|
private subscriptions: any[] = [];
|
|
|
|
private searchText: RegExp = new RegExp('');
|
|
public keyword = '';
|
|
|
|
public properties: EnvProperties = null;
|
|
|
|
public showLoading = true;
|
|
public errorMessage = '';
|
|
public updateErrorMessage = '';
|
|
public modalErrorMessage = '';
|
|
public portalUtils: PortalUtils = new PortalUtils();
|
|
|
|
ngOnInit() {
|
|
this.portalFG = this._fb.group({
|
|
name: this._fb.control('', Validators.required),
|
|
_id: this._fb.control(''),
|
|
pid: this._fb.control('', Validators.required),
|
|
piwik: this._fb.control(''),
|
|
type: this._fb.control('', Validators.required),
|
|
});
|
|
this.filterForm = this._fb.group({
|
|
keyword: [''],
|
|
type: ['all', Validators.required]
|
|
});
|
|
this.subscriptions.push(this.filterForm.get('keyword').valueChanges.subscribe(value => {
|
|
this.filterBySearch(value);
|
|
}));
|
|
this.subscriptions.push(this.filterForm.get('type').valueChanges.subscribe(value => {
|
|
this.applyTypeFilter();
|
|
}));
|
|
|
|
HelperFunctions.scroll();
|
|
this.properties = properties;
|
|
this.getPortals();
|
|
|
|
}
|
|
|
|
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();
|
|
}
|
|
});
|
|
}
|
|
|
|
getPortals() {
|
|
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.subscriptions.push(this._helpContentService.getPortalsFull(this.properties.adminToolsAPIURL).subscribe(
|
|
portals => {
|
|
this.portals = portals;
|
|
if (portals) {
|
|
portals.forEach(_ => {
|
|
this.checkboxes.push(<CheckPortal>{portal: _, checked: false});
|
|
});
|
|
}
|
|
this.showLoading = false;
|
|
},
|
|
error => this.handleError('System error retrieving portals', error)));
|
|
}
|
|
}
|
|
|
|
public toggleCheckBoxes(event) {
|
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
|
}
|
|
|
|
public applyCheck(flag: boolean) {
|
|
this.checkboxes.forEach(_ => _.checked = flag);
|
|
}
|
|
|
|
public getSelectedPortals(): string[] {
|
|
return this.checkboxes.filter(portal => portal.checked === true).map(checkedPortal => checkedPortal.portal).map(res => res._id);
|
|
}
|
|
|
|
private deletePortalsFromArray(ids: string[]): void {
|
|
for (let id of ids) {
|
|
let i = this.checkboxes.findIndex(_ => _.portal._id === id);
|
|
this.checkboxes.splice(i, 1);
|
|
}
|
|
}
|
|
|
|
public confirmDeletePortal(id: string) {
|
|
// this.deleteConfirmationModal.ids = [id];
|
|
// this.deleteConfirmationModal.showModal();
|
|
this.selectedPortals = [id];
|
|
this.confirmModalOpen();
|
|
}
|
|
|
|
public confirmDeleteSelectedPortals() {
|
|
this.selectedPortals = this.getSelectedPortals();
|
|
this.confirmModalOpen();
|
|
}
|
|
|
|
private confirmModalOpen() {
|
|
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 portal(-ies)?';
|
|
this.deleteModal.okButtonText = 'Yes';
|
|
this.deleteModal.open();
|
|
}
|
|
|
|
public confirmedDeletePortals(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.subscriptions.push(this._helpContentService.deleteCommunities(this.selectedPortals, this.properties.adminToolsAPIURL).subscribe(
|
|
_ => {
|
|
this.deletePortalsFromArray(this.selectedPortals);
|
|
this.showLoading = false;
|
|
},
|
|
error => this.handleUpdateError('System error deleting the selected communities', error)
|
|
));
|
|
}
|
|
}
|
|
|
|
public editPortal(i: number) {
|
|
const portal: Portal = this.checkboxes[i].portal;
|
|
this.portalFG = this._fb.group({
|
|
name: this._fb.control(portal.name, Validators.required),
|
|
_id: this._fb.control(portal._id),
|
|
pid: this._fb.control(portal.pid, Validators.required),
|
|
piwik: this._fb.control(portal.piwik),
|
|
type: this._fb.control(portal.type, Validators.required),
|
|
});
|
|
this.portalFG.controls['type'].disable();
|
|
this.modalErrorMessage = '';
|
|
this.portalModalOpen('Update Portal', 'Update');
|
|
}
|
|
|
|
public newPortal() {
|
|
this.portalFG.controls['type'].enable();
|
|
this.portalFG = this._fb.group({
|
|
name: this._fb.control('', Validators.required),
|
|
_id: this._fb.control(''),
|
|
pid: this._fb.control('', Validators.required),
|
|
piwik: this._fb.control(''),
|
|
type: this._fb.control('', Validators.required),
|
|
});
|
|
this.modalErrorMessage = '';
|
|
this.portalModalOpen('Create Portal', 'Save');
|
|
}
|
|
|
|
private portalModalOpen(title: string, yesBtn: string) {
|
|
this.portalModal.okButtonLeft = false;
|
|
this.portalModal.cancelButton = true;
|
|
this.portalModal.okButton = true;
|
|
this.portalModal.alertTitle = title;
|
|
this.portalModal.okButtonText = yesBtn;
|
|
this.portalModal.open();
|
|
}
|
|
|
|
public portalSaveConfirmed(data: any) {
|
|
this.modalErrorMessage = '';
|
|
if (this.portalFG.getRawValue()['_id'].length > 0) {
|
|
this.portalFG.controls['type'].enable();
|
|
this.subscriptions.push(this._helpContentService.updateCommunity(<Portal>this.portalFG.value,
|
|
this.properties.adminToolsAPIURL).subscribe(
|
|
portal => {
|
|
this.portalUpdatedSuccessfully(portal);
|
|
},
|
|
error => this.handleUpdateError('System error updating portal', error)
|
|
));
|
|
} else {
|
|
this.subscriptions.push(this._helpContentService.saveCommunity(<Portal>this.portalFG.value,
|
|
this.properties.adminToolsAPIURL).subscribe(
|
|
portal => {
|
|
this.portalSavedSuccessfully(portal);
|
|
},
|
|
error => this.handleUpdateError('System error creating portal', error)
|
|
));
|
|
}
|
|
}
|
|
|
|
public portalUpdateConfirmed(data: any) {
|
|
if (!Session.isLoggedIn()) {
|
|
this._router.navigate(['/user-info'], {
|
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
|
});
|
|
} else {
|
|
this.portalFG.controls['type'].enable();
|
|
this.subscriptions.push(this._helpContentService.updateCommunity(<Portal>this.portalFG.value,
|
|
this.properties.adminToolsAPIURL).subscribe(
|
|
portal => {
|
|
this.portalUpdatedSuccessfully(portal);
|
|
},
|
|
error => this.handleUpdateError('System error updating portal', error)
|
|
));
|
|
}
|
|
}
|
|
|
|
public portalSavedSuccessfully(portal: Portal) {
|
|
this.checkboxes.push(<CheckPortal>{portal: portal, checked: false});
|
|
this.applyCheck(false);
|
|
}
|
|
|
|
public portalUpdatedSuccessfully(portal: Portal) {
|
|
this.checkboxes.find(checkItem => checkItem.portal._id === portal._id).portal = portal;
|
|
this.applyCheck(false);
|
|
}
|
|
|
|
public filterBySearch(text: string) {
|
|
this.searchText = new RegExp(text, 'i');
|
|
this.applyFilter();
|
|
}
|
|
|
|
public applyFilter() {
|
|
this.checkboxes = [];
|
|
this.portals.filter(item => this.filterPortals(item)).forEach(
|
|
_ => this.checkboxes.push(<CheckPortal>{portal: _, checked: false})
|
|
);
|
|
}
|
|
|
|
public applyTypeFilter() {
|
|
this.checkboxes = [];
|
|
this.portals.filter(item => this.filterByType(item)).forEach(
|
|
_ => this.checkboxes.push(<CheckPortal>{portal: _, checked: false})
|
|
);
|
|
}
|
|
|
|
public filterByType(portal: Portal): boolean {
|
|
let type = this.filterForm.get("type").value;
|
|
return type == "all" || (type == portal.type);
|
|
}
|
|
|
|
public filterPortals(portal: Portal): boolean {
|
|
const textFlag = this.searchText.toString() === '' || (portal.name || portal.type).match(this.searchText) != null;
|
|
return textFlag;
|
|
}
|
|
|
|
handleUpdateError(message: string, error) {
|
|
if (error == null) {
|
|
this.portalFG = this._fb.group({
|
|
name: '',
|
|
_id: '',
|
|
pid: '',
|
|
piwik: '',
|
|
type: ''
|
|
});
|
|
} 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;
|
|
}
|
|
}
|