2023-03-02 15:20:56 +01:00
|
|
|
import {HttpClient} from "@angular/common/http";
|
|
|
|
import {Component, Input} from "@angular/core";
|
2023-03-13 20:23:00 +01:00
|
|
|
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";
|
|
|
|
|
|
|
|
@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;
|
2023-03-09 15:29:25 +01:00
|
|
|
@Input() isFeedback: boolean = true;
|
2023-03-02 15:20:56 +01:00
|
|
|
|
|
|
|
public loading: boolean;
|
2023-03-13 20:23:00 +01:00
|
|
|
public sdgs: UntypedFormArray;
|
2023-03-02 15:20:56 +01:00
|
|
|
|
|
|
|
constructor(
|
2023-03-13 20:23:00 +01:00
|
|
|
private httpClient: HttpClient,
|
|
|
|
private fb: UntypedFormBuilder
|
2023-03-02 15:20:56 +01:00
|
|
|
) {}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.loading = true;
|
2023-03-13 20:23:00 +01:00
|
|
|
this.sdgs = this.fb.array([]);
|
2023-03-02 15:20:56 +01:00
|
|
|
this.httpClient.get(this.properties.domain+'/assets/common-assets/vocabulary/sdg.json').subscribe(data => {
|
|
|
|
data['sdg'].forEach(element => {
|
2023-03-13 20:23:00 +01:00
|
|
|
// 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
|
|
|
});
|
2023-03-09 15:29:25 +01:00
|
|
|
if(this.isFeedback) {
|
2023-03-13 20:23:00 +01:00
|
|
|
// // 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)
|
|
|
|
}));
|
2023-03-09 15:29:25 +01:00
|
|
|
}
|
|
|
|
this.loading = false;
|
2023-03-02 15:20:56 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-13 20:23:00 +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() {
|
2023-03-13 20:23:00 +01:00
|
|
|
return this.sdgs.controls.slice(0, Math.ceil(this.sdgs.length/2));
|
2023-03-02 15:20:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public get secondColumn() {
|
2023-03-13 20:23:00 +01:00
|
|
|
return this.sdgs.controls.slice(Math.ceil(this.sdgs.length/2), this.sdgs.length);
|
2023-03-02 15:20:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public getSelectedSubjects() {
|
2023-03-13 20:23:00 +01:00
|
|
|
if(this.sdgs) {
|
|
|
|
return this.sdgs.value.filter(sub => sub.checked == true);
|
|
|
|
}
|
|
|
|
return [];
|
2023-03-02 15:20:56 +01:00
|
|
|
}
|
|
|
|
|
2023-03-13 20:23:00 +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);
|
|
|
|
}
|
|
|
|
}
|