import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core'; import {ModalComponent} from '../../modals/modal/modal.component'; import {FormBuilder, FormGroup, FormGroupDirective, Validators} from '@angular/forms'; import {Activity, 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'; import {NoteService} from '../../api/note.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 activity: Activity | undefined; public isEditMode: boolean | undefined; public isDirty: boolean = false; public submitError: boolean = false; public allowActivityChange = true; public date: Date | undefined; constructor( private fb: FormBuilder, private carePlanService: CarePlanService, private activitiesService: ActivitiesService, private noteService: NoteService ) { } ngOnInit(): void { this.form = this.fb.group({ title: ['', {disabled: true}], content: ['', Validators.required] }); } ngOnDestroy(): void { this.form = undefined; } public submit(form: FormGroup | undefined, formDirective: FormGroupDirective | undefined): void { if (!this.activity) { this.submitError = true; return; } if (form?.valid) { if (!this.activity?.id) { 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; this.activity.note = note; this.noteService.createOrUpdateNote(this.activity.id, note, this.date).subscribe( () => { this.setVisible(false); formDirective?.resetForm(); this.activity = undefined; this.carePlanService.refresh(); }, (error) => { this.submitError = true; throw new AppError(undefined, error); }); } else { form?.markAllAsTouched(); } } public setNoteModalState( isVisible: boolean, activity: Activity | undefined, allowActivityChange: boolean | undefined): void { if (activity) { this.setActivity(activity); } this.allowActivityChange = !!allowActivityChange; this.setVisible(isVisible); } setActivity(activity: Activity | undefined): void { if (activity) { this.activity = copy(activity); } this.isEditMode = !!activity?.note; } setVisible(isVisible: boolean): void { this.isDirty = false; this.submitError = false; if (this.modalComponent) { this.modalComponent.setVisible(isVisible); if (isVisible) { this.setFormValues(this.activity); } } } setFormValues(activity: Activity | undefined): void { let title = activity?.title; const from = calculateWeekNumberFromPregnancyStart(this.activitiesService.dataCard, activity?.plannedTime?.from); const to = calculateWeekNumberFromPregnancyStart(this.activitiesService.dataCard, activity?.plannedTime?.to); title += from && to ? ` (${from} - ${to})` : ''; function getDateFromActivity(selectedActivity: Activity | undefined): Date | undefined { if (!selectedActivity) { return; } switch (selectedActivity.status) { case 'finished': if (selectedActivity.conductedTime) { return selectedActivity.conductedTime; } else { return selectedActivity.plannedTime?.from; } default: return new Date(); } } this.form = this.fb.group({ title: [title || ''], content: [ { value: activity?.note?.content || '', disabled: activity && activity.ownership === 'otherUser' }, Validators.required ] }); this.date = getDateFromActivity(activity); this.form.controls.title.disable(); } public isDateDisabled(activity: Activity | undefined): boolean { if (!activity) { return true; } return activity?.status === 'finished'; } 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 activityChanged(): void { if (this.activity) { this.setFormValues(this.activity); } } public handleClose(): void { this.formDirective?.resetForm(); } }