-
Notifications
You must be signed in to change notification settings - Fork 3
/
profile-modal.tsx
257 lines (246 loc) · 8 KB
/
profile-modal.tsx
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
export const ProfileModal: React.FC<{
setInteraction: () => void;
interaction: ConversationInterface;
}> = ({ interaction }) => {
const [editing, setEditing] = useState<boolean>(false);
const dispatch = useDispatch();
const {
websocket,
currentConnectionId,
connectedUsers,
inviteAssistant,
} = useWebsocketContext();
const {
setCurrentUser,
currentUser,
availableUsers,
} = useCurrentUserContext();
const { hideModal } = useModalContext();
const [customerName, setCustomerName] = useState("");
const [errors, setErrors] = useState<{ customerName: string; email: string }>(
{ customerName: "", email: "" }
);
const merchantInvited = useSelector(inviteAssistantSelector);
useEffect(() => {
if (customerName && !customerName) {
setErrors({ ...errors, customerName: "Must enter a name" });
return;
}
setErrors({ customerName: "", email: "" });
}, [customerName]);
const setCustomerNameHandler = (
event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
) => {
setCustomerName(event.target.value);
};
const createProfileHandler = async () => {
const newUser = await createProfile(
`https://i.pravatar.cc/150?u=${interaction.id}`,
customerName,
interaction.id,
store.user_id || ""
);
setCurrentUser(newUser);
// We need to connect to the websocket, and tell peers to update the conversation. There will be better ways to do this...
setTimeout(() => {
websocket.send(
JSON.stringify({
storeId: store.id,
connectionId: undefined,
senderConnectionId: currentConnectionId,
action: "sendmessage",
type: MessageType.UPDATE,
userId: newUser.id,
conversationId: interaction.id,
} as Message)
);
}, 3000);
};
const selectProfile = async (profileId: string) => {
setCurrentUser(
interaction.profiles.find((profile) => profile.id === profileId)
);
hideModal();
};
return (
<>
{currentUser?.id && (
<>
<h3 style={{ marginBottom: 16 }}>
There are {connectedUsers.length + 1} users online.
</h3>
<div style={{ width: "100%", overflow: "auto" }}>
<div
style={{
display: "flex",
marginBottom: 32,
width: (availableUsers.length + 2) * 110,
padding: "10px 0",
}}
>
<UserStyled>
<div
style={{
background: "green",
width: 16,
height: 16,
borderRadius: "50%",
position: "absolute",
top: -8,
right: -8,
border: `solid 2px ${store.textColor}`,
}}
/>
<UserSVG style={{ marginBottom: 8 }} />
<h2 style={{ height: 60 }}>{currentUser?.name.slice(0, 8)}</h2>
<p>YOU</p>
</UserStyled>
<UserStyledSmall
selected={merchantInvited}
onClick={() => {
if (merchantInvited) return;
inviteAssistant();
dispatch(
createToast({
type: "success",
message: `You have invited ${assistant.name} to the conversation`,
action: () => {},
icon: <></>,
})
);
}}
key={assistant?.id}
>
<div
style={{
background: "grey",
width: 16,
height: 16,
borderRadius: "50%",
position: "absolute",
top: -8,
right: -8,
border: `solid 2px ${store.textColor}`,
}}
/>
{
<img
src={assistant.profilePic}
style={{ width: 50, height: 50, borderRadius: "50%" }}
/>
}
<h2 style={{ height: 60 }}>{assistant?.name}</h2>
<p>Merchant</p>
<span>{!merchantInvited ? "(invite)" : "(invited)"}</span>
</UserStyledSmall>
{connectedUsers.map((profile: UserProfile) => (
<UserStyled
selected={profile.id === currentUser?.id}
key={profile.id}
>
<div
style={{
background: "green",
width: 16,
height: 16,
borderRadius: "50%",
position: "absolute",
top: -8,
right: -8,
border: `solid 2px ${store.textColor}`,
}}
/>
<UserSVG style={{ marginBottom: 8 }} />
<h2 style={{ height: 60 }}>{profile.name.slice(0, 8)}</h2>
<p>FRIEND</p>
</UserStyled>
))}
</div>
</div>
</>
)}
<>
<h3 style={{ marginBottom: 8 }}>
Please select your profile or create a new one.
</h3>
<div style={{ width: "100%", overflow: "auto" }}>
<div
style={{
display: "flex",
marginBottom: 32,
width: availableUsers.length * 110,
padding: "10px 0",
}}
>
{availableUsers
.filter(
(user) => !connectedUsers.map((a) => a.id).includes(user.id)
)
.filter((user) => user.id !== currentUser?.id)
.slice(0, 5)
.map((profile: UserProfile) => (
<UserStyled
onClick={() => selectProfile(profile.id)}
selected={profile.id === currentUser?.id}
key={profile.id}
>
<div
style={{
background: "grey",
width: 16,
height: 16,
borderRadius: "50%",
position: "absolute",
top: -8,
right: -8,
border: `solid 2px ${store.textColor}`,
}}
/>
<UserSVG style={{ marginBottom: 8 }} />
<h2 style={{ height: 60 }}>{profile.name.slice(0, 8)}</h2>
{profile.id !== currentUser?.id && <p>SELECT</p>}
{profile.id == currentUser?.id && <p>YOU</p>}
</UserStyled>
))}
</div>
</div>
{!editing && (
<ClientButton
store={store}
large
content={"Create profile"}
icon={<PlusSVG style={{ marginRight: 16 }} />}
action={() => setEditing(true)}
/>
)}
{editing && (
<div style={{ maxWidth: 360 }}>
<TextInput
store={store}
id="Name"
label="Name"
placeholder="Name"
onChangeHandler={setCustomerNameHandler}
onBlurHandler={() => {}}
value={customerName}
type="text"
success={Boolean(customerName)}
error={errors.customerName}
/>
<div style={{ height: 16 }} />
<ClientButton
store={store}
large
content={"Create profile"}
icon={<PlusSVG style={{ marginRight: 16 }} />}
action={createProfileHandler}
disabled={!customerName}
/>
</div>
)}
</>
<div style={{ height: 16 }} />
<InviteFriends interaction={interaction} />
</>
);
};