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

Enhance error handling and user feedback in ActionButtons component #235

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions src/components/action-buttons/action-buttons.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ function ActionButtons({ schema, t }: ActionButtonsProps) {
const { form, mutate } = useForm(formUuid);
const [status, setStatus] = useState<Status>('idle');
const [showUnpublishModal, setShowUnpublishModal] = useState(false);
const [showError, setShowError] = useState(false);
const [errorMessage, setErrorMessage] = useState('');

const launchUnpublishModal = () => {
setShowUnpublishModal(true);
};

async function handlePublish() {
setStatus('publishing');
setShowError(false);
try {
await publishForm(form.uuid);

Expand All @@ -49,6 +52,8 @@ function ActionButtons({ schema, t }: ActionButtonsProps) {
subtitle: error?.message,
});
setStatus('error');
setShowError(true);
setErrorMessage(error.message);
}
}
}
Expand Down Expand Up @@ -86,6 +91,17 @@ function ActionButtons({ schema, t }: ActionButtonsProps) {
<SaveFormModal form={form} schema={schema} />

<>
{/* Error message display */}
{showError && (
<div className={styles.errorMessage}>
<p>{errorMessage}</p>
{/* Retry button */}
<Button kind="secondary" onClick={handlePublish}>
{t('retry', 'Retry')}
</Button>
</div>
)}

{form && !form.published ? (
<Button kind="secondary" onClick={handlePublish} disabled={status === 'publishing'}>
{status === 'publishing' && !form?.published ? (
Expand Down
21 changes: 15 additions & 6 deletions src/components/form-renderer/form-renderer.component.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import { useTranslation } from 'react-i18next';
import { Button, InlineLoading, Tile } from '@carbon/react';
import { Button, Loading, Tile } from '@carbon/react';
import { type OHRIFormSchema, OHRIForm } from '@openmrs/openmrs-form-engine-lib';
import styles from './form-renderer.scss';

Expand All @@ -16,6 +16,18 @@ interface FormRendererProps {
schema: OHRIFormSchema;
}

// enhance loading state with a progress bar or an animated loader
const LoadingUI = () => {
const { t } = useTranslation();
return (
<div className={styles.loadingContainer}>
{/* animated loading indicator */}
<Loading description={t('loading', 'Loading')} withOverlay={false} />
<p className={styles.loadingText}>{t('loadingForm', 'Loading form...')}</p>
</div>
);
};

const FormRenderer: React.FC<FormRendererProps> = ({ isLoading, schema }) => {
const { t } = useTranslation();

Expand Down Expand Up @@ -58,11 +70,8 @@ const FormRenderer: React.FC<FormRendererProps> = ({ isLoading, schema }) => {
}, [schema]);

if (isLoading) {
return (
<div className={styles.loadingContainer}>
<InlineLoading className={styles.loader} description={t('loading', 'Loading') + '...'} />
</div>
);
// render the enhanced loading UI
return <LoadingUI />;
}

return (
Expand Down