connect-admin/src/app/pages/community/communities.component.ts

242 lines
8.4 KiB
TypeScript
Raw Normal View History

/**
* Created by stefania on 7/13/17.
*/
import { Component, ViewChild, OnInit } from '@angular/core';
import { ActivatedRoute } from "@angular/router";
import { HelpContentService } from "../../services/help-content.service";
import { FormGroup } from "@angular/forms";
import { ModalFormComponent } from "../modal-form.component";
import { DeleteConfirmationDialogComponent } from "../delete-confirmation-dialog.component";
import { CommunityFormComponent } from "./community-form.component";
import { CheckCommunity, Community } from "../../domain/community";
import { EnvProperties } from '../../openaireLibrary/utils/properties/env-properties';
@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)
public formComponent : CommunityFormComponent;
public checkboxes : CheckCommunity[] = [];
public communities : Community[] = [];
//public errorMessage: string;
public formGroup : FormGroup;
private searchText : RegExp = new RegExp('');
public properties:EnvProperties = null;
public showLoading: boolean = true;
public errorMessage: string = '';
public updateErrorMessage: string = '';
public modalErrorMessage: string = '';
ngOnInit() {
this.formGroup = this.formComponent.form;
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
this.getCommunities();
});
}
constructor(private route: ActivatedRoute, private _helpContentService: HelpContentService) {}
getCommunities() {
this.showLoading = true;
this.updateErrorMessage = "";
this.errorMessage = "";
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));
}
// public showModal():void {
// this.modal.showModal();
// }
public toggleCheckBoxes(event) {
this.checkboxes.forEach(_ => _.checked = event.target.checked);
}
public applyCheck(flag : boolean) {
console.info("applyCheck "+flag);
this.checkboxes.forEach(_ => _.checked = flag);
}
public getSelectedCommunities() : string[] {
return this.checkboxes.filter(community => community.checked == true).map(checkedCommunity => checkedCommunity.community).map(res => res._id);
}
private deleteCommunitiesFromArray(ids : string[]) : void {
for(let id of ids) {
let i = this.checkboxes.findIndex(_ => _.community._id == id);
this.checkboxes.splice(i, 1);
}
}
public confirmDeleteCommunity(id : string) {
//this.deleteConfirmationModal.ids = [id];
//this.deleteConfirmationModal.showModal();
this.selectedCommunities = [id];
this.confirmModalOpen();
}
public confirmDeleteSelectedCommunities() {
//this.deleteConfirmationModal.ids = this.getSelectedCommunities();
//this.deleteConfirmationModal.showModal();
this.selectedCommunities = this.getSelectedCommunities();
this.confirmModalOpen();
}
private confirmModalOpen() {
this.alertModalDeleteCommunities.cancelButton = true;
this.alertModalDeleteCommunities.okButton = true;
this.alertModalDeleteCommunities.alertTitle = "Delete Confirmation";
this.alertModalDeleteCommunities.message = "Are you sure you want to delete the selected community(-ies)?";
this.alertModalDeleteCommunities.okButtonText = "Yes";
this.alertModalDeleteCommunities.open();
}
public confirmedDeleteCommunities(data: any) {
this.showLoading = true;
this.updateErrorMessage = "";
this._helpContentService.deleteCommunities(this.selectedCommunities, this.properties.adminToolsAPIURL).subscribe(
_ => {
this.deleteCommunitiesFromArray(this.selectedCommunities);
this.showLoading = false;
},
error => this.handleUpdateError('System error deleting the selected communities', error)
);
}
public editCommunity(i : number) {
let community : Community = this.checkboxes[i].community;
this.formGroup.patchValue(community);
//this.updateModal.showModal();
this.modalErrorMessage = "";
this.communitiesModalOpen(this.alertModalUpdateCommunity, "Update", "Update Community");
}
public newCommunity() {
this.formComponent.reset();
this.modalErrorMessage = "";
this.communitiesModalOpen(this.alertModalSaveCommunity, "Save", "Add a new Community");
}
private communitiesModalOpen(modal: any, title: string, yesBtn: string) {
modal.cancelButton = true;
modal.okButton = true;
modal.alertTitle = title;
modal.okButtonText = yesBtn;
modal.open();
}
public communitySaveConfirmed(data: any) {
if(!this.formGroup.valid) {
this.communitiesModalOpen(this.alertModalSaveCommunity, "Save", "Add a new Community");
this.modalErrorMessage = "Please fill in all required fields marked with *";
} else {
this.modalErrorMessage = "";
this._helpContentService.saveCommunity(<Community> this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
community => {
this.communitySavedSuccessfully(community);
},
error => this.handleUpdateError("System error creating community", error)
);
}
}
public communityUpdateConfirmed(data: any) {
if(!this.formGroup.valid) {
this.communitiesModalOpen(this.alertModalUpdateCommunity, "Update", "Update Community");
this.modalErrorMessage = "Please fill in all required fields marked with *";
} else {
this._helpContentService.updateCommunity(<Community> this.formGroup.value, this.properties.adminToolsAPIURL).subscribe(
community => {
this.communityUpdatedSuccessfully(community);
},
error => this.handleUpdateError("System error updating community", error)
);
}
}
public communitySavedSuccessfully(community: Community) {
this.checkboxes.push(<CheckCommunity>{community : community, checked : false});
console.info("checkboxes length: "+this.checkboxes.length);
this.applyCheck(false);
}
public communityUpdatedSuccessfully(community : Community) {
this.checkboxes.find(checkItem => checkItem.community._id==community._id).community = community;
this.applyCheck(false);
}
public filterBySearch(text : string) {
this.searchText = new RegExp(text,'i');
this.applyFilter();
}
public applyFilter() {
this.checkboxes = [];
this.communities.filter(item => this.filterCommunities(item)).forEach(
_ => this.checkboxes.push(<CheckCommunity>{community: _, checked: false})
);
}
public filterCommunities(community : Community) : boolean {
let textFlag = this.searchText.toString() == '' || (community.name).match(this.searchText) != null;
return textFlag;
}
handleUpdateError(message: string, error) {
if(error == null) {
this.formComponent.reset();
} 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;
}
}