-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCreateAppointment.jsx
95 lines (80 loc) · 2.96 KB
/
CreateAppointment.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {Outlet, useLocation} from 'react-router';
import Card from 'components/Card';
import ErrorBoundary from 'components/Errors/ErrorBoundary';
import FormDisplay from 'components/FormDisplay';
import {LiteralsProvider} from 'components/Literal';
import Loader from 'components/Loader';
import {SessionTrackerModal} from 'components/Sessions';
import {checkMatchesPath} from 'components/utils/routers';
import useFormContext from 'hooks/useFormContext';
import useGetOrCreateSubmission from 'hooks/useGetOrCreateSubmission';
import useSessionTimeout from 'hooks/useSessionTimeout';
import {AppointmentConfigContext} from '../Context';
import AppointmentProgress from './AppointmentProgress';
import {CreateAppointmentState} from './CreateAppointmentState';
import {APPOINTMENT_STEP_PATHS} from './steps';
const useIsConfirmation = () => {
// useMatch requires absolute paths... and react-router are NOT receptive to changing that.
const {pathname} = useLocation();
return checkMatchesPath(pathname, 'bevestiging');
};
const CreateAppointment = () => {
const form = useFormContext();
const {pathname} = useLocation();
const skipSubmissionCreation = useIsConfirmation();
const {
isLoading,
error,
submission,
clear: clearSubmission,
} = useGetOrCreateSubmission(form, skipSubmissionCreation);
if (error) throw error;
const [, expiryDate, resetSession] = useSessionTimeout();
const supportsMultipleProducts = form?.appointmentOptions.supportsMultipleProducts ?? false;
const currentStep =
APPOINTMENT_STEP_PATHS.find(relPath => checkMatchesPath(pathname, relPath)) ||
APPOINTMENT_STEP_PATHS[0];
const reset = () => {
clearSubmission();
resetSession();
};
const progressIndicator = form.showProgressIndicator ? (
<AppointmentProgress title={form.name} currentStep={currentStep} />
) : null;
return (
<AppointmentConfigContext.Provider value={{supportsMultipleProducts}}>
<SessionTrackerModal expiryDate={expiryDate}>
<CreateAppointmentState
currentStep={currentStep}
submission={submission}
resetSession={reset}
>
<FormDisplay progressIndicator={progressIndicator}>
<Wrapper title={form.name}>
<ErrorBoundary>
{isLoading ? (
<Loader modifiers={['centered']} />
) : (
<LiteralsProvider literals={form.literals}>
<Outlet />
</LiteralsProvider>
)}
</ErrorBoundary>
</Wrapper>
</FormDisplay>
</CreateAppointmentState>
</SessionTrackerModal>
</AppointmentConfigContext.Provider>
);
};
CreateAppointment.propTypes = {};
const Wrapper = ({children, ...props}) => {
const isConfirmation = useIsConfirmation();
if (isConfirmation) return <>{children}</>;
return (
<Card mobileHeaderHidden {...props}>
{children}
</Card>
);
};
export default CreateAppointment;