2019-12-18 16:14:21 +01:00
|
|
|
import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
|
|
|
|
import {ActivatedRoute, Router} from "@angular/router";
|
|
|
|
import {HelpContentService} from "../../services/help-content.service";
|
2022-09-23 12:25:08 +02:00
|
|
|
import {UntypedFormArray, UntypedFormBuilder, UntypedFormGroup, Validators} from "@angular/forms";
|
2019-12-18 16:14:21 +01:00
|
|
|
import {CheckDivId, DivId} from "../../utils/entities/adminTool/divId";
|
|
|
|
import {Page} from "../../utils/entities/adminTool/page";
|
|
|
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
|
|
|
import {HelperFunctions} from "../../utils/HelperFunctions.class";
|
2021-01-28 17:33:16 +01:00
|
|
|
import {Subscriber} from "rxjs";
|
2021-01-11 10:21:42 +01:00
|
|
|
import {properties} from "../../../../environments/environment";
|
|
|
|
import {PortalUtils} from "../portal/portalHelper";
|
2021-01-28 17:33:16 +01:00
|
|
|
import {AlertModal} from "../../utils/modal/alert";
|
|
|
|
import {Option} from "../../sharedComponents/input/input.component";
|
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({
|
|
|
|
selector: 'divIds',
|
|
|
|
templateUrl: './divIds.component.html',
|
|
|
|
})
|
|
|
|
export class DivIdsComponent implements OnInit {
|
2021-01-28 17:33:16 +01:00
|
|
|
@ViewChild('editModal') editModal: AlertModal;
|
|
|
|
@ViewChild('deleteModal') deleteModal: AlertModal;
|
2019-12-18 16:14:21 +01:00
|
|
|
private selectedDivIds: string[] = [];
|
|
|
|
public checkboxes: CheckDivId[] = [];
|
|
|
|
public divIds: DivId[] = [];
|
2022-09-23 12:25:08 +02:00
|
|
|
public classForm: UntypedFormGroup;
|
|
|
|
public pagesCtrl: UntypedFormArray;
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
private searchText: RegExp = new RegExp('');
|
|
|
|
public keyword: string = "";
|
2021-01-28 17:33:16 +01:00
|
|
|
public properties: EnvProperties = properties;
|
2019-12-18 16:14:21 +01:00
|
|
|
public formPages: Page[] = [];
|
|
|
|
public showLoading: boolean = true;
|
2022-09-23 12:25:08 +02:00
|
|
|
public filterForm: UntypedFormGroup;
|
2019-12-18 16:14:21 +01:00
|
|
|
private subscriptions: any[] = [];
|
2021-01-28 17:33:16 +01:00
|
|
|
public allPages: Option[] = [];
|
2019-12-18 16:14:21 +01:00
|
|
|
selectedCommunityPid = null;
|
2021-03-19 17:51:02 +01:00
|
|
|
public portalUtils: PortalUtils = new PortalUtils();
|
2021-01-28 17:33:16 +01:00
|
|
|
private index: number;
|
|
|
|
|
2021-03-19 17:51:02 +01:00
|
|
|
constructor(private element: ElementRef, private route: ActivatedRoute, private _router: Router,
|
|
|
|
private title: Title,
|
2022-09-23 12:25:08 +02:00
|
|
|
private _helpContentService: HelpContentService, private _fb: UntypedFormBuilder,
|
2022-07-12 15:50:56 +02:00
|
|
|
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 | Classes');
|
|
|
|
this.filterForm = this._fb.group({
|
2021-01-11 10:21:42 +01:00
|
|
|
keyword: [''],
|
2021-03-19 17:51:02 +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
|
|
|
}));
|
|
|
|
this.getDivIds();
|
|
|
|
this.subscriptions.push(this.route.queryParams.subscribe(params => {
|
|
|
|
HelperFunctions.scroll();
|
|
|
|
this.selectedCommunityPid = params['communityId'];
|
|
|
|
this.getPages();
|
|
|
|
}));
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +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-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
getDivIds() {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.showLoading = true;
|
|
|
|
this.subscriptions.push(this._helpContentService.getAllDivIdsFull(this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
divIds => {
|
|
|
|
this.divIds = divIds;
|
|
|
|
this.checkboxes = [];
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
divIds.forEach(_ => {
|
|
|
|
self.checkboxes.push(<CheckDivId>{divId: _, checked: false});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving classes', error)));
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
// public showModal():void {
|
|
|
|
// this.modal.showModal();
|
|
|
|
// }
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public toggleCheckBoxes(event) {
|
|
|
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public applyCheck(flag: boolean) {
|
|
|
|
this.checkboxes.forEach(_ => _.checked = flag);
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public getSelectedDivIds(): string[] {
|
|
|
|
return this.checkboxes.filter(divId => divId.checked == true).map(checkedDivId => checkedDivId.divId).map(res => res._id);
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
private deleteDivIdsFromArray(ids: string[]): void {
|
|
|
|
for (let id of ids) {
|
2021-01-28 17:33:16 +01:00
|
|
|
let i = this.divIds.findIndex(_ => _._id == id);
|
|
|
|
this.divIds.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-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public confirmDeleteDivId(id: string) {
|
|
|
|
this.selectedDivIds = [id];
|
|
|
|
this.confirmModalOpen();
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public confirmDeleteSelectedDivIds() {
|
|
|
|
this.selectedDivIds = this.getSelectedDivIds();
|
|
|
|
this.confirmModalOpen();
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
private confirmModalOpen() {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.deleteModal.alertTitle = "Delete Confirmation";
|
|
|
|
this.deleteModal.message = "Are you sure you want to delete the selected class(es)?";
|
|
|
|
this.deleteModal.okButtonText = "Yes";
|
|
|
|
this.deleteModal.open();
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public confirmedDeleteDivIds(data: any) {
|
2021-03-19 17:51:02 +01:00
|
|
|
this.showLoading = true;
|
|
|
|
this.subscriptions.push(this._helpContentService.deleteDivIds(this.selectedDivIds, this.properties.adminToolsAPIURL).subscribe(
|
|
|
|
_ => {
|
|
|
|
this.deleteDivIdsFromArray(this.selectedDivIds);
|
2022-07-13 19:25:19 +02:00
|
|
|
NotificationHandler.rise('Classes have been <b>successfully deleted</b>');
|
2021-03-19 17:51:02 +01:00
|
|
|
this.showLoading = false;
|
2024-04-03 09:26:29 +02:00
|
|
|
//this._clearCacheService.clearCache("Class ids deleted");
|
2021-03-19 17:51:02 +01:00
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error deleting the selected classes', error)
|
|
|
|
));
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public editDivId(i: number) {
|
|
|
|
let divId: DivId = this.checkboxes[i].divId;
|
2021-01-28 17:33:16 +01:00
|
|
|
this.index = this.divIds.findIndex(value => value._id === divId._id);
|
2019-12-18 16:14:21 +01:00
|
|
|
this.formPages = <Page[]>divId.pages;
|
2021-01-28 17:33:16 +01:00
|
|
|
this.pagesCtrl = this._fb.array([], Validators.required);
|
|
|
|
this.classForm = this._fb.group({
|
|
|
|
_id: this._fb.control(divId._id),
|
|
|
|
name: this._fb.control(divId.name, Validators.required),
|
2019-12-18 16:14:21 +01:00
|
|
|
pages: this.pagesCtrl,
|
2021-01-28 17:33:16 +01:00
|
|
|
portalType: this._fb.control(divId.portalType, Validators.required)
|
2019-12-18 16:14:21 +01:00
|
|
|
});
|
2021-01-28 17:33:16 +01:00
|
|
|
this.classForm.get('portalType').disable();
|
2021-03-19 17:51:02 +01:00
|
|
|
for (let i = 0; i < divId.pages.length; i++) {
|
2019-12-18 16:14:21 +01:00
|
|
|
this.pagesCtrl.push(this._fb.control(divId.pages[i]));
|
|
|
|
}
|
2021-01-28 17:33:16 +01:00
|
|
|
this.divIdsModalOpen("Edit class", "Save Changes");
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public newDivId() {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.pagesCtrl = this._fb.array([], Validators.required);
|
2021-03-19 17:51:02 +01:00
|
|
|
if (this.classForm) {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.classForm.get('portalType').enable();
|
|
|
|
}
|
|
|
|
this.classForm = this._fb.group({
|
|
|
|
_id: this._fb.control(null),
|
|
|
|
name: this._fb.control('', Validators.required),
|
2019-12-18 16:14:21 +01:00
|
|
|
pages: this.pagesCtrl,
|
2021-01-28 17:33:16 +01:00
|
|
|
portalType: this._fb.control('', Validators.required)
|
2019-12-18 16:14:21 +01:00
|
|
|
});
|
2021-01-28 17:33:16 +01:00
|
|
|
this.divIdsModalOpen("Create class", "Create");
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2021-01-28 17:33:16 +01:00
|
|
|
private divIdsModalOpen(title: string, yesBtn: string) {
|
2021-03-19 17:51:02 +01: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-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public divIdSaveConfirmed(data: any) {
|
2021-02-01 20:17:55 +01:00
|
|
|
this.showLoading = true;
|
2022-07-13 19:25:19 +02:00
|
|
|
if (!this.classForm.getRawValue()._id) {
|
|
|
|
this.subscriptions.push(this._helpContentService.saveDivId(<DivId>this.classForm.getRawValue(), this.properties.adminToolsAPIURL).subscribe(
|
2021-03-19 17:51:02 +01:00
|
|
|
divId => {
|
|
|
|
this.divIdSavedSuccessfully(divId);
|
2022-07-13 19:25:19 +02:00
|
|
|
NotificationHandler.rise('Class <b>' + divId.name + '</b> has been <b>successfully created</b>');
|
2024-04-03 09:26:29 +02:00
|
|
|
//this._clearCacheService.clearCache("Class id saved");
|
2021-03-19 17:51:02 +01:00
|
|
|
},
|
|
|
|
error => this.handleUpdateError("System error creating class", error)
|
|
|
|
));
|
2019-12-18 16:14:21 +01:00
|
|
|
} else {
|
2022-07-13 19:25:19 +02:00
|
|
|
this.subscriptions.push(this._helpContentService.updateDivId(<DivId>this.classForm.getRawValue(), this.properties.adminToolsAPIURL).subscribe(
|
2021-03-19 17:51:02 +01:00
|
|
|
divId => {
|
|
|
|
this.divIdUpdatedSuccessfully(divId);
|
2022-07-13 19:25:19 +02:00
|
|
|
NotificationHandler.rise('Class <b>' + divId.name + '</b> has been <b>successfully updated</b>');
|
2024-04-03 09:26:29 +02:00
|
|
|
//this._clearCacheService.clearCache("Class id updated");
|
2021-03-19 17:51:02 +01:00
|
|
|
},
|
|
|
|
error => this.handleUpdateError("System error updating class", error)
|
|
|
|
));
|
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public divIdSavedSuccessfully(divId: DivId) {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.divIds.push(divId);
|
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-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public divIdUpdatedSuccessfully(divId: DivId) {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.divIds[this.index] = divId;
|
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-03-19 17:51:02 +01:00
|
|
|
|
2021-01-29 11:47:37 +01:00
|
|
|
public applyFilters() {
|
2021-01-11 10:21:42 +01:00
|
|
|
this.checkboxes = [];
|
|
|
|
this.divIds.filter(item => this.filterByType(item)).forEach(
|
2021-01-29 11:47:37 +01:00
|
|
|
item => this.checkboxes.push(<CheckDivId>{divId: item, 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.filterDivIds(item.divId));
|
2021-01-11 10:21:42 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2021-01-11 10:21:42 +01:00
|
|
|
public filterByType(divId: DivId): boolean {
|
|
|
|
let type = this.filterForm.get("type").value;
|
2021-03-19 17:51:02 +01:00
|
|
|
return type == "all" || (type == divId.portalType);
|
2021-01-11 10:21:42 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
public filterDivIds(divId: DivId): boolean {
|
2022-07-13 19:25:19 +02:00
|
|
|
return this.searchText.toString() == '' || (divId.name + ' ' + divId.portalType).match(this.searchText) != null;
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2022-07-13 19:25:19 +02:00
|
|
|
handleUpdateError(message: string, error = null) {
|
|
|
|
if (error) {
|
2019-12-18 16:14:21 +01:00
|
|
|
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-03-19 17:51:02 +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;
|
|
|
|
}
|
2021-03-19 17:51:02 +01:00
|
|
|
|
2019-12-18 16:14:21 +01:00
|
|
|
getPages() {
|
|
|
|
this.showLoading = true;
|
2021-01-11 10:21:42 +01:00
|
|
|
this.subscriptions.push(this._helpContentService.getAllPages(this.properties.adminToolsAPIURL).subscribe(
|
2019-12-18 16:14:21 +01:00
|
|
|
pages => {
|
2021-01-28 17:33:16 +01:00
|
|
|
this.allPages = [];
|
|
|
|
pages.forEach(page => {
|
|
|
|
this.allPages.push({
|
2021-05-25 14:29:07 +02:00
|
|
|
label: page.name + " [" + page.portalType + "]",
|
2021-01-28 17:33:16 +01:00
|
|
|
value: page
|
|
|
|
});
|
|
|
|
});
|
2019-12-18 16:14:21 +01:00
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving pages', error)
|
2021-01-11 10:21:42 +01:00
|
|
|
));
|
2019-12-18 16:14:21 +01:00
|
|
|
}
|
|
|
|
}
|