2018-12-03 12:33:03 +01:00
|
|
|
import {Component, OnInit, Input, ElementRef} from '@angular/core';
|
2018-10-30 16:31:16 +01:00
|
|
|
import {SimpleChanges, OnChanges} from '@angular/core';
|
|
|
|
import {FormGroup, FormArray, FormBuilder, Validators} from "@angular/forms";
|
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
|
|
|
|
import {HelpContentService} from "../../../services/help-content.service";
|
|
|
|
import {CommunityService} from "../../../openaireLibrary/connect/community/community.service";
|
2018-11-13 16:26:49 +01:00
|
|
|
import {SubjectsService} from "../subjects.service"
|
2018-10-30 16:31:16 +01:00
|
|
|
import {EnvProperties} from '../../../openaireLibrary/utils/properties/env-properties';
|
|
|
|
|
|
|
|
import {CommunityInfo} from '../../../openaireLibrary/connect/community/communityInfo';
|
2018-11-13 16:26:49 +01:00
|
|
|
|
|
|
|
import {Session} from '../../../openaireLibrary/login/utils/helper.class';
|
|
|
|
import {LoginErrorCodes} from '../../../openaireLibrary/login/utils/guardHelper.class';
|
|
|
|
|
|
|
|
import { concat } from 'rxjs/observable/concat';
|
2018-10-30 16:31:16 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'subjects-edit-form',
|
|
|
|
templateUrl: './subjects-edit-form.component.html',
|
|
|
|
})
|
|
|
|
|
|
|
|
export class SubjectsEditFormComponent implements OnInit{
|
|
|
|
|
|
|
|
@Input('group')
|
|
|
|
myForm: FormGroup;
|
|
|
|
|
|
|
|
public showLoading: boolean = true;
|
|
|
|
public errorMessage: string = '';
|
|
|
|
public updateErrorMessage: string = '';
|
|
|
|
|
|
|
|
public successfulSaveMessage: string = '';
|
|
|
|
public successfulResetMessage: string = '';
|
|
|
|
|
|
|
|
public hasChanged: boolean = false;
|
|
|
|
|
|
|
|
public res=[];
|
|
|
|
|
|
|
|
params: any;
|
|
|
|
|
|
|
|
public communityId = null;
|
|
|
|
public community = null;
|
|
|
|
public properties: EnvProperties = null;
|
|
|
|
public newsubject ="";
|
|
|
|
public edit = null;
|
|
|
|
public editSubjectOriginalValue = null;
|
|
|
|
public originalSubjects = [];
|
|
|
|
|
2018-12-03 12:33:03 +01:00
|
|
|
constructor (private element: ElementRef,
|
|
|
|
private route: ActivatedRoute,
|
2018-10-30 16:31:16 +01:00
|
|
|
private _router: Router,
|
|
|
|
public _fb: FormBuilder,
|
|
|
|
private _helpContentService: HelpContentService,
|
|
|
|
private _communityService: CommunityService,
|
|
|
|
private _subjectsService: SubjectsService){ }
|
|
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
|
|
|
|
this.properties = data.envSpecific;
|
|
|
|
this.route.queryParams.subscribe(
|
|
|
|
communityId => {
|
2018-12-03 12:33:03 +01:00
|
|
|
this.scroll();
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
this.communityId = communityId['communityId'];
|
|
|
|
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
|
|
|
if (this.communityId != null && this.communityId != '') {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.updateErrorMessage = "";
|
|
|
|
this.errorMessage = "";
|
|
|
|
|
|
|
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI+this.communityId).subscribe (
|
|
|
|
community => {
|
|
|
|
this.community = community;
|
|
|
|
this.params = {community: encodeURIComponent('"' + community.queryId + '"')};
|
|
|
|
this.originalSubjects = [];
|
|
|
|
for (var i = 0; i < this.community.subjects.length; i++) {
|
|
|
|
this.originalSubjects.push(this.community.subjects[i]);
|
|
|
|
}
|
|
|
|
if (this.community.subjects.length == 0) {
|
|
|
|
this.community.subjects.push("");
|
|
|
|
}
|
|
|
|
this.showLoading = false;
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving community profile', error)
|
|
|
|
);
|
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public addSubject() {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
2018-10-30 16:31:16 +01:00
|
|
|
this.community.subjects.push("");
|
2018-11-13 16:26:49 +01:00
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public removeSubject(i: any) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
2018-10-30 16:31:16 +01:00
|
|
|
this.community.subjects.splice(i, 1);
|
2018-11-13 16:26:49 +01:00
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public resetForm(communityId: string) {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
2018-10-30 16:31:16 +01:00
|
|
|
if (communityId != null && communityId != '') {
|
|
|
|
this.showLoading = true;
|
|
|
|
this.updateErrorMessage = "";
|
|
|
|
this.errorMessage = "";
|
|
|
|
|
|
|
|
this._communityService.getCommunity(this.properties, this.properties.communityAPI + communityId).subscribe (
|
|
|
|
community => {
|
|
|
|
this.community = community;
|
|
|
|
this.params = {community: encodeURIComponent('"' + community.queryId + '"')};
|
|
|
|
this.showLoading = false;
|
|
|
|
this.handleSuccessfulReset('Form reseted!');
|
|
|
|
},
|
|
|
|
error => this.handleError('System error retrieving community profile', error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.resetChange();
|
2018-11-13 16:26:49 +01:00
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// save(newValue){
|
|
|
|
// this.edit=null;
|
|
|
|
// console.log(this.editSubjectOriginalValue + "-->"+newValue)
|
|
|
|
// if (this.communityId != null && this.communityId != '' && this.editSubjectOriginalValue != newValue) {
|
|
|
|
// this.showLoading = true;
|
|
|
|
// var subjectsToDelete = this.parseUpdatedSubjects([this.editSubjectOriginalValue]);
|
|
|
|
// var subjectsToAdd = this.parseUpdatedSubjects([newValue]);
|
|
|
|
// let obs = concat(this._subjectsService.addSubjects(this.properties.communityAPI+this.communityId+"/subjects", subjectsToAdd),
|
|
|
|
// this._subjectsService.removeSubjects(this.properties.communityAPI+this.communityId+"/subjects", subjectsToDelete));
|
|
|
|
// obs.subscribe(res => {
|
|
|
|
// console.log(res);
|
|
|
|
// this.community.subjects = res[1]["subjects"];
|
|
|
|
// this.originalSubjects = this.community.subjects;
|
|
|
|
// this.handleSuccessfulSave('Subjects updated!')
|
|
|
|
// this.showLoading = false;
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// this.editSubjectOriginalValue = null;
|
|
|
|
// }
|
|
|
|
public getSubjectsExistOnlyInFirst(firstArray: string[], secondArray: string[]): string[] {
|
|
|
|
|
|
|
|
var difference = [];
|
|
|
|
for (var i = 0; i < firstArray.length; i++) {
|
|
|
|
if (secondArray.indexOf(firstArray[i]) == -1) {
|
|
|
|
difference.push(firstArray[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return difference;
|
|
|
|
|
|
|
|
}
|
|
|
|
public updateSubjects() {
|
2018-11-13 16:26:49 +01:00
|
|
|
if(!Session.isLoggedIn()){
|
|
|
|
this._router.navigate(['/user-info'], { queryParams: { "errorCode": LoginErrorCodes.NOT_VALID, "redirectUrl": this._router.url} });
|
|
|
|
} else {
|
2018-10-30 16:31:16 +01:00
|
|
|
if (this.communityId != null && this.communityId != '') {
|
|
|
|
this.showLoading = true;
|
|
|
|
var subjectsToDeleteAr = this.getSubjectsExistOnlyInFirst(this.originalSubjects, this.community.subjects);
|
|
|
|
var subjectsToAddAr = this.getSubjectsExistOnlyInFirst(this.community.subjects, this.originalSubjects);
|
|
|
|
|
|
|
|
var subjectsToDelete = this.parseUpdatedSubjects(subjectsToDeleteAr);
|
|
|
|
var subjectsToAdd = this.parseUpdatedSubjects(subjectsToAddAr);
|
|
|
|
if(subjectsToAddAr.length > 0 && subjectsToDeleteAr.length > 0){
|
|
|
|
let obs = concat(this._subjectsService.addSubjects(this.properties.communityAPI + this.communityId + "/subjects", subjectsToAdd),
|
|
|
|
this._subjectsService.removeSubjects(this.properties.communityAPI + this.communityId + "/subjects", subjectsToDelete));
|
|
|
|
obs.subscribe(res => {
|
|
|
|
if (res["method"] == "delete") {
|
|
|
|
this.afterUpdateActions(res);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error updating subjects', error)
|
|
|
|
);
|
|
|
|
}else if (subjectsToAddAr.length > 0){
|
|
|
|
this._subjectsService.addSubjects(this.properties.communityAPI + this.communityId +"/subjects", subjectsToAdd).subscribe(res => {
|
|
|
|
this.afterUpdateActions(res);
|
|
|
|
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error updating subjects', error)
|
|
|
|
);
|
|
|
|
}else if (subjectsToDeleteAr.length > 0) {
|
|
|
|
this._subjectsService.removeSubjects(this.properties.communityAPI + this.communityId + "/subjects", subjectsToDelete).subscribe(res => {
|
|
|
|
this.afterUpdateActions(res);
|
|
|
|
|
|
|
|
},
|
|
|
|
error => this.handleUpdateError('System error updating subjects', error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// this._router.navigate(['/manage-subjects'], {queryParams: { "communityId": this.communityId}});
|
|
|
|
|
|
|
|
}
|
|
|
|
this.resetChange();
|
2018-11-13 16:26:49 +01:00
|
|
|
}
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|
|
|
|
afterUpdateActions(res){
|
|
|
|
this.community.subjects = res["subjects"];
|
|
|
|
this.originalSubjects = [];
|
|
|
|
for (var i = 0; i < this.community.subjects.length; i++) {
|
|
|
|
this.originalSubjects.push(this.community.subjects[i]);
|
|
|
|
}
|
|
|
|
if (this.community.subjects.length == 0) {
|
|
|
|
this.community.subjects.push("");
|
|
|
|
}
|
|
|
|
this.handleSuccessfulSave('Subjects updated!')
|
|
|
|
this.showLoading = false;
|
|
|
|
}
|
|
|
|
private parseUpdatedSubjects(subjects): {} {
|
|
|
|
var parsedSubjects = this.getNonEmptyItems(subjects);
|
|
|
|
return parsedSubjects;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private getNonEmptyItems(data: string[]): string[] {
|
|
|
|
let length = data.length;
|
|
|
|
let arrayNonEmpty = new Array<string>();
|
|
|
|
|
|
|
|
let j = 0;
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
if (this.isEmpty(data[i])) {
|
|
|
|
} else if (this.isNonEmpty(data[i])) {
|
|
|
|
arrayNonEmpty[j] = data[i];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arrayNonEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
private hasFilled(data: any): boolean {
|
|
|
|
if (this.isNonEmpty(data) && !this.isEmpty(data)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private isEmpty(data: string): boolean {
|
|
|
|
if (data != undefined && !data.replace(/\s/g, '').length)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private isNonEmpty(data: string): boolean {
|
|
|
|
if (data != undefined && data != null)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private hasValidEmail(data: any): boolean {
|
|
|
|
let length = data['managers'].length;
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
if (!this.emailValidator(data['managers'][i])){
|
|
|
|
// TODO remove console message after final testing
|
2019-02-14 13:20:00 +01:00
|
|
|
//console.log("INVALID EMAIL");
|
2018-10-30 16:31:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO remove console message after final testing
|
2019-02-14 13:20:00 +01:00
|
|
|
//console.log("ALL EMAILS ARE VALID");
|
2018-10-30 16:31:16 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private emailValidator(email: any): boolean {
|
|
|
|
if (email.match("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"))
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private change() {
|
|
|
|
this.hasChanged = true;
|
|
|
|
this.successfulSaveMessage = '';
|
|
|
|
this.successfulResetMessage = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
private resetChange() {
|
|
|
|
this.hasChanged = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get form() {
|
|
|
|
return this._fb.group({
|
|
|
|
_id : '',
|
|
|
|
name : ['', Validators.required]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public reset() {
|
|
|
|
this.myForm.patchValue({
|
|
|
|
name : '',
|
|
|
|
_id : ''
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUpdateError(message: string, error) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSuccessfulSave(message) {
|
|
|
|
this.showLoading = false;
|
|
|
|
this.successfulSaveMessage = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSuccessfulReset(message) {
|
|
|
|
this.showLoading = false;
|
|
|
|
this.successfulResetMessage = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
trackByFn(index: any, item: any) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2018-12-03 12:33:03 +01:00
|
|
|
public scroll() {
|
|
|
|
if (typeof document !== 'undefined') {
|
|
|
|
this.element.nativeElement.scrollIntoView();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:31:16 +01:00
|
|
|
}
|