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 {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";
|
2020-10-30 15:01:44 +01:00
|
|
|
import {CheckPortal, Portal} from "../../utils/entities/adminTool/portal";
|
|
|
|
import {PortalUtils} from "./portalHelper";
|
2020-12-18 16:09:38 +01:00
|
|
|
import {properties} from "../../../../environments/environment";
|
2021-01-26 17:21:55 +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";
|
2021-03-19 17:51:02 +01:00
|
|
|
import {Title} from "@angular/platform-browser";
|
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({
|
2020-10-30 15:01:44 +01:00
|
|
|
selector: 'portals',
|
|
|
|
templateUrl: './portals.component.html',
|
2019-12-18 16:14:21 +01:00
|
|
|
})
|
|
|
|
|
2020-10-30 15:01:44 +01:00
|
|
|
export class PortalsComponent implements OnInit {
|
2021-01-26 17:21:55 +01:00
|
|
|
|
2021-01-28 17:33:16 +01:00
|
|
|
@ViewChild('editModal') editModal: AlertModal;
|
2021-01-26 17:21:55 +01:00
|
|
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
|
|
|
private selectedPortals: string[] = [];
|
|
|
|
|
2020-09-24 13:18:24 +02:00
|
|
|
public checkboxes: CheckPortal[] = [];
|
2021-01-26 17:21:55 +01:00
|
|
|
public portals: Portal[] = [];
|
|
|
|
|
2021-01-28 17:33:16 +01:00
|
|
|
public portalForm: FormGroup;
|
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
|
|
|
private searchText: RegExp = new RegExp('');
|
|
|
|
public keyword = '';
|
2021-01-26 17:21:55 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public properties: EnvProperties = null;
|
2021-01-26 17:21:55 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public showLoading = true;
|
2021-01-26 17:21:55 +01:00
|
|
|
public portalUtils: PortalUtils = new PortalUtils();
|
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
|
|
|
|
2021-03-19 17:51:02 +01:00
|
|
|
constructor(private element: ElementRef, private route: ActivatedRoute,
|
|
|
|
private title: Title,
|
|
|
|
private _router: Router, private _helpContentService: HelpContentService, private _fb: FormBuilder) {
|
|
|
|
}
|
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
ngOnInit() {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.title.setTitle('Administrator Dashboard | Portals');
|
2021-01-26 17:21:55 +01:00
|
|
|
this.filterForm = this._fb.group({
|
2021-01-11 10:21:42 +01:00
|
|
|
keyword: [''],
|
2021-01-26 17:21:55 +01:00
|
|
|
type: ['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('type').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
|
|
|
|
2020-12-18 16:09:38 +01:00
|
|
|
HelperFunctions.scroll();
|
|
|
|
this.properties = properties;
|
2021-01-26 17:21:55 +01:00
|
|
|
this.getPortals();
|
|
|
|
|
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
|
|
|
|
|
|
|
getPortals() {
|
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;
|
2020-12-18 16:09:38 +01:00
|
|
|
this.subscriptions.push(this._helpContentService.getPortalsFull(this.properties.adminToolsAPIURL).subscribe(
|
2021-01-26 17:21:55 +01:00
|
|
|
portals => {
|
|
|
|
this.portals = portals;
|
|
|
|
if (portals) {
|
|
|
|
portals.forEach(_ => {
|
|
|
|
this.checkboxes.push(<CheckPortal>{portal: _, checked: false});
|
2020-10-30 15:01:44 +01:00
|
|
|
});
|
|
|
|
}
|
2019-12-18 16:14:21 +01:00
|
|
|
this.showLoading = false;
|
|
|
|
},
|
2021-01-11 10:21:42 +01:00
|
|
|
error => this.handleError('System error retrieving portals', 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
|
|
|
|
|
|
|
public getSelectedPortals(): string[] {
|
|
|
|
return this.checkboxes.filter(portal => portal.checked === true).map(checkedPortal => checkedPortal.portal).map(res => res._id);
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
|
|
|
private deletePortalsFromArray(ids: string[]): void {
|
2019-12-18 16:14:21 +01:00
|
|
|
for (let id of ids) {
|
2021-01-28 17:33:16 +01:00
|
|
|
let i = this.portals.findIndex(_ => _._id == id);
|
|
|
|
this.portals.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
|
|
|
|
|
|
|
public confirmDeletePortal(id: string) {
|
2019-12-18 16:14:21 +01:00
|
|
|
// this.deleteConfirmationModal.ids = [id];
|
|
|
|
// this.deleteConfirmationModal.showModal();
|
2021-01-26 17:21:55 +01:00
|
|
|
this.selectedPortals = [id];
|
2019-12-18 16:14:21 +01:00
|
|
|
this.confirmModalOpen();
|
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
|
|
|
public confirmDeleteSelectedPortals() {
|
|
|
|
this.selectedPortals = this.getSelectedPortals();
|
2019-12-18 16:14:21 +01:00
|
|
|
this.confirmModalOpen();
|
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
private confirmModalOpen() {
|
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 portal(-ies)?';
|
|
|
|
this.deleteModal.okButtonText = 'Yes';
|
|
|
|
this.deleteModal.open();
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
|
|
|
public confirmedDeletePortals(data: any) {
|
2019-12-18 16:14:21 +01:00
|
|
|
this.showLoading = true;
|
2021-01-26 17:21:55 +01:00
|
|
|
this.subscriptions.push(this._helpContentService.deleteCommunities(this.selectedPortals, this.properties.adminToolsAPIURL).subscribe(
|
2019-12-18 16:14:21 +01:00
|
|
|
_ => {
|
2021-01-26 17:21:55 +01:00
|
|
|
this.deletePortalsFromArray(this.selectedPortals);
|
2021-02-01 20:17:55 +01:00
|
|
|
UIkit.notification('Portals 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 communities', error)
|
2020-12-18 16:09:38 +01:00
|
|
|
));
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
|
|
|
public editPortal(i: number) {
|
|
|
|
const portal: Portal = this.checkboxes[i].portal;
|
2021-01-28 17:33:16 +01:00
|
|
|
this.index = this.portals.findIndex(value => value._id === portal._id);
|
|
|
|
this.portalForm = this._fb.group({
|
2021-01-26 17:21:55 +01:00
|
|
|
_id: this._fb.control(portal._id),
|
2021-01-28 17:33:16 +01:00
|
|
|
name: this._fb.control(portal.name, Validators.required),
|
2021-01-26 17:21:55 +01:00
|
|
|
pid: this._fb.control(portal.pid, Validators.required),
|
|
|
|
piwik: this._fb.control(portal.piwik),
|
2021-10-21 11:39:27 +02:00
|
|
|
twitterAccount: this._fb.control(portal.twitterAccount),
|
2021-01-26 17:21:55 +01:00
|
|
|
type: this._fb.control(portal.type, Validators.required),
|
2019-12-18 16:14:21 +01:00
|
|
|
});
|
2021-01-28 17:38:06 +01:00
|
|
|
this.portalForm.get('type').disable();
|
2021-01-28 17:33:16 +01:00
|
|
|
this.portalModalOpen('Edit Portal', 'Save');
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
|
|
|
public newPortal() {
|
2021-01-28 17:38:06 +01:00
|
|
|
if(this.portalForm) {
|
|
|
|
this.portalForm.get('type').enable();
|
|
|
|
}
|
2021-01-28 17:33:16 +01:00
|
|
|
this.portalForm = this._fb.group({
|
2021-01-26 17:21:55 +01:00
|
|
|
_id: this._fb.control(''),
|
2021-01-28 17:33:16 +01:00
|
|
|
name: this._fb.control('', Validators.required),
|
2021-01-26 17:21:55 +01:00
|
|
|
pid: this._fb.control('', Validators.required),
|
|
|
|
piwik: this._fb.control(''),
|
2021-10-21 11:39:27 +02:00
|
|
|
twitterAccount: this._fb.control(''),
|
2021-01-26 17:21:55 +01:00
|
|
|
type: this._fb.control('', Validators.required),
|
2019-12-18 16:14:21 +01:00
|
|
|
});
|
2021-01-28 17:33:16 +01:00
|
|
|
this.portalModalOpen('Create Portal', 'Create');
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
|
|
|
private portalModalOpen(title: string, yesBtn: string) {
|
2021-01-28 17:33:16 +01:00
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
|
|
|
"redirectUrl": this._router.url
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.editModal.okButtonLeft = false;
|
|
|
|
this.editModal.cancelButton = true;
|
|
|
|
this.editModal.okButton = true;
|
|
|
|
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
|
|
|
|
|
|
|
public portalSaveConfirmed(data: any) {
|
2021-02-01 20:17:55 +01:00
|
|
|
this.showLoading = true;
|
2021-01-28 17:33:16 +01:00
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
|
|
|
"redirectUrl": this._router.url
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (this.portalForm.value._id) {
|
2021-01-28 17:38:06 +01:00
|
|
|
this.portalForm.get('type').enable();
|
2021-01-28 17:33:16 +01:00
|
|
|
this.subscriptions.push(this._helpContentService.updateCommunity(<Portal>this.portalForm.value,
|
2019-12-18 16:14:21 +01:00
|
|
|
this.properties.adminToolsAPIURL).subscribe(
|
2021-01-26 17:21:55 +01:00
|
|
|
portal => {
|
|
|
|
this.portalUpdatedSuccessfully(portal);
|
2021-02-01 20:17:55 +01:00
|
|
|
UIkit.notification('Portal <b>' + portal.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 portal', error)
|
2020-12-18 16:09:38 +01:00
|
|
|
));
|
2021-01-26 17:21:55 +01:00
|
|
|
} else {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.subscriptions.push(this._helpContentService.saveCommunity(<Portal>this.portalForm.value,
|
2019-12-18 16:14:21 +01:00
|
|
|
this.properties.adminToolsAPIURL).subscribe(
|
2021-01-26 17:21:55 +01:00
|
|
|
portal => {
|
|
|
|
this.portalSavedSuccessfully(portal);
|
2021-02-01 20:17:55 +01:00
|
|
|
UIkit.notification('Portal <b>' + portal.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 portal', error)
|
2020-12-18 16:09:38 +01:00
|
|
|
));
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-01-28 17:33:16 +01:00
|
|
|
}
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
|
|
|
public portalSavedSuccessfully(portal: Portal) {
|
2021-01-29 11:47:37 +01:00
|
|
|
this.portals.push(portal);
|
|
|
|
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
|
|
|
|
|
|
|
public portalUpdatedSuccessfully(portal: Portal) {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.portals[this.index] = portal;
|
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() {
|
2021-01-11 10:21:42 +01:00
|
|
|
this.checkboxes = [];
|
2021-01-26 17:21:55 +01:00
|
|
|
this.portals.filter(item => this.filterByType(item)).forEach(
|
|
|
|
_ => this.checkboxes.push(<CheckPortal>{portal: _, checked: false})
|
2021-01-11 10:21:42 +01:00
|
|
|
);
|
2021-01-29 11:47:37 +01:00
|
|
|
this.checkboxes = this.checkboxes.filter(item => this.filterPortals(item.portal));
|
2021-01-11 10:21:42 +01:00
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
|
|
|
public filterByType(portal: Portal): boolean {
|
2021-01-11 10:21:42 +01:00
|
|
|
let type = this.filterForm.get("type").value;
|
2021-01-26 17:21:55 +01:00
|
|
|
return type == "all" || (type == portal.type);
|
2021-01-11 10:21:42 +01:00
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
|
|
|
public filterPortals(portal: Portal): boolean {
|
|
|
|
const textFlag = this.searchText.toString() === '' || (portal.name || portal.type).match(this.searchText) != null;
|
2019-12-18 16:14:21 +01:00
|
|
|
return textFlag;
|
|
|
|
}
|
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.portalForm = this._fb.group({
|
2019-12-18 16:14:21 +01:00
|
|
|
name: '',
|
|
|
|
_id: '',
|
2020-09-24 13:18:24 +02:00
|
|
|
pid: '',
|
2021-01-26 17:21:55 +01:00
|
|
|
piwik: '',
|
2020-09-24 13:18:24 +02:00
|
|
|
type: ''
|
2019-12-18 16:14:21 +01:00
|
|
|
});
|
|
|
|
} else {
|
2021-03-18 15:11:41 +01:00
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
2019-12-18 16:14:21 +01:00
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
}
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
handleError(message: string, error) {
|
2021-03-18 15:11:41 +01:00
|
|
|
UIkit.notification(message, {
|
|
|
|
status: 'danger',
|
|
|
|
timeout: 6000,
|
|
|
|
pos: 'bottom-right'
|
|
|
|
});
|
2019-12-18 16:14:21 +01:00
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
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()
|
|
|
|
}
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|