-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCreateProfileDialog.tsx
More file actions
192 lines (184 loc) · 6.21 KB
/
Copy pathCreateProfileDialog.tsx
File metadata and controls
192 lines (184 loc) · 6.21 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
import { Plus } from "lucide-react";
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { useCreateProfile } from "@/hooks/profiles/use-create-profile";
import { useQueryClient } from "@tanstack/react-query";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
const linkedinUrlRegex = /^(?:https?:\/\/(?:www\.)?linkedin\.com\/in\/)?[a-zA-Z0-9-]{3,100}\/?$/;
const formSchema = z.object({
name: z.string().min(2, { message: "Name must be at least 2 characters." }),
description: z.string().optional(),
githubLogin: z.string().optional(),
twitterHandle: z.string().optional(),
linkedinAccount: z
.string()
.optional()
.refine(
(value) => !value || linkedinUrlRegex.test(value),
"LinkedIn URL must be in format: https://www.linkedin.com/in/your-handle or just the handle (3-100 characters)"
),
});
type FormValues = z.infer<typeof formSchema>;
export function CreateProfileButton() {
const [open, setOpen] = useState(false);
const createProfile = useCreateProfile();
const queryClient = useQueryClient();
const form = useForm<FormValues>({
resolver: zodResolver(formSchema),
defaultValues: { name: "", description: "" },
});
const onSubmit = async (values: FormValues) => {
await createProfile.mutateAsync({
input: {
name: values.name,
description: values.description || "",
github_login: values.githubLogin || "",
twitter_handle: values.twitterHandle || "",
linkedin_account: values.linkedinAccount || "",
},
});
await queryClient.invalidateQueries({ queryKey: ["profiles"] });
setOpen(false);
form.reset();
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button className="flex items-center space-x-2">
<Plus className="h-4 w-4" />
<span>Create Profile</span>
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Create Profile</DialogTitle>
<DialogDescription>
Provide a name and an optional description for your profile.
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input placeholder="My profile" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="description"
render={({ field }) => (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
<Textarea
placeholder="Write a short introduction..."
className="resize-none"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="githubLogin"
render={({ field }) => (
<FormItem>
<FormLabel>GitHub Handle</FormLabel>
<FormControl>
<div className="flex items-center gap-2">
<span className="text-sm text-gray-500">@</span>
<Input placeholder="username" {...field} />
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="twitterHandle"
render={({ field }) => (
<FormItem>
<FormLabel>Twitter/X Handle</FormLabel>
<FormControl>
<div className="flex items-center gap-2">
<span className="text-sm text-gray-500">@</span>
<Input placeholder="username" {...field} />
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="linkedinAccount"
render={({ field }) => (
<FormItem>
<FormLabel>LinkedIn Profile URL</FormLabel>
<FormControl>
<Input
placeholder="https://www.linkedin.com/in/your-handle/"
{...field}
/>
</FormControl>
<p className="text-xs text-gray-500 mt-1">
Paste your full LinkedIn URL or just the handle
</p>
<FormMessage />
</FormItem>
)}
/>
<div className="flex justify-end gap-2 pt-2">
<DialogClose asChild>
<Button type="button" variant="secondary">
Cancel
</Button>
</DialogClose>
<Button type="submit" disabled={createProfile.isPending}>
{createProfile.isPending ? "Creating..." : "Create"}
</Button>
</div>
{createProfile.isError ? (
<p className="text-sm text-red-600">
{(createProfile.error as Error).message}
</p>
) : null}
</form>
</Form>
</DialogContent>
</Dialog>
);
}
export default CreateProfileButton;