openaire-library/sdg/sdg-selection/sdg-selection.component.ts

85 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-03-02 15:20:56 +01:00
import {HttpClient} from "@angular/common/http";
import {Component, Input} from "@angular/core";
import {UntypedFormArray, UntypedFormBuilder} from "@angular/forms";
2023-03-02 15:20:56 +01:00
import {properties} from "../../../../environments/environment";
import {EnvProperties} from "../../utils/properties/env-properties";
import {StringUtils} from "../../utils/string-utils.class";
import {ISVocabulariesService} from "../../utils/staticAutoComplete/ISVocabularies.service";
2023-03-02 15:20:56 +01:00
@Component({
selector: 'sdg-selection',
templateUrl: 'sdg-selection.component.html',
styleUrls: ['sdg-selection.component.less']
})
export class SdgSelectionComponent {
public properties: EnvProperties = properties;
@Input() subjects: string[];
@Input() entityType: string;
@Input() isFeedback: boolean = true;
2023-03-02 15:20:56 +01:00
public loading: boolean;
public sdgs: UntypedFormArray;
2023-03-02 15:20:56 +01:00
constructor(
private vocabulariesService: ISVocabulariesService,
private fb: UntypedFormBuilder
2023-03-02 15:20:56 +01:00
) {}
ngOnInit() {
this.loading = true;
this.sdgs = this.fb.array([]);
this.vocabulariesService.getSDGs(properties).subscribe(data => {
2023-03-02 15:20:56 +01:00
data['sdg'].forEach(element => {
// this.sdgs.push({code: element.code, id: element.id, label: element.label, html: element.html, checked: this.subjects?.includes(element.id)});
this.sdgs.push(this.fb.group({
code: this.fb.control(element.code),
id: this.fb.control(element.id),
label: this.fb.control(element.label),
html: this.fb.control(element.html),
checked: this.fb.control(this.subjects?.includes(element.id))
}));
2023-03-02 15:20:56 +01:00
});
if(this.isFeedback) {
// // this.sdgs.push({code: '18', id: 'No SDGs are relevant for this ' + this.getEntityName(this.entityType), label: 'Not relevant', html: 'Not relevant', checked: false});
this.sdgs.push(this.fb.group({
code: this.fb.control('18'),
id: this.fb.control('No SDGs are relevant for this ' + this.getEntityName(this.entityType)),
label: this.fb.control('Not relevant'),
html: this.fb.control('Not relevant'),
checked: this.fb.control(false)
}));
}
this.loading = false;
2023-03-02 15:20:56 +01:00
});
}
public reset() {
this.sdgs.controls.forEach(control => {
control.get('checked').setValue(this.subjects?.includes(control.value.id));
});
}
2023-03-02 15:20:56 +01:00
public get firstColumn() {
return this.sdgs.controls.slice(0, Math.ceil(this.sdgs.length/2));
2023-03-02 15:20:56 +01:00
}
public get secondColumn() {
return this.sdgs.controls.slice(Math.ceil(this.sdgs.length/2), this.sdgs.length);
2023-03-02 15:20:56 +01:00
}
public getSelectedSubjects() {
if(this.sdgs) {
return this.sdgs.value.filter(sub => sub.checked == true);
}
return [];
2023-03-02 15:20:56 +01:00
}
public get hasChanges() {
return !!this.sdgs && this.sdgs.dirty;
}
2023-03-02 15:20:56 +01:00
private getEntityName (entityType:string) {
return StringUtils.getEntityName(entityType, false);
}
}