Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) O3-3133: Clicking the cancel button should not close the workspace #1815

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
import React from 'react';
import React, { useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { InlineLoading } from '@carbon/react';
import { FormEngine } from '@openmrs/openmrs-form-engine-lib';
import { type Visit } from '@openmrs/esm-framework';
import useFormSchema from '../hooks/useFormSchema';
import { launchPatientWorkspace, type DefaultWorkspaceProps } from '@openmrs/esm-patient-common-lib';
import FormError from './form-error.component';
import useFormSchema from '../hooks/useFormSchema';
import styles from './form-renderer.scss';
import { type DefaultWorkspaceProps } from '@openmrs/esm-patient-common-lib';

interface FormRendererProps {
formUuid: string;
patientUuid: string;
visit?: Visit;
encounterUuid?: string;
additionalProps?: Record<string, any>;
closeWorkspace: DefaultWorkspaceProps['closeWorkspace'];
closeWorkspaceWithSavedChanges: DefaultWorkspaceProps['closeWorkspaceWithSavedChanges'];
encounterUuid?: string;
formUuid: string;
patientUuid: string;
promptBeforeClosing: DefaultWorkspaceProps['promptBeforeClosing'];
visit?: Visit;
}

const FormRenderer: React.FC<FormRendererProps> = ({
formUuid,
patientUuid,
visit,
additionalProps,
closeWorkspace,
closeWorkspaceWithSavedChanges,
promptBeforeClosing,
encounterUuid,
additionalProps,
formUuid,
patientUuid,
promptBeforeClosing,
visit,
}) => {
const { t } = useTranslation();
const { schema, error, isLoading } = useFormSchema(formUuid);

const handleCloseForm = useCallback(() => {
closeWorkspace();
launchPatientWorkspace('clinical-forms-workspace');
}, [closeWorkspace]);

const handleMarkFormAsDirty = useCallback(
(isDirty: boolean) => promptBeforeClosing(() => isDirty),
[promptBeforeClosing],
);

if (isLoading) {
return (
<div className={styles.loaderContainer}>
Expand All @@ -43,7 +53,7 @@ const FormRenderer: React.FC<FormRendererProps> = ({
if (error) {
return (
<div className={styles.errorContainer}>
<FormError closeWorkspace={closeWorkspace} />
<FormError closeWorkspace={handleCloseForm} />
</div>
);
}
Expand All @@ -53,13 +63,13 @@ const FormRenderer: React.FC<FormRendererProps> = ({
{schema && (
<FormEngine
encounterUUID={encounterUuid}
patientUUID={patientUuid}
visit={visit}
formJson={schema}
handleClose={closeWorkspace}
onSubmit={closeWorkspaceWithSavedChanges}
handleClose={handleCloseForm}
markFormAsDirty={handleMarkFormAsDirty}
mode={additionalProps?.mode}
markFormAsDirty={(isDirty: boolean) => promptBeforeClosing(() => isDirty)}
onSubmit={closeWorkspaceWithSavedChanges}
patientUUID={patientUuid}
visit={visit}
/>
)}
</>
Expand Down
Loading