[develop | DONE | FIXED ] problem in saving subjects - make a single request

This commit is contained in:
argirok 2024-09-18 13:22:09 +03:00
parent 56045fba35
commit 2777d4ccc3
2 changed files with 9 additions and 62 deletions

View File

@ -295,34 +295,13 @@ export class SubjectsEditFormComponent implements OnInit {
public saveAllSubjects() { public saveAllSubjects() {
let selectedSdg = this.sdgSelection.getSelectedSubjects().map(a => a.id); let selectedSdg = this.sdgSelection.getSelectedSubjects().map(a => a.id);
let selectedFos = this.fosSelection.getSelectedSubjects().map(a => a.id); let selectedFos = this.fosSelection.getSelectedSubjects().map(a => a.id);
let requests = [ this.subscriptions.push(this._subjectsService.updateSubjects(this.communityId ,this.displayedSubjects, selectedFos, selectedSdg).subscribe(res => {
...this.saveSubjects(this.community.subjects, this.displayedSubjects, 'subjects'), this.community.subjects = this.displayedSubjects;
...this.saveSubjects(this.community.sdg, selectedSdg, 'sdg'), this.community.fos = selectedFos;
...this.saveSubjects(this.community.fos, selectedFos, 'fos') this.community.sdg = selectedSdg
];
this.subscriptions.push(forkJoin(requests).subscribe(res => {
this.afterUpdateActions(res, "updated"); this.afterUpdateActions(res, "updated");
})); }))
}
public saveSubjects(subjects, displayedSubjects, type: string) {
if (this.communityId != null && this.communityId !== '') {
this.loading = true;
const subjectsToDeleteAr = this.getSubjectsExistOnlyInFirst(subjects, displayedSubjects);
const subjectsToAddAr = this.getSubjectsExistOnlyInFirst(displayedSubjects, subjects);
const subjectsToDelete = this.getNonEmptyItems(subjectsToDeleteAr);
const subjectsToAdd = this.getNonEmptyItems(subjectsToAddAr);
let requests = [];
if (subjectsToDelete.length > 0) {
requests.push(this._subjectsService.removeSubjects(
this.properties.communityAPI + this.communityId + '/' + type, subjectsToDelete))
}
if (subjectsToAdd.length > 0) {
requests.push(this._subjectsService.addSubjects(
this.properties.communityAPI + this.communityId + '/' + type, subjectsToAdd))
}
return requests;
}
} }
handleUpdateError(message: string, error = null) { handleUpdateError(message: string, error = null) {
@ -334,15 +313,6 @@ export class SubjectsEditFormComponent implements OnInit {
} }
afterUpdateActions(response, message: string) { afterUpdateActions(response, message: string) {
response.forEach(res => {
if (res.subjects) {
this.community.subjects = res.subjects;
} else if (res.sdg) {
this.community.sdg = res.sdg;
} else if (res.fos) {
this.community.fos = res.fos;
}
});
this._communityService.updateSubjects(this.community.subjects, this.community.fos, this.community.sdg); this._communityService.updateSubjects(this.community.subjects, this.community.fos, this.community.sdg);
this._clearCacheService.purgeBrowserCache("Subjects " + message, this.communityId); this._clearCacheService.purgeBrowserCache("Subjects " + message, this.communityId);
NotificationHandler.rise('Subjects successfully ' + message + '!') NotificationHandler.rise('Subjects successfully ' + message + '!')

View File

@ -1,6 +1,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from "@angular/common/http"; import {HttpClient, HttpHeaders} from "@angular/common/http";
import {map} from "rxjs/operators"; import {map} from "rxjs/operators";
import {properties} from "../../../environments/environment";
@Injectable() @Injectable()
export class SubjectsService { export class SubjectsService {
@ -9,34 +10,10 @@ export class SubjectsService {
} }
addSubjects(url: string, subjects: any) { updateSubjects(communityId: string, subjects: any, fos, sdgs) {
let headers = new HttpHeaders({'Content-Type': 'application/json'}); let headers = new HttpHeaders({'Content-Type': 'application/json'});
const body = JSON.stringify({subjects: subjects, fos: fos, sdg: sdgs});
const body = JSON.stringify(subjects); return this.http.post(properties.communityAPI + communityId , body, {headers: headers});
return this.http.post(url, body, {headers: headers})
// .do(request => console.log("Insert Response:"+request.status))
//.map(res => res.json())
.pipe(map(res => {
res['method'] = 'post';
return res;
}));
} }
removeSubjects(url: string, subjects: any) {
//const headers = new Headers({'Content-Type': 'application/json'});
let headers = new HttpHeaders({'Content-Type': 'application/json'});
const body = JSON.stringify(subjects);
//const options = new RequestOptions({headers: headers, body: body});
//return this.http.delete(url, options)
return this.http.request('delete', url, { body: body, headers: headers})
// .do(request => console.log("Delete Response:"+request.status))
//.map(res => res.json())
.pipe(map(res => {
res['method'] = 'delete';
return res;
}));
}
} }