2018-10-30 16:31:16 +01:00
|
|
|
/**
|
|
|
|
* Created by stefania on 7/13/17.
|
|
|
|
*/
|
2018-12-03 12:33:03 +01:00
|
|
|
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
|
2019-03-05 15:53:50 +01:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { HelpContentService } from '../../services/help-content.service';
|
|
|
|
import { FormGroup } from '@angular/forms';
|
|
|
|
import { CommunityFormComponent } from './community-form.component';
|
|
|
|
import { CheckCommunity, Community } from '../../domain/community';
|
2018-10-30 16:31:16 +01:00
|
|
|
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
|
|
|
|
|
2018-11-13 16:26:49 +01:00
|
|
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
|
|
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
2019-04-24 13:44:46 +02:00
|
|
|
import {HelperFunctions} from "../../openaireLibrary/utils/HelperFunctions.class";
|
2018-11-13 16:26:49 +01:00
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'communities',
|
|
|
|
templateUrl: './communities.component.html',
|
|
|
|
})
|
|
|
|
|
|
|
|
export class CommunitiesComponent implements OnInit {
|
|
|
|
|
|
|
|
// @ViewChild(ModalFormComponent)
|
|
|
|
// @ViewChild('saveModal')
|
|
|
|
// public modal:ModalFormComponent;
|
|
|
|
//
|
|
|
|
// @ViewChild('updateModal')
|
|
|
|
// public updateModal:ModalFormComponent;
|
|
|
|
|
|
|
|
// @ViewChild('deleteConfirmationModal')
|
|
|
|
// public deleteConfirmationModal : DeleteConfirmationDialogComponent;
|
|
|
|
@ViewChild('AlertModalSaveCommunity') alertModalSaveCommunity;
|
|
|
|
@ViewChild('AlertModalUpdateCommunity') alertModalUpdateCommunity;
|
|
|
|
@ViewChild('AlertModalDeleteCommunities') alertModalDeleteCommunities;
|
|
|
|
private selectedCommunities: string[] = [];
|
|
|
|
|
|
|
|
@ViewChild(CommunityFormComponent)
|
2019-03-05 15:53:50 +01:00
|
|
|
public formComponent: CommunityFormComponent;
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public checkboxes: CheckCommunity[] = [];
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public communities: Community[] = [];
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
// public errorMessage: string;
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public formGroup: FormGroup;
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
private searchText: RegExp = new RegExp('');
|
|
|
|
public keyword = '';
|
2018-12-03 12:33:03 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public properties: EnvProperties = null;
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public showLoading = true;
|
|
|
|
public errorMessage = '';
|
|
|
|
public updateErrorMessage = '';
|
|
|
|
public modalErrorMessage = '';
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.formGroup = this.formComponent.form;
|
|
|
|
|
|
|
|
this.route.data
|
|
|
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
2019-04-24 13:44:46 +02:00
|
|
|
HelperFunctions.scroll();
|
2019-02-14 13:20:00 +01:00
|
|
|
|
2019-04-24 13:44:46 +02:00
|
|
|
this.properties = data.envSpecific;
|
2018-10-30 16:31:16 +01:00
|
|
|
this.getCommunities();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
constructor(private element: ElementRef, private route: ActivatedRoute,
|
|
|
|
private _router: Router, private _helpContentService: HelpContentService) {}
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
getCommunities() {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
2018-11-13 16:26:49 +01:00
|
|
|
} else {
|
2018-10-30 16:31:16 +01:00
|
|
|
this.showLoading = true;
|
2019-03-05 15:53:50 +01:00
|
|
|
this.updateErrorMessage = '';
|
|
|
|
this.errorMessage = '';
|
2018-10-30 16:31:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
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));
|
2018-11-13 16:26:49 +01:00
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// public showModal():void {
|
|
|
|
// this.modal.showModal();
|
|
|
|
// }
|
|
|
|
|
|
|
|
public toggleCheckBoxes(event) {
|
|
|
|
this.checkboxes.forEach(_ => _.checked = event.target.checked);
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public applyCheck(flag: boolean) {
|
2018-10-30 16:31:16 +01:00
|
|
|
this.checkboxes.forEach(_ => _.checked = flag);
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public getSelectedCommunities(): string[] {
|
|
|
|
return this.checkboxes.filter(community => community.checked === true).
|
|
|
|
map(checkedCommunity => checkedCommunity.community).map(res => res._id);
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
private deleteCommunitiesFromArray(ids: string[]): void {
|
|
|
|
for (let id of ids) {
|
|
|
|
let i = this.checkboxes.findIndex(_ => _.community._id === id);
|
2018-10-30 16:31:16 +01:00
|
|
|
this.checkboxes.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public confirmDeleteCommunity(id: string) {
|
|
|
|
// this.deleteConfirmationModal.ids = [id];
|
|
|
|
// this.deleteConfirmationModal.showModal();
|
2018-10-30 16:31:16 +01:00
|
|
|
this.selectedCommunities = [id];
|
|
|
|
this.confirmModalOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
public confirmDeleteSelectedCommunities() {
|
2019-03-05 15:53:50 +01:00
|
|
|
// this.deleteConfirmationModal.ids = this.getSelectedCommunities();
|
|
|
|
// this.deleteConfirmationModal.showModal();
|
2018-10-30 16:31:16 +01:00
|
|
|
this.selectedCommunities = this.getSelectedCommunities();
|
|
|
|
this.confirmModalOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
private confirmModalOpen() {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
2018-11-13 16:26:49 +01:00
|
|
|
} else {
|
|
|
|
this.alertModalDeleteCommunities.cancelButton = true;
|
|
|
|
this.alertModalDeleteCommunities.okButton = true;
|
2019-03-05 15:53:50 +01:00
|
|
|
this.alertModalDeleteCommunities.alertTitle = 'Delete Confirmation';
|
|
|
|
this.alertModalDeleteCommunities.message = 'Are you sure you want to delete the selected community(-ies)?';
|
|
|
|
this.alertModalDeleteCommunities.okButtonText = 'Yes';
|
2018-11-13 16:26:49 +01:00
|
|
|
this.alertModalDeleteCommunities.open();
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public confirmedDeleteCommunities(data: any) {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
2018-11-13 16:26:49 +01:00
|
|
|
} else {
|
2018-10-30 16:31:16 +01:00
|
|
|
this.showLoading = true;
|
2019-03-05 15:53:50 +01:00
|
|
|
this.updateErrorMessage = '';
|
2018-10-30 16:31:16 +01:00
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
this._helpContentService.deleteCommunities(this.selectedCommunities, this.properties.adminToolsAPIURL).
|
|
|
|
subscribe(
|
2018-10-30 16:31:16 +01:00
|
|
|
_ => {
|
|
|
|
this.deleteCommunitiesFromArray(this.selectedCommunities);
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error deleting the selected communities', error)
|
|
|
|
);
|
2018-11-13 16:26:49 +01:00
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public editCommunity(i: number) {
|
|
|
|
const community: Community = this.checkboxes[i].community;
|
2018-10-30 16:31:16 +01:00
|
|
|
this.formGroup.patchValue(community);
|
2019-03-05 15:53:50 +01:00
|
|
|
// this.updateModal.showModal();
|
|
|
|
this.modalErrorMessage = '';
|
|
|
|
this.communitiesModalOpen(this.alertModalUpdateCommunity, 'Update', 'Update Community');
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public newCommunity() {
|
|
|
|
this.formComponent.reset();
|
2019-03-05 15:53:50 +01:00
|
|
|
this.modalErrorMessage = '';
|
|
|
|
this.communitiesModalOpen(this.alertModalSaveCommunity, 'Save', 'Add a new Community');
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private communitiesModalOpen(modal: any, title: string, yesBtn: string) {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
2018-11-13 16:26:49 +01:00
|
|
|
} else {
|
|
|
|
modal.cancelButton = true;
|
|
|
|
modal.okButton = true;
|
|
|
|
modal.alertTitle = title;
|
|
|
|
modal.okButtonText = yesBtn;
|
|
|
|
modal.open();
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public communitySaveConfirmed(data: any) {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
2018-10-30 16:31:16 +01:00
|
|
|
} else {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (!this.formGroup.valid) {
|
|
|
|
this.communitiesModalOpen(this.alertModalSaveCommunity, 'Save', 'Add a new Community');
|
|
|
|
this.modalErrorMessage = 'Please fill in all required fields marked with *';
|
2018-11-13 16:26:49 +01:00
|
|
|
} else {
|
2019-03-05 15:53:50 +01:00
|
|
|
this.modalErrorMessage = '';
|
|
|
|
this._helpContentService.saveCommunity(<Community> this.formGroup.value,
|
|
|
|
this.properties.adminToolsAPIURL).subscribe(
|
2018-11-13 16:26:49 +01:00
|
|
|
community => {
|
|
|
|
this.communitySavedSuccessfully(community);
|
|
|
|
},
|
2019-03-05 15:53:50 +01:00
|
|
|
error => this.handleUpdateError('System error creating community', error)
|
2018-11-13 16:26:49 +01:00
|
|
|
);
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public communityUpdateConfirmed(data: any) {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (!Session.isLoggedIn()) {
|
|
|
|
this._router.navigate(['/user-info'], {
|
|
|
|
queryParams: { 'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url} });
|
2018-10-30 16:31:16 +01:00
|
|
|
} else {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (!this.formGroup.valid) {
|
|
|
|
this.communitiesModalOpen(this.alertModalUpdateCommunity, 'Update', 'Update Community');
|
|
|
|
this.modalErrorMessage = 'Please fill in all required fields marked with *';
|
2018-11-13 16:26:49 +01:00
|
|
|
} else {
|
2019-03-05 15:53:50 +01:00
|
|
|
this._helpContentService.updateCommunity(<Community> this.formGroup.value,
|
|
|
|
this.properties.adminToolsAPIURL).subscribe(
|
2018-11-13 16:26:49 +01:00
|
|
|
community => {
|
|
|
|
this.communityUpdatedSuccessfully(community);
|
|
|
|
},
|
2019-03-05 15:53:50 +01:00
|
|
|
error => this.handleUpdateError('System error updating community', error)
|
2018-11-13 16:26:49 +01:00
|
|
|
);
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public communitySavedSuccessfully(community: Community) {
|
|
|
|
this.checkboxes.push(<CheckCommunity>{community : community, checked : false});
|
|
|
|
this.applyCheck(false);
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public communityUpdatedSuccessfully(community: Community) {
|
|
|
|
this.checkboxes.find(checkItem => checkItem.community._id === community._id).community = community;
|
2018-10-30 16:31:16 +01:00
|
|
|
this.applyCheck(false);
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public filterBySearch(text: string) {
|
|
|
|
this.searchText = new RegExp(text, 'i');
|
2018-10-30 16:31:16 +01:00
|
|
|
this.applyFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
public applyFilter() {
|
|
|
|
this.checkboxes = [];
|
|
|
|
this.communities.filter(item => this.filterCommunities(item)).forEach(
|
|
|
|
_ => this.checkboxes.push(<CheckCommunity>{community: _, checked: false})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:53:50 +01:00
|
|
|
public filterCommunities(community: Community): boolean {
|
|
|
|
const textFlag = this.searchText.toString() === '' || (community.name).match(this.searchText) != null;
|
2018-10-30 16:31:16 +01:00
|
|
|
return textFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUpdateError(message: string, error) {
|
2019-03-05 15:53:50 +01:00
|
|
|
if (error == null) {
|
2018-10-30 16:31:16 +01:00
|
|
|
this.formComponent.reset();
|
|
|
|
} else {
|
|
|
|
this.updateErrorMessage = message;
|
2019-03-05 15:53:50 +01:00
|
|
|
console.log('Server responded: ' + error);
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(message: string, error) {
|
|
|
|
this.errorMessage = message;
|
|
|
|
console.log('Server responded: ' + error);
|
|
|
|
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
}
|