import { NgZone, Component, HostListener, OnDestroy, OnInit } from '@angular/core'; import { registerLocaleData } from '@angular/common'; import { Form } from '@openmrs/ngx-formentry'; import { Observable, forkJoin, from, throwError, of, Subscription } from 'rxjs'; import { catchError, concatAll, filter, map, mergeMap, take } from 'rxjs/operators'; import { OpenmrsEsmApiService } from '../openmrs-api/openmrs-esm-api.service'; import { FormSchemaService } from '../form-schema/form-schema.service'; import { FormSubmissionService } from '../form-submission/form-submission.service'; import { EncounterResourceService } from '../openmrs-api/encounter-resource.service'; import { ConceptReferenceRange, Encounter, EncounterCreate, FormSchema, Identifier, Order } from '../types'; import { showSnackbar, createGlobalStore, showModal } from '@openmrs/esm-framework'; import { PatientPreviousEncounterService } from '../openmrs-api/patient-previous-encounter.service'; import { SingleSpaPropsService } from '../single-spa-props/single-spa-props.service'; import { CreateFormParams, FormCreationService } from '../form-creation/form-creation.service'; import { TranslateService } from '@ngx-translate/core'; import { ProgramResourceService } from '../openmrs-api/program-resource.service'; import { FormDataSourceService } from '../form-data-source/form-data-source.service'; import { PatientResourceService } from '../openmrs-api/patient-resource.service'; import { VisitResourceService } from '../openmrs-api/visit-resource.service'; import { ConceptReferenceRangeResourceService } from '../openmrs-api/concept-reference-range-resource.service'; import { getNumericQuestionConcepts } from '../form-schema/concept-reference-ranges'; type FormState = | 'initial' | 'loading' | 'loadingError' | 'ready' | 'readyWithValidationErrors' | 'submitting' | 'submitted' | 'submissionError'; const store = createGlobalStore>('ampath-form-state', {}); @Component({ selector: 'my-app-fe-wrapper', templateUrl: './fe-wrapper.component.html', styleUrls: ['./fe-wrapper.component.scss'], standalone: false, }) export class FeWrapperComponent implements OnInit, OnDestroy { private launchFormSubscription?: Subscription; private workspaceDirtyStateListenerSubscription: Subscription; private unsubscribeStore: () => void | undefined; public formUuid: string; public form: Form; public loadingError?: string; public labelMap: Record = {}; public formState: FormState = 'initial'; public showDiscardSubmitButtons: boolean = true; public language: string = (window as any).i18next.language.substring(0, 2).toLowerCase(); public constructor( private readonly openmrsApi: OpenmrsEsmApiService, private readonly formSchemaService: FormSchemaService, private readonly encounterResourceService: EncounterResourceService, private readonly formSubmissionService: FormSubmissionService, private readonly patientPreviousEncounter: PatientPreviousEncounterService, private readonly formCreationService: FormCreationService, private readonly singleSpaPropsService: SingleSpaPropsService, private readonly translateService: TranslateService, private readonly ngZone: NgZone, private readonly programService: ProgramResourceService, private readonly formDataSourceService: FormDataSourceService, private readonly patientResourceService: PatientResourceService, private readonly visitResourceService: VisitResourceService, private readonly conceptReferenceRangeResourceService: ConceptReferenceRangeResourceService, ) {} public ngOnInit() { this.unsubscribeStore = store.subscribe((value) => { this.formState = value[this.formUuid]; }); this.changeState('initial'); this.launchForm(); } public ngOnDestroy() { this.unsubscribeStore && this.unsubscribeStore(); this.launchFormSubscription?.unsubscribe(); this.workspaceDirtyStateListenerSubscription?.unsubscribe(); } public launchForm() { const language = window.i18next?.language?.substring(0, 2) ?? ''; this.translateService.addLangs([language]); this.translateService.use(language); this.changeState('loading'); this.showDiscardSubmitButtons = this.singleSpaPropsService.getProp('showDiscardSubmitButtons') ?? true; this.launchFormSubscription?.unsubscribe(); this.launchFormSubscription = this.loadAllFormDependencies() .pipe( take(1), map((createFormParams) => from(this.formCreationService.initAndCreateForm(createFormParams))), concatAll(), ) .subscribe({ next: (form) => { this.form = form; if (Boolean(form.schema?.conceptReferences)) { this.labelMap = Object.entries( form.schema?.conceptReferences as { [reference: string]: { uuid?: string | null; display?: string | null }; }, ).reduce((acc: { [reference: string]: string }, current) => { if (current && current[0] && current[1]?.display) { acc[current[0]] = current[1].display; } return acc; }, {}); } this.changeState('ready'); this.setupWorkspaceDirtyStateListener(); }, error: (err) => { // TODO: Improve error handling. this.loadingError = this.translateService.instant('errorLoadingForm'); console.error('Error rendering form', err); this.changeState('loadingError'); }, }); } private loadAllFormDependencies(): Observable { this.formUuid = this.singleSpaPropsService.getPropOrThrow('formUuid'); const encounterUuid = this.singleSpaPropsService.getPropOrThrow('encounterUuid'); const patient = this.singleSpaPropsService.getPropOrThrow('patient'); const identifiers = this.formDataSourceService.getPatientObject(patient)?.identifiers ?? []; const locale = window.i18next?.language?.substring(0, 2) ?? ''; this.translateService.addLangs([locale]); this.translateService.use(locale); import( /* webpackInclude: /locales[\/\\][a-z]{2}\.js$/ */ /* webpackChunkName: "./assets/l10n/locales/[request]"*/ /* webpackMode: "lazy" */ `../../../node_modules/@angular/common/locales/${locale}.js` ) .then((module) => registerLocaleData(module.default)) .catch((err) => { console.warn(`Could not load Angular locale data for "${locale}":`, err); }); return forkJoin({ formSchema: this.fetchCompiledFormSchema(this.formUuid, locale).pipe(take(1)), session: this.openmrsApi.getCurrentSession().pipe(take(1)), encounter: encounterUuid ? this.getEncounterToEdit(encounterUuid).pipe(take(1)) : of(null), patientIdentifiers: of>(identifiers), }).pipe( mergeMap((result) => forkJoin({ previousEncounter: this.loadPatientPreviousEncounters(result.formSchema).pipe(take(1)), conceptReferenceRanges: this.loadConceptReferenceRanges(result.formSchema).pipe(take(1)), }).pipe(map((schemaDependentData) => ({ ...result, ...schemaDependentData }))), ), catchError((err) => throwError( () => new Error(this.translateService.instant('errorFetchingFormData').replace('{detail}', JSON.stringify(err))), ), ), ); } private fetchCompiledFormSchema(uuid: string, language: string): Observable { return this.formSchemaService.getFormSchemaByUuid(uuid, language).pipe(take(1)); } private getEncounterToEdit(encounterUuid: string): Observable { return this.encounterResourceService.getEncounterByUuid(encounterUuid); } private loadPatientPreviousEncounters(formSchema: FormSchema): Observable { const patientUuid = this.singleSpaPropsService.getProp('patientUuid'); return from(this.patientPreviousEncounter.getPreviousEncounter(formSchema.encounterType?.uuid, patientUuid)); } private loadConceptReferenceRanges(formSchema: FormSchema): Observable> { const patientUuid = this.singleSpaPropsService.getProp('patientUuid'); return this.conceptReferenceRangeResourceService.getConceptReferenceRanges( patientUuid, getNumericQuestionConcepts(formSchema), ); } private handleFormSubmission() { this.changeState('submitting'); this.patientResourceService.validateIdentifiers(this.form).subscribe((resp) => { if (resp.length > 0) { this.changeState('readyWithValidationErrors'); showSnackbar({ title: this.translateService.instant('patientIdentifierDuplication'), subtitle: this.translateService.instant('patientIdentifierDuplicationDescription'), kind: 'error', isLowContrast: false, }); } else { this.formSubmissionService.submitPayload(this.form).subscribe({ next: ({ encounter }) => { this.onPostResponse(encounter); if (encounter?.uuid) { this.encounterResourceService .getEncounterByUuid(encounter.uuid) .pipe(take(1)) .subscribe((encounter) => { if (Array.isArray(encounter?.orders)) { const submittedOrders = encounter.orders.filter(({ auditInfo }) => !auditInfo.dateVoided); this.showLabOrdersNotification(submittedOrders); } }); } this.programService.handlePatientCareProgram(this.form, encounter.uuid); showSnackbar({ isLowContrast: true, kind: 'success', subtitle: this.translateService.instant('formSubmittedSuccessfully'), title: this.form.schema.display ?? this.form.schema.name, timeoutInMs: 5000, }); this.changeState('submitted'); this.closeFormWithSavedChanges(); }, error: (error: Error) => { this.changeState('submissionError'); showSnackbar({ isLowContrast: true, kind: 'error', subtitle: this.translateService.instant('formSubmissionFailed').replace('{error}', error.message), title: this.form.schema.display ?? this.form.schema.name, timeoutInMs: 5000, }); }, }); } }); } private modifyVisitDate(visitUuid: string, visitStartDatetime: string, visitStopDatetime: string) { const dispose = showModal('modify-visit-date-dialog', { onDiscard: () => { dispose(); }, onConfirmation: () => { this.visitResourceService .updateVisitDates(visitUuid, visitStartDatetime, visitStopDatetime) .subscribe(() => this.handleFormSubmission()); dispose(); }, }); } private validateEncounterDatetimeWithVisit(encounterCreate: EncounterCreate) { const visitUuid = this.singleSpaPropsService.getProp('visitUuid'); const visitStartDatetime = this.singleSpaPropsService.getProp('visitStartDatetime'); const visitStopDatetime = this.singleSpaPropsService.getProp('visitStopDatetime'); if (encounterCreate.uuid) { if (visitStartDatetime && new Date(encounterCreate.encounterDatetime) < new Date(visitStartDatetime)) { this.modifyVisitDate(visitUuid, encounterCreate.encounterDatetime, visitStopDatetime); return false; } else if (visitStopDatetime && new Date(encounterCreate.encounterDatetime) > new Date(visitStopDatetime)) { this.modifyVisitDate(visitUuid, visitStartDatetime, encounterCreate.encounterDatetime); return false; } } return true; } public onSubmit() { if (!this.validateForm()) { return; } const encounterToSubmit = this.formSubmissionService.buildEncounterPayload(this.form); const isEncounterDatetimeValid = this.validateEncounterDatetimeWithVisit(encounterToSubmit); if (isEncounterDatetimeValid) { this.handleFormSubmission(); } } /** * Validates that the form is valid (and shows error messages if that is not the case). * Returns the validation result. * @returns `true` if the form is valid; `false` if not. */ private validateForm(): boolean { if (!this.form.valid) { this.form.markInvalidControls(this.form.rootNode); this.form.showErrors = true; this.changeState('readyWithValidationErrors'); } this.onValidate(this.form.valid); return this.form.valid; } public showLabOrdersNotification(submittedOrders: Array = []) { const orders = submittedOrders.map((order, index) => ` ${index + 1} : ${order.display} : ${order.orderNumber}`).join() ?? ''; if (orders.length) { showSnackbar({ title: 'Lab order(s) generated', kind: 'success', isLowContrast: true, subtitle: orders, }); } } public closeForm() { const closeWorkspace = this.singleSpaPropsService.getPropOrThrow('closeWorkspace'); closeWorkspace(); } public closeFormWithSavedChanges() { const closeWorkspaceWithSavedChanges = this.singleSpaPropsService.getPropOrThrow('closeWorkspaceWithSavedChanges'); closeWorkspaceWithSavedChanges(); } public onPostResponse(encounter: Encounter | undefined) { const handlePostResponse = this.singleSpaPropsService.getProp('handlePostResponse'); if (handlePostResponse && typeof handlePostResponse === 'function') handlePostResponse(encounter); } public onValidate(valid: boolean): void { const handleOnValidate = this.singleSpaPropsService.getProp('handleOnValidate'); if (handleOnValidate && typeof handleOnValidate === 'function') handleOnValidate(valid); } @HostListener('window:ampath-form-action', ['$event']) onFormAction(event) { this.ngZone.run(() => { const formUuid = this.singleSpaPropsService.getPropOrThrow('formUuid'); const patientUuid = this.singleSpaPropsService.getPropOrThrow('patientUuid'); if (event.detail?.formUuid === formUuid && event.detail?.patientUuid === patientUuid) { switch (event.detail?.action) { case 'onSubmit': this.onSubmit(); break; case 'validateForm': this.validateForm(); break; default: break; } } }); } private changeState(state) { if (state != this.formState) { const obj = {}; obj[this.formUuid] = state; store.setState(obj); } } /** * Sets up a listener to mark the workspace as dirty when form values change. * * This method observes the root form control for changes. When a change occurs, * it sets the 'promptBeforeClosing' flag to true, indicating that the user * should be prompted before closing the workspace due to unsaved changes. */ private setupWorkspaceDirtyStateListener(): void { const promptBeforeClosing = this.singleSpaPropsService.getPropOrThrow('promptBeforeClosing'); this.workspaceDirtyStateListenerSubscription = this.form.rootNode.control.valueChanges .pipe( map(() => this.form.rootNode.control.dirty), filter((isDirty) => isDirty), take(1), ) .subscribe(() => { promptBeforeClosing(() => true); }); } }