-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathinviteMemberCard.tsx
More file actions
193 lines (185 loc) · 8.94 KB
/
inviteMemberCard.tsx
File metadata and controls
193 lines (185 loc) · 8.94 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
'use client';
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
import { Form, FormField, FormItem, FormLabel, FormControl, FormMessage } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { useCallback, useState } from "react";
import { z } from "zod";
import { PlusCircleIcon, Loader2, AlertCircle } from "lucide-react";
import { OrgRole } from "@prisma/client";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog";
import { createInvites } from "@/actions";
import { isServiceError } from "@/lib/utils";
import { useToast } from "@/components/hooks/use-toast";
import { useRouter } from "next/navigation";
import useCaptureEvent from "@/hooks/useCaptureEvent";
export const inviteMemberFormSchema = z.object({
emails: z.array(z.object({
email: z.string().email()
}))
.refine((emails) => {
const emailSet = new Set(emails.map(e => e.email.toLowerCase()));
return emailSet.size === emails.length;
}, "Duplicate email addresses are not allowed")
});
interface InviteMemberCardProps {
currentUserRole: OrgRole;
seatsAvailable?: boolean;
}
export const InviteMemberCard = ({ currentUserRole, seatsAvailable = true }: InviteMemberCardProps) => {
const [isInviteDialogOpen, setIsInviteDialogOpen] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const { toast } = useToast();
const router = useRouter();
const captureEvent = useCaptureEvent();
const form = useForm<z.infer<typeof inviteMemberFormSchema>>({
resolver: zodResolver(inviteMemberFormSchema),
defaultValues: {
emails: [{ email: "" }]
},
});
const addEmailField = useCallback(() => {
const emails = form.getValues().emails;
form.setValue('emails', [...emails, { email: "" }]);
}, [form]);
const onSubmit = useCallback((data: z.infer<typeof inviteMemberFormSchema>) => {
setIsLoading(true);
createInvites(data.emails.map(e => e.email))
.then((res) => {
if (isServiceError(res)) {
toast({
description: `❌ Failed to invite members. Reason: ${res.message}`
});
captureEvent('wa_invite_member_card_invite_fail', {
errorCode: res.errorCode,
num_emails: data.emails.length,
});
} else {
form.reset();
router.push(`?tab=invites`);
toast({
description: `✅ Successfully invited ${data.emails.length} members`
});
captureEvent('wa_invite_member_card_invite_success', {
num_emails: data.emails.length,
});
}
})
.finally(() => {
setIsLoading(false);
});
}, [form, toast, router, captureEvent]);
const isDisabled = !seatsAvailable || currentUserRole !== OrgRole.OWNER || isLoading;
return (
<>
<Card className={!seatsAvailable ? "opacity-70" : ""}>
<CardHeader>
<CardTitle>Invite Member</CardTitle>
<CardDescription>Invite new members to your organization.</CardDescription>
</CardHeader>
{!seatsAvailable && (
<div className="px-6 mb-4">
<div className="flex items-start space-x-2.5 p-3 rounded-md border border-gray-700 bg-gray-800/50 text-gray-200 shadow-md">
<AlertCircle className="h-4 w-4 text-amber-400 mt-0.5 flex-shrink-0" />
<div className="flex-1">
<p className="text-sm font-medium leading-tight text-white">
Maximum seats reached
</p>
<p className="text-xs mt-1 text-gray-300">
You've reached the maximum number of seats for your license. Upgrade your plan to invite additional members.
</p>
</div>
</div>
</div>
)}
<Form {...form}>
<form onSubmit={form.handleSubmit(() => setIsInviteDialogOpen(true))}>
<CardContent className="space-y-4">
<FormLabel>Email Address</FormLabel>
{form.watch('emails').map((_, index) => (
<FormField
key={index}
control={form.control}
name={`emails.${index}.email`}
render={({ field }) => (
<FormItem>
<FormControl>
<Input
{...field}
className="max-w-md"
placeholder="melissa@example.com"
disabled={isDisabled}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
))}
{form.formState.errors.emails?.root?.message && (
<FormMessage>{form.formState.errors.emails.root.message}</FormMessage>
)}
<Button
type="button"
variant="outline"
size="sm"
onClick={addEmailField}
disabled={isDisabled}
>
<PlusCircleIcon className="w-4 h-4 mr-0.5" />
Add more
</Button>
</CardContent>
<CardFooter className="flex justify-end">
<Button
size="sm"
type="submit"
disabled={isDisabled}
>
{isLoading && <Loader2 className="w-4 h-4 mr-0.5 animate-spin" />}
Invite
</Button>
</CardFooter>
</form>
</Form>
</Card>
<AlertDialog
open={isInviteDialogOpen}
onOpenChange={setIsInviteDialogOpen}
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Invite Team Members</AlertDialogTitle>
<AlertDialogDescription>
{`Your team is growing! By confirming, you will be inviting ${form.getValues().emails.length} new members to your organization.`}
</AlertDialogDescription>
</AlertDialogHeader>
<div className="border rounded-lg overflow-hidden">
<div className="max-h-[400px] overflow-y-auto divide-y">
{form.getValues().emails.map(({ email }, index) => (
<p
key={index}
className="text-sm p-2"
>
{email}
</p>
))}
</div>
</div>
<AlertDialogFooter>
<AlertDialogCancel onClick={() => captureEvent('wa_invite_member_card_invite_cancel', {
num_emails: form.getValues().emails.length,
})}>Cancel</AlertDialogCancel>
<AlertDialogAction
onClick={() => onSubmit(form.getValues())}
>
Invite
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
)
}