-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUnenrolModal.tsx
More file actions
103 lines (95 loc) · 3.04 KB
/
Copy pathUnenrolModal.tsx
File metadata and controls
103 lines (95 loc) · 3.04 KB
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
96
97
98
99
100
101
102
103
import { useTranslation } from 'react-i18next';
import { useMutation } from '@apollo/client';
import { useNavigate } from 'react-router-dom';
// TODO: KK-300 Check how the cancel button should look
// TODO: KK-300 If the same, find a better/reusable location for this css module
import { toast } from 'react-toastify';
import { useDispatch } from 'react-redux';
import useGetPathname from '../../../common/route/utils/useGetPathname';
import unenrolOccurrenceMutation from '../mutations/unenrolOccurrenceMutation';
import {
UnenrolOccurrenceMutation,
UnenrolOccurrenceMutationVariables,
} from '../../api/generatedTypes/graphql';
import ConfirmModal from '../../../common/components/confirm/ConfirmModal';
import { saveChildEvents } from '../state/EventActions';
import getEventOrEventGroupOccurrenceRefetchQueries from '../getEventOrEventGroupOccurrenceRefetchQueries';
import AppConfig from '../../app/AppConfig';
interface UnenrolModalProps {
isOpen: boolean;
setIsOpen: (value: boolean) => void;
childId: string;
occurrenceId: string;
eventGroupId?: string;
}
const UnenrolModal = ({
isOpen,
setIsOpen,
childId,
occurrenceId,
eventGroupId,
}: UnenrolModalProps) => {
const navigate = useNavigate();
const { t } = useTranslation();
const dispatch = useDispatch();
const getPathname = useGetPathname();
const [unenrolOccurrence] = useMutation<
UnenrolOccurrenceMutation,
UnenrolOccurrenceMutationVariables
>(unenrolOccurrenceMutation, {
refetchQueries: getEventOrEventGroupOccurrenceRefetchQueries({
childId,
eventGroupId,
}),
awaitRefetchQueries: true,
onCompleted: (data) => {
if (data.unenrolOccurrence?.child?.occurrences.edges) {
dispatch(
saveChildEvents({
childId: data.unenrolOccurrence.child.id,
occurrences: data.unenrolOccurrence.child.occurrences,
})
);
}
navigate(getPathname(`/profile/child/${childId}`), { replace: true });
},
});
const unenrol = async () => {
try {
await unenrolOccurrence({
variables: {
input: {
occurrenceId: occurrenceId,
childId: childId,
},
},
});
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
// TODO: KK-280 Handle errors nicely
toast.error(t('registration.submitMutation.errorMessage'));
}
};
const confirmUnenrol = (answer: boolean) => {
if (answer === true) unenrol();
};
return (
<ConfirmModal
isOpen={isOpen}
setIsOpen={setIsOpen}
heading={t('event.cancellation.confirmationModal.heading')}
cancel={t('event.cancellation.confirmationModal.cancel.buttonText')}
ok={t('event.cancellation.confirmationModal.confirm.buttonText')}
answer={confirmUnenrol}
>
<p>
{t('enrollment.cancellation.enabledDescription', {
unerolHoursBeforeOccurrence:
AppConfig.enrolmentCancellationTimeLimitHours,
})}
</p>
</ConfirmModal>
);
};
export default UnenrolModal;