admin-funding-agencies-form.presentational.ts 2.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  Input,
} from "@angular/core";
import {
  FormBuilder,
  Validators,
} from "@angular/forms";
Florent Poittevin's avatar
Florent Poittevin committed
11
import {OrganizationalUnit} from "@app/generated-api";
12
import {FundingAgencyExtended} from "@deposit/models/funding-agency-extended.model";
13
import {SharedAbstractFormPresentational} from "@shared/components/presentationals/shared-abstract-form/shared-abstract-form.presentational";
14
import {LocalModelAttributeEnum} from "@shared/enums/model-attribute.enum";
15
import {BaseFormDefinition} from "@shared/models/base-form-definition.model";
16
import _ from "lodash";
17
18
19
20
import {
  PropertyName,
  SolidifyValidator,
} from "solidify-frontend";
21
22
23
24

@Component({
  selector: "dlcm-admin-funding-agencies-form",
  templateUrl: "./admin-funding-agencies-form.presentational.html",
25
  styleUrls: ["../../../../../../shared/components/presentationals/shared-abstract-form/shared-abstract-form.presentational.scss"],
26
27
  changeDetection: ChangeDetectionStrategy.OnPush,
})
28
export class AdminFundingAgenciesFormPresentational extends SharedAbstractFormPresentational<FundingAgencyExtended> {
29
30
  formDefinition: FormComponentFormDefinition = new FormComponentFormDefinition();

31
32
33
34
35
36
  @Input()
  selectedOrgUnits: OrganizationalUnit[];

  @Input()
  listOrgUnits: OrganizationalUnit[];

37
38
39
40
41
42
43
44
45
46
47
  constructor(protected readonly _changeDetectorRef: ChangeDetectorRef,
              private readonly _fb: FormBuilder) {
    super(_changeDetectorRef);
  }

  protected initNewForm(): void {
    this.form = this._fb.group({
      [this.formDefinition.acronym]: ["", [Validators.required, SolidifyValidator]],
      [this.formDefinition.name]: ["", [Validators.required, SolidifyValidator]],
      [this.formDefinition.description]: ["", [SolidifyValidator]],
      [this.formDefinition.url]: ["", [SolidifyValidator]],
48
      [this.formDefinition.organizationalUnits]: [],
49
50
51
    });
  }

52
  protected bindFormTo(fundingAgency: FundingAgencyExtended): void {
53
54
55
56
57
    this.form = this._fb.group({
      [this.formDefinition.acronym]: [fundingAgency.acronym, [Validators.required, SolidifyValidator]],
      [this.formDefinition.name]: [fundingAgency.name, [Validators.required, SolidifyValidator]],
      [this.formDefinition.description]: [fundingAgency.description, [SolidifyValidator]],
      [this.formDefinition.url]: [fundingAgency.url, [SolidifyValidator]],
58
      [this.formDefinition.organizationalUnits]: [_.map(this.selectedOrgUnits, LocalModelAttributeEnum.resId)],
59
60
61
    });
  }

62
63
  protected treatmentBeforeSubmit(fundingAgency: FundingAgencyExtended): FundingAgencyExtended {
    return fundingAgency;
64
65
66
67
68
69
70
  }
}

class FormComponentFormDefinition extends BaseFormDefinition {
  @PropertyName() acronym: string;
  @PropertyName() name: string;
  @PropertyName() description: string;
71
  @PropertyName() organizationalUnits: string;
72
73
  @PropertyName() url: string;
}