-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAddCommunityForm.tsx
More file actions
123 lines (112 loc) · 3.14 KB
/
Copy pathAddCommunityForm.tsx
File metadata and controls
123 lines (112 loc) · 3.14 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
"use client"
import { useMemo } from "react"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { z } from "zod"
import { Button } from "ui/button"
import { Form, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "ui/form"
import { Loader2 } from "ui/icon"
import { Input } from "ui/input"
import { toast } from "ui/use-toast"
import { AvatarEditor } from "~/app/(user)/settings/AvatarEditor"
import { uploadTempAvatar } from "~/app/login/actions"
import { didSucceed, useServerAction } from "~/lib/serverActions"
import { createCommunity } from "./actions"
export const communityCreateFormSchema = z.object({
name: z.string().min(1),
slug: z.string().min(1),
avatar: z.string().url().nullable().optional(),
})
type Props = {
setOpen: (open: false) => void
}
export const AddCommunityForm = (props: Props) => {
const runCreateCommunity = useServerAction(createCommunity)
const runUpload = useServerAction(uploadTempAvatar)
async function onSubmit(data: z.infer<typeof communityCreateFormSchema>) {
const result = await runCreateCommunity({ ...data })
if (didSucceed(result)) {
props.setOpen(false)
toast.success("Community created")
}
}
const form = useForm<z.infer<typeof communityCreateFormSchema>>({
resolver: zodResolver(communityCreateFormSchema),
defaultValues: {
name: "",
slug: "",
avatar: null,
},
})
const signedUploadUrl = (fileName: string) => {
return runUpload({ fileName })
}
const communityName = form.watch("name")
const communityInitials = useMemo(() => {
return (
communityName
.split(" ")
.slice(0, 2)
.map((word) => word[0])
.join("")
.toUpperCase() || "C"
)
}, [communityName])
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="flex flex-col gap-y-4">
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<Input {...field} />
<FormDescription>What is the name of your community</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="slug"
render={({ field }) => (
<FormItem>
<FormLabel>Slug</FormLabel>
<Input {...field} />
<FormDescription>
Name the string you want your community to route to
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="avatar"
render={({ field }) => (
<FormItem>
<FormLabel>Avatar (optional)</FormLabel>
<AvatarEditor
initials={communityInitials}
avatar={field.value ?? null}
onEdit={field.onChange}
upload={signedUploadUrl}
label="Community Avatar"
showDeleteButton={false}
/>
<FormMessage />
</FormItem>
)}
/>
<Button size="sm" type="submit" disabled={form.formState.isSubmitting}>
{form.formState.isSubmitting ? (
<Loader2 />
) : (
<div className="flex items-center gap-x-2">Create Community</div>
)}
</Button>
</form>
</Form>
)
}