Skip to content

Commit

Permalink
♻️ [open-formulieren/open-forms#4929] Remove need to track appointmen…
Browse files Browse the repository at this point in the history
…t processing error

We can grab the error from the state now and make it
disappear when a new submit attempt is made with
local component state rather than needing to use
or update the context.
  • Loading branch information
sergei-maertens committed Jan 17, 2025
1 parent 8bb788d commit 537d743
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default {
statusUrl: {control: false},
},
args: {
onFailure: fn(),
onConfirmed: fn(),
},
parameters: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import {useCreateAppointmentContext} from './CreateAppointmentState';

const Confirmation = () => {
const [params] = useSearchParams();
const {reset, setProcessingError} = useCreateAppointmentContext();
const {reset} = useCreateAppointmentContext();
const statusUrl = params.get('statusUrl');
if (!statusUrl) throw new Error('Missing statusUrl param');
return (
<ConfirmationView returnTo="../overzicht" onFailure={setProcessingError} onConfirmed={reset} />
);
return <ConfirmationView returnTo="../overzicht" onConfirmed={reset} />;
};

Confirmation.propTypes = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ describe('Create appointment status checking', () => {
// wait for summary page to be rendered again
await screen.findByText('Check and confirm', undefined, {timeout: 2000});
expect(screen.getByText('Computer says no.')).toBeVisible();

// submitting again causes error message to vanish
for (const checkbox of screen.getAllByRole('checkbox')) {
await user.click(checkbox);
}
const submitButton2 = screen.getByRole('button', {name: 'Confirm'});
await user.click(submitButton2);
expect(screen.queryByText('Computer says no.')).toBeNull();
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export const buildContextValue = ({
appointmentErrors = {initialTouched: {}, initialErrors: {}},
setAppointmentErrors = () => {},
resetSession = () => {},
processingError = '',
setProcessingError = () => {},
}) => {
const submittedSteps = Object.keys(appointmentData).filter(
subObject => Object.keys(subObject).length
Expand Down Expand Up @@ -55,16 +53,13 @@ export const buildContextValue = ({
submitStep: values => setAppointmentData({...appointmentData, [currentStep]: values}),
setErrors: setAppointmentErrors,
stepErrors: {initialTouched: stepInitialTouched, initialErrors: stepInitialErrors},
processingError,
setProcessingError,
clearStepErrors: () => {
const newInitialErrors = produce(initialErrors, draft => {
errorKeys.forEach(key => delete draft[key]);
});
setAppointmentErrors({initialTouched, initialErrors: newInitialErrors});
},
reset: () => {
setProcessingError('');
setAppointmentData({});
resetSession();
},
Expand All @@ -77,7 +72,6 @@ export const CreateAppointmentState = ({currentStep, submission, resetSession, c
initialTouched: {},
initialErrors: {},
});
const [processingError, setProcessingError] = useState('');

// check if the session is expired
useSessionTimeout();
Expand All @@ -90,8 +84,6 @@ export const CreateAppointmentState = ({currentStep, submission, resetSession, c
appointmentErrors,
setAppointmentErrors,
resetSession,
processingError,
setProcessingError,
});

return (
Expand Down
12 changes: 8 additions & 4 deletions src/components/appointments/CreateAppointment/Summary.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Form, Formik} from 'formik';
import {useContext, useState} from 'react';
import {FormattedMessage, useIntl} from 'react-intl';
import {createSearchParams, useNavigate} from 'react-router-dom';
import {createSearchParams, useLocation, useNavigate} from 'react-router-dom';
import {useAsync} from 'react-use';

import {ConfigContext} from 'Context';
Expand Down Expand Up @@ -60,9 +60,10 @@ const getErrorsNavigateTo = errors => {
const Summary = () => {
const intl = useIntl();
const {baseUrl} = useContext(ConfigContext);
const {state: routerState} = useLocation();
const navigate = useNavigate();
const {appointmentData, submission, setErrors, processingError, setProcessingError} =
useCreateAppointmentContext();
const {appointmentData, submission, setErrors} = useCreateAppointmentContext();
const [submitting, setSubmitting] = useState(false);
const [submitError, setSubmitError] = useState(null);
useTitle(
intl.formatMessage({
Expand Down Expand Up @@ -162,7 +163,7 @@ const Summary = () => {
* Submit the appointment data to the backend.
*/
const onSubmit = async statementValues => {
setProcessingError('');
setSubmitting(true);
let appointment;
try {
appointment = await createAppointment(baseUrl, submission, appointmentData, statementValues);
Expand All @@ -176,6 +177,8 @@ const Summary = () => {
}
setSubmitError(e);
return;
} finally {
setSubmitting(false);
}
// TODO: store details in sessionStorage instead, to survive hard refreshes
navigate(
Expand All @@ -191,6 +194,7 @@ const Summary = () => {
);
};

const processingError = submitting ? '' : routerState?.errorMessage;
return (
<>
<CardTitle
Expand Down

0 comments on commit 537d743

Please sign in to comment.