import {Component, forwardRef, OnDestroy, OnInit, ViewChild} from '@angular/core'; import {ModalComponent} from '../../modals/modal/modal.component'; import {FormBuilder, FormGroup, FormGroupDirective, NG_VALUE_ACCESSOR, Validators} from '@angular/forms'; import {Activity, CarePlan, Note} from '@nspop/gm-web-facade-api'; import {copy} from '../../../utils/copy'; import {MatInput} from '@angular/material/input'; import {ActivitiesService} from '../activities.service'; import {calculateWeekNumberFromPregnancyStart} from '../../../utils/dates'; import {CarePlanService} from '../../api/care-plan.service'; import {AppError} from '../../services/error.service'; @Component({ selector: 'app-note-modal', templateUrl: './note-modal.component.html', styleUrls: ['./note-modal.component.scss'] }) export class NoteModalComponent implements OnInit, OnDestroy { @ViewChild('modal') public modalComponent: ModalComponent | undefined; @ViewChild('formDirective') public formDirective: FormGroupDirective | undefined; @ViewChild('title') public title: MatInput | undefined; public form: FormGroup | undefined; public carePlan: CarePlan | undefined; public activity: Activity | undefined; public isEditMode: boolean | undefined; public isDirty: boolean = false; public submitError: boolean = false; public allowActivityChange = true; public activityId: string | undefined; public date: Date = new Date(); private activities: Activity[] | undefined; constructor( private fb: FormBuilder, private carePlanService: CarePlanService, private activitiesService: ActivitiesService, ) { this.carePlanService.activities$.subscribe((activities) => { this.activities = activities; }); } ngOnInit(): void { this.form = this.fb.group({ title: ['', {disabled: true}], content: ['', Validators.required], date: [new Date(), Validators.required] }); } ngOnDestroy(): void { this.form = undefined; } public submit(form: FormGroup | undefined, formDirective: FormGroupDirective | undefined): void { if (!this.activity || !this.carePlan || !this.carePlan.activities) { this.submitError = true; return; } if (form?.valid) { const activity = this.carePlan.activities.find((a) => a.id === this.activity?.id && a.id !== undefined); if (!activity) { this.submitError = true; return console.error('Could not find activity with id: ' + this.activity.id); } const note = this.getNoteFromForm(); note.documentUniqueId = this.activity.note?.documentUniqueId; note.version = this.activity.note?.version; const {date} = this.form?.value; if (date) { note.date = date; } activity.note = note; this.submitCarePlan(formDirective); } else { form?.markAllAsTouched(); } } private submitCarePlan(formDirective?: FormGroupDirective): void { if (!this.carePlan) { return; } this.carePlanService.update(this.carePlan, this.carePlan.id + '', this.carePlan.version + '').subscribe( () => { formDirective?.resetForm(); this.setVisible(false); this.carePlanService.refresh(); }, (error) => { this.submitError = true; throw new AppError(undefined, error); }); } public setNoteModalState( isVisible: boolean, activity: Activity | undefined, allowActivityChange: boolean | undefined): void { this.allowActivityChange = !!allowActivityChange; this.setVisible(isVisible); if (activity) { this.setActivity(activity); } } setActivity(activity: Activity | undefined): void { if (activity) { this.activity = copy(activity); this.activityId = this.activity.id; } this.isEditMode = !!activity?.note; } setVisible(isVisible: boolean): void { this.carePlan = this.activitiesService.carePlan; this.isDirty = false; this.submitError = false; if (this.modalComponent) { this.modalComponent.setVisible(isVisible); this.formDirective?.resetForm(); this.setFormValues(); } } setFormValues(): void { let title = this.activity?.title; const from = calculateWeekNumberFromPregnancyStart(this.activitiesService.dataCard, this.activity?.plannedTime?.from); const to = calculateWeekNumberFromPregnancyStart(this.activitiesService.dataCard, this.activity?.plannedTime?.to); title += from && to ? ` (${from} - ${to})` : ''; this.form = this.fb.group({ title: [title || ''], content: [ { value: this.activity?.note?.content || '', disabled: this.activity && this.activity.ownership === 'otherUser' }, Validators.required ], date: [this.activity?.note?.date ? this.activity?.note?.date : new Date(), Validators.required] }); this.form.controls.title.disable(); } isVisible(): boolean { return !!this.modalComponent?.isModalVisible; } getNoteFromForm(): Note { const {content} = this.form?.value; return { content, dirty: this.isDirty, } as Note; } public onNoteContentChange(): void { this.isDirty = true; } public validateActivityId(): boolean { if (this.activities && this.activityId) { this.activity = this.activities?.find(activity => activity.id === this.activityId); this.setFormValues(); } return false; } }