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 {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-03-19 17:51:02 +01:00
|
|
|
import {Title} from "@angular/platform-browser";
|
2022-07-12 15:50:56 +02:00
|
|
|
import {ClearCacheService} from "../../services/clear-cache.service";
|
2022-07-13 19:25:19 +02:00
|
|
|
import {NotificationHandler} from "../../utils/notification-handler";
|
2021-02-01 20:17:55 +01:00
|
|
|
|
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-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,
|
2022-07-12 15:50:56 +02:00
|
|
|
private _router: Router, private _helpContentService: HelpContentService, private _fb: FormBuilder,
|
|
|
|
private _clearCacheService: ClearCacheService) {
|
2021-03-19 17:51:02 +01:00
|
|
|
}
|
|
|
|
|
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() {
|
2022-07-08 20:57:12 +02:00
|
|
|
this.showLoading = true;
|
|
|
|
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)));
|
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.alertTitle = 'Delete Confirmation';
|
2022-07-05 00:31:56 +02:00
|
|
|
this.deleteModal.message = 'Are you sure you want to delete the selected portal(s)?';
|
2021-01-28 17:33:16 +01:00
|
|
|
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);
|
2022-07-13 19:25:19 +02:00
|
|
|
NotificationHandler.rise('Portals have been <b>successfully deleted</b>');
|
2019-12-18 16:14:21 +01:00
|
|
|
this.showLoading = false;
|
2022-07-12 15:50:56 +02:00
|
|
|
this._clearCacheService.clearCache("Portals deleted");
|
2019-12-18 16:14:21 +01:00
|
|
|
},
|
|
|
|
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) {
|
2022-07-08 20:57:12 +02:00
|
|
|
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
|
|
|
|
|
|
|
public portalSaveConfirmed(data: any) {
|
2021-02-01 20:17:55 +01:00
|
|
|
this.showLoading = true;
|
2022-07-13 19:25:19 +02:00
|
|
|
if (this.portalForm.getRawValue()._id) {
|
|
|
|
this.subscriptions.push(this._helpContentService.updateCommunity(<Portal>this.portalForm.getRawValue(),
|
2022-07-08 20:57:12 +02:00
|
|
|
this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
portal => {
|
|
|
|
this.portalUpdatedSuccessfully(portal);
|
2022-07-13 19:25:19 +02:00
|
|
|
NotificationHandler.rise('Portal <b>' + portal.name + '</b> has been <b>successfully updated</b>');
|
2022-07-12 15:50:56 +02:00
|
|
|
this._clearCacheService.clearCache("Portal updated");
|
2022-07-08 20:57:12 +02:00
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error updating portal', error)
|
|
|
|
));
|
2021-01-28 17:33:16 +01:00
|
|
|
} else {
|
2022-07-13 19:25:19 +02:00
|
|
|
this.subscriptions.push(this._helpContentService.saveCommunity(<Portal>this.portalForm.getRawValue(),
|
2022-07-08 20:57:12 +02:00
|
|
|
this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
portal => {
|
|
|
|
this.portalSavedSuccessfully(portal);
|
2022-07-13 19:25:19 +02:00
|
|
|
NotificationHandler.rise('Portal <b>' + portal.name + '</b> has been <b>successfully created</b>');
|
2022-07-12 15:50:56 +02:00
|
|
|
this._clearCacheService.clearCache("Portal saved");
|
2022-07-08 20:57:12 +02:00
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error creating portal', error)
|
|
|
|
));
|
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 {
|
2022-07-13 19:25:19 +02:00
|
|
|
return this.searchText.toString() === '' || (portal.name || portal.type).match(this.searchText) != null;
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
2022-07-13 19:25:19 +02:00
|
|
|
handleUpdateError(message: string, error = null) {
|
|
|
|
if (!error) {
|
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 {
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
}
|
2022-07-13 19:25:19 +02:00
|
|
|
NotificationHandler.rise(message, 'danger');
|
2019-12-18 16:14:21 +01:00
|
|
|
this.showLoading = false;
|
|
|
|
}
|
2021-01-26 17:21:55 +01:00
|
|
|
|
2022-07-13 19:25:19 +02:00
|
|
|
handleError(message: string, error = null) {
|
|
|
|
if(error) {
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
}
|
|
|
|
NotificationHandler.rise(message, 'danger');
|
2019-12-18 16:14:21 +01:00
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
}
|