-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserSelectionModal.tsx
More file actions
218 lines (205 loc) · 6.17 KB
/
Copy pathUserSelectionModal.tsx
File metadata and controls
218 lines (205 loc) · 6.17 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"use client";
import {
Button,
Center,
rem,
SimpleGrid,
Stack,
Text,
UnstyledButton,
} from "@mantine/core";
import { useForm } from "@mantine/form";
import { useRef, useState } from "react";
import { useCounter } from "@mantine/hooks";
import { Carousel } from "@mantine/carousel";
import { Participant, PaymentMethodType } from "@/types";
import ParticipantAvatar from "./ParticipantAvatar";
import { useHover } from "@mantine/hooks";
import { useUpdateParticipant } from "@/api";
import Modal from "./Modal";
import { getHashedSessionId, recordConsent } from "@/lib/consent";
import { validateIban } from "@/lib/validateIban";
import PaymentForm from "@/components/PaymentForm";
type ParticipantItemProps = {
participant: Participant;
onSelect: (id: string) => void;
};
const ParticipantItem = ({ participant, onSelect }: ParticipantItemProps) => {
const { hovered, ref } = useHover();
return (
<UnstyledButton
ref={ref}
onClick={() => participant.id && onSelect(participant.id)}
>
<ParticipantAvatar
avatar={participant.avatar}
name={participant.name}
isSelected={hovered}
/>
</UnstyledButton>
);
};
type UserSelectionModalProps = {
opened: boolean;
participants: Participant[];
onSelect: (participantId: string) => void;
onClose?: () => void;
};
const UserSelectionModal = ({
opened,
participants,
onSelect,
onClose,
}: UserSelectionModalProps) => {
const [page, pageHandler] = useCounter(0, { min: 0, max: 1 });
const pageIncrementRef = useRef<(() => void) | null>(null);
const [selectedParticipant, setSelectedParticipant] =
useState<Participant | null>(null);
const updateParticipant = useUpdateParticipant();
const form = useForm<Participant>({
initialValues: {
avatar: { emoji: "", unified: "" },
name: "",
accountName: "",
selectedPaymentMethod: PaymentMethodType.Iban,
paymentMethod: { iban: "", paypal: "" },
},
validate: (values) => {
if (page === 1) {
return {
"paymentMethod.iban":
values.selectedPaymentMethod === PaymentMethodType.Iban
? validateIban(values.paymentMethod.iban)
: null,
};
}
return {};
},
});
const hasPaymentDetails = (p: Participant): boolean => {
switch (p.selectedPaymentMethod) {
case PaymentMethodType.Cash:
return true;
case PaymentMethodType.Iban:
return validateIban(p.paymentMethod.iban) === null;
case PaymentMethodType.Paypal:
return p.paymentMethod.paypal.trim().length > 0;
default:
return false;
}
};
const handleParticipantSelect = (participantId: string) => {
const participant = participants.find((p) => p.id === participantId);
if (!participant) return;
if (hasPaymentDetails(participant)) {
onSelect(participantId);
return;
}
setSelectedParticipant(participant);
form.setValues(participant);
pageIncrementRef.current?.();
};
const handleSave = async () => {
if (!selectedParticipant?.id) return;
if (form.validate().hasErrors) return;
const needsConsent =
form.values.selectedPaymentMethod === PaymentMethodType.Iban ||
form.values.selectedPaymentMethod === PaymentMethodType.Paypal;
if (needsConsent) {
const hashed = await getHashedSessionId();
recordConsent(selectedParticipant.id, hashed);
}
await updateParticipant.mutateAsync({
...form.values,
id: selectedParticipant.id,
});
onSelect(selectedParticipant.id);
};
const handleSkip = () => {
if (selectedParticipant?.id) onSelect(selectedParticipant.id);
};
return (
<Modal
opened={opened}
onClose={onClose}
page={page}
pageHandler={pageHandler}
maxPage={1}
confirmPage={-1}
onConfirmClick={() => {}}
form={form}
pageIncrementRef={pageIncrementRef}
footerContent={(currentPage) => {
if (currentPage === 0) return null;
const requiresConsent =
form.values.selectedPaymentMethod === PaymentMethodType.Iban ||
form.values.selectedPaymentMethod === PaymentMethodType.Paypal;
return (
<Stack gap="xs" pb="md">
{requiresConsent && (
<Text size="xs" c="dimmed" ta="center">
By saving, you consent to your payment details being stored and visible to group members
</Text>
)}
<Button
onClick={handleSave}
loading={updateParticipant.isPending}
loaderProps={{ type: "dots" }}
fullWidth
>
Save
</Button>
<Center>
<Text
size="sm"
c="dimmed"
style={{ cursor: "pointer", textDecoration: "underline" }}
onClick={handleSkip}
>
Skip for now
</Text>
</Center>
</Stack>
);
}}
>
{/* Step 1: Who are you? */}
<Carousel.Slide>
<Stack>
<Center>
<Stack gap={rem(2)} align="center">
<Text fw={500}>Who are you?</Text>
<Text size="sm" c="dimmed">
Choose your name to personalise your experience
</Text>
</Stack>
</Center>
<SimpleGrid cols={3} spacing={0} pb="xl">
{participants.map((participant) => (
<ParticipantItem
key={participant.id}
participant={participant}
onSelect={handleParticipantSelect}
/>
))}
</SimpleGrid>
</Stack>
</Carousel.Slide>
{/* Step 2: Payment details */}
<Carousel.Slide>
<Stack gap="md">
<Center>
<Stack gap={rem(2)} align="center" pb="lg">
<Text fw={500}>Your payment details</Text>
<Text size="sm" c="dimmed">
So others know how to pay you back
</Text>
</Stack>
</Center>
<PaymentForm form={form} />
</Stack>
</Carousel.Slide>
</Modal>
);
};
export default UserSelectionModal;