-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWithdrawMySignupModal.tsx
More file actions
131 lines (118 loc) · 4.82 KB
/
WithdrawMySignupModal.tsx
File metadata and controls
131 lines (118 loc) · 4.82 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import AppRootContext from 'AppRootContext';
import { Signup, Event, SignupState, SignupAutomationMode, Run } from 'graphqlTypes.generated';
import { useCallback, useContext, useMemo, useState } from 'react';
import Modal from 'react-bootstrap4-modal';
import { Trans, useTranslation } from 'react-i18next';
import { parseSignupRounds } from 'SignupRoundUtils';
import { DateTime } from 'luxon';
import { BootstrapFormCheckbox, ErrorDisplay } from '@neinteractiveliterature/litform';
import { WithdrawMySignupDocument } from './mutations.generated';
import { useApolloClient } from '@apollo/client/react';
import { useRevalidator } from 'react-router';
export type WithdrawMySignupModalProps = {
close: () => void;
event: Pick<Event, 'title'>;
signup: Pick<Signup, 'id' | 'state' | 'counted'>;
run: Pick<Run, 'id'>;
signupRounds: Parameters<typeof parseSignupRounds>[0];
};
export function WithdrawMySignupModal({ close, event, run, signup, signupRounds }: WithdrawMySignupModalProps) {
const { signupMode, signupAutomationMode } = useContext(AppRootContext);
const { t } = useTranslation();
const [checked, setChecked] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<Error>();
const client = useApolloClient();
const revalidator = useRevalidator();
const currentRound = useMemo(() => {
const parsedRounds = parseSignupRounds(signupRounds);
const now = DateTime.local();
return parsedRounds.find((round) => round.timespan.includesTime(now));
}, [signupRounds]);
const requiresCheckbox = useMemo(() => signup.state === SignupState.Confirmed, [signup.state]);
const withdraw = async () => {
setBusy(true);
try {
await client.mutate({ mutation: WithdrawMySignupDocument, variables: { runId: run.id } });
await client.resetStore();
revalidator.revalidate();
close();
} catch (error) {
setBusy(false);
setError(error instanceof Error ? error : undefined);
}
};
const withdrawPrompt = useMemo(() => {
if (signup && signup.state === SignupState.Confirmed && !signup.counted) {
return t('events.withdrawPrompt.notCounted', { eventTitle: event.title });
} else if (
signupAutomationMode === SignupAutomationMode.RankedChoice &&
typeof currentRound?.maximum_event_signups === 'number'
) {
return t('events.withdrawPrompt.duringLimitedRankedChoiceSignupRoundCounted', { eventTitle: event.title });
} else if (signupMode === 'moderated') {
return (
<Trans i18nKey="events.withdrawPrompt.moderatedSignup" values={{ eventTitle: event.title }}>
<p>
<strong>
If you’re thinking of signing up for a different event instead, please go to that event’s page and request
to sign up for it.
</strong>{' '}
If the request is accepted, you’ll automatically be withdrawn from this event.
</p>
<p className="mb-0">Are you sure you want to withdraw from {{ eventTitle: event.title }}?</p>
</Trans>
);
} else {
return t('events.withdrawPrompt.selfServiceSignup', { eventTitle: event.title });
}
}, [event.title, signup, signupMode, signupAutomationMode, t, currentRound?.maximum_event_signups]);
return (
<Modal visible>
<div className="modal-header">
<div className="lead">{t('events.withdrawPrompt.title')}</div>
</div>
<div className="modal-body">
<div>{withdrawPrompt}</div>
{requiresCheckbox && (
<div className="mt-2">
<BootstrapFormCheckbox
type="checkbox"
label={<Trans i18nKey="events.withdrawPrompt.checkboxLabel" values={{ eventTitle: event.title }} />}
checked={checked}
onChange={(event) => setChecked(event.target.checked)}
/>
</div>
)}
<ErrorDisplay graphQLError={error} />
</div>
<div className="modal-footer">
<button className="btn btn-secondary" disabled={busy} onClick={close}>
{t('buttons.cancel')}
</button>
<button className="btn btn-primary" disabled={busy || (requiresCheckbox && !checked)} onClick={withdraw}>
{t('buttons.confirm')}
</button>
</div>
</Modal>
);
}
export function useWithdrawMySignupModal() {
const [props, setProps] = useState<WithdrawMySignupModalProps>();
const openModal = useCallback((newProps: Omit<WithdrawMySignupModalProps, 'close'>) => {
setProps((prevProps) => {
if (prevProps != null) {
throw new Error('Modal is already open');
}
return { ...newProps, close: () => setProps(undefined) };
});
}, []);
const Component = () => {
if (props == null) {
return <></>;
} else {
return <WithdrawMySignupModal {...props} />;
}
};
return { openModal, Component };
}