-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-feedback.tsx
125 lines (115 loc) · 3.54 KB
/
send-feedback.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
'use client';
import { sendFeedback } from '@/actions/feedback';
import { Button } from '@/components/ui/button';
import {
Drawer,
DrawerContent,
DrawerDescription,
DrawerHeader,
DrawerTitle,
DrawerTrigger,
} from '@/components/ui/drawer';
import {
Form,
FormControl,
FormField,
FormItem,
FormMessage,
} from '@/components/ui/form';
import { zodResolver } from '@hookform/resolvers/zod';
import { toast } from '@mosespace/toast';
import { Loader, Send } from 'lucide-react';
import { useRouter } from 'next/navigation';
import * as React from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { Textarea } from './ui/textarea';
const FormSchema = z.object({
message: z.string().min(2, {
message: 'Message must be at least 2 characters.',
}),
userId: z.string().optional(),
});
export const SendFeedback = ({ user }: any) => {
const form = useForm<z.infer<typeof FormSchema>>({
resolver: zodResolver(FormSchema),
defaultValues: {
message: '',
userId: user?.id || '',
},
});
const [isOpen, setIsOpen] = React.useState<boolean>(false);
const [loading, setLoading] = React.useState<boolean>(false);
const router = useRouter();
const userId = user?.id;
async function onSubmit(data: z.infer<typeof FormSchema>) {
setLoading(true);
data.userId = userId;
console.log('Data:', data);
if (userId) {
const req = await sendFeedback(data);
if (req.status === 201) {
toast.success('Success', 'Feedback has been submitted');
form.reset();
} else {
toast.error('Error', 'Failed to submit feedback');
}
setLoading(false);
} else {
setLoading(false);
router.push('/login');
}
setIsOpen(false);
}
return (
<Drawer open={isOpen} onOpenChange={setIsOpen}>
<DrawerTrigger asChild>
<Button
variant="outline"
className="fixed bottom-6 right-6 z-10 group inline-flex h-12 w-12 items-center justify-center overflow-hidden rounded-full"
>
<div className="transition duration-300 group-hover:rotate-[360deg]">
<Send className="size-8 text-brandColor -rotate-12" />
</div>
</Button>
</DrawerTrigger>
<DrawerContent className="space-y-4 pb-4 lg:pb-9">
<DrawerHeader>
<DrawerTitle className="text-center lg:text-3xl">
What feature would you like to see next?
</DrawerTitle>
<DrawerDescription className="text-center lg:text-xl">
Send it, and.. I’ll build it for you 🤩
</DrawerDescription>
</DrawerHeader>
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="w-full flex flex-col gap-2 max-w-2xl mx-auto px-4"
>
<FormField
control={form.control}
name="message"
render={({ field }) => (
<FormItem>
<FormControl>
<Textarea
className="h-24"
placeholder="Enter request here..."
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button className="flex bg-primary/80 text-white items-center gap-2">
{loading && <Loader className="w-4 h-4 animate-spin" />}
send feedback
</Button>
</form>
</Form>
</DrawerContent>
</Drawer>
);
};