import {Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild} from '@angular/core'; import {ModalComponent} from '../../modals/modal/modal.component'; import { Activity, SaveRegularObservationsRequest, SaveUltrasoundObservationsRequest, DataCard, Note, } from '@nspop/gm-web-facade-api'; import {FormComponent} from '../../confirm-navigation-if-dirty.framework'; import {NgForm} from '@angular/forms'; import {copy} from '../../../utils/copy'; import {merge, Observable, of} from 'rxjs'; import {finalize} from 'rxjs/operators'; import {DataCardWrapper} from '../../model-wrappers/dataCard.wrapper'; import {ObservationService} from '../../api/observation.service'; import {DataCardService} from '../../api/data-card.service'; import {CarePlanService} from '../../api/care-plan.service'; export type measurementsModalEvent = { isOpen: boolean; observationType: 'regular' | 'ultrasound'; allowTypeChange: boolean; activity?: Activity; allowActivityChange: boolean; }; type ObservationFormType = 'regular' | 'ultrasound'; @Component({ selector: 'app-measurements-modal', templateUrl: './measurements-modal.component.html', styleUrls: ['./measurements-modal.component.scss'] }) export class MeasurementsModalComponent extends FormComponent implements OnChanges, OnInit { public note: Note | undefined; public noteContent: string = ''; public isSaving = false; public dataCard?: DataCardWrapper; public activityId: string | undefined; public observationDataRegular?: SaveRegularObservationsRequest | undefined; public observationDataUltrasound?: SaveUltrasoundObservationsRequest | undefined; @ViewChild('modal') public modalComponent: ModalComponent | undefined; @ViewChild('form') public form: NgForm | undefined; @Input() public isOpen = false; @Input() public observationType: ObservationFormType = 'regular'; @Input() public allowTypeChange = false; @Input() public activity: Activity | undefined = undefined; @Input() public allowActivityChange = true; @Output() public modalClosed: EventEmitter = new EventEmitter(); @Output() public save: EventEmitter = new EventEmitter(); private dataCardId = ''; private dataCardVersion = ''; constructor( private carePlanService: CarePlanService, private dataCardService: DataCardService, private observationService: ObservationService, ) { super(); } ngOnInit(): void { this.dataCardService.selectedDataCard$.subscribe((dataCard?: DataCard) => { if (dataCard) { this.dataCardId = dataCard.documentUniqueId || ''; this.dataCardVersion = dataCard.version || ''; this.dataCard = new DataCardWrapper(copy(dataCard)); } }); } ngOnChanges(changes: SimpleChanges): void { if (changes.isOpen) { if (this.isOpen) { this.initObservationData(); this.modalComponent?.setVisible(true); } else { this.handleClose(); } } if (changes.activity) { if (this.activity) { this.activityId = this.activity?.id; } } } public handleClose(): void { if (!this.isOpen) { return; } this.modalComponent?.setVisible(false); if (this.modalComponent?.isModalVisible) { return; } this.modalClosed.emit(); this.clearObservationData(); } public validateActivityId(): boolean { const activitySelector = document.getElementById('activitySelector'); if (!this.activityId && this.modalComponent?.isModalVisible) { activitySelector?.classList.add('ng-invalid'); return false; } activitySelector?.classList.remove('ng-invalid'); this.note = this.activityId ? copy(this.carePlanService.getActivityById(this.activityId)?.note) : undefined; this.noteContent = this.note?.content ? this.note.content : ''; return true; } public doSave(): void { if (!this.form?.valid) { this.form?.resetForm(this.form?.form.value); // We make a fake reset, so the form is not seen as 'submitted' this.form?.form.markAsDirty(); // (Ensuring we still get prompted that we are going to discard changes if we try to leave the page) return; } if (!this.validateActivityId()) { const activitySelector = document.getElementById('activitySelector'); activitySelector?.scrollIntoView({behavior: 'smooth', block: 'center'}); return; } this.setActivityId(this.activityId); this.isSaving = true; const save = (this.observationType === 'ultrasound') ? this.saveUltrasound() : this.saveRegular(); save// TODO: create note too (Eller hvad? Skal vi se om der allerede er en på aktiviteten og hvis ja redigere den?) .pipe(finalize(() => this.isSaving = false)) .subscribe(() => { this.handleClose(); this.dataCardService.refresh(); // updating dataCard => update episodeOfCare => update carePlan & observations (see app.component) this.save.emit(); }); } private saveUltrasound(): Observable { if (this.dataCard) { return merge( this.dataCardService.update(this.dataCard.asDataCard, this.dataCardId, this.dataCardVersion), this.observationService.createUltrasoundObservations(this.observationDataUltrasound as SaveUltrasoundObservationsRequest) ) as unknown as Observable; } return of(); } private saveRegular(): Observable { return this.observationService.createRegularObservations(this.observationDataRegular as SaveRegularObservationsRequest); } private setActivityId(activityId?: string): void { if (!this.observationDataRegular || !this.observationDataUltrasound) { return; } this.observationDataRegular.activityId = activityId; this.observationDataUltrasound.activityId = activityId; } private initObservationData(): void { this.observationDataRegular = { fetuses: [...new Array(this.dataCard?.fetusCount || 1)].map((x, index) => ({id: `Foster ${index + 1}`})), }; this.observationDataUltrasound = { fetuses: [...new Array(this.dataCard?.fetusCount || 1)].map((x, index) => ({id: `Foster ${index + 1}`})), }; this.observationDataRegular = copy(this.observationDataRegular); this.observationDataUltrasound = copy(this.observationDataUltrasound); this.form?.form.markAsPristine(); this.form?.form.markAsUntouched(); } public isObservationDataInitialized(): boolean { return !!this.observationDataRegular && !!this.observationDataUltrasound; } private clearObservationData(): void { this.activityId = undefined; this.observationType = 'regular'; this.observationDataRegular = undefined; this.observationDataUltrasound = undefined; } }