aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/feedback-button/feedback-button-client.tsx
blob: 16debec5cd5945f5151374700280b965d58b98ac (plain)
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
'use client'
import { formSchema } from '@/components/feedback-button/feedback-button-common'
import { Button } from '@/components/ui/button'
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from '@/components/ui/dialog'
import {
  Drawer,
  DrawerContent,
  DrawerDescription,
  DrawerHeader,
  DrawerTitle,
  DrawerTrigger,
} from '@/components/ui/drawer'
import {
  Form,
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { Textarea } from '@/components/ui/textarea'
import { useToast } from '@/components/ui/use-toast'
import { useMediaQuery } from '@/lib/hooks'
import { zodResolver } from '@hookform/resolvers/zod'
import { Loader2, MessageCircle } from 'lucide-react'
import { useState } from 'react'
import { useForm } from 'react-hook-form'
import * as z from 'zod'

type FormValues = z.infer<typeof formSchema>

type Props = {
  sendFeedback: (values: FormValues) => Promise<void>
}

export function FeedbackButtonClient({ sendFeedback }: Props) {
  const { toast } = useToast()
  const isDesktop = useMediaQuery('(min-width: 768px)')

  async function onSubmit(values: FormValues) {
    await sendFeedback(values)
    toast({
      title: 'Thank you for your feedback!',
      description:
        'We will have a look at it as soon as possible, and will get back to you if needed.',
    })
  }

  return (
    <div className="fixed right-4 bottom-4">
      {isDesktop ? (
        <FeedbackDialog onSubmit={onSubmit} />
      ) : (
        <FeedbackDrawer onSubmit={onSubmit} />
      )}
    </div>
  )
}

function FeedbackDrawer({
  onSubmit,
}: {
  onSubmit: (values: FormValues) => Promise<void>
}) {
  const [open, setOpen] = useState(false)

  return (
    <div className="fixed right-4 bottom-4">
      <Drawer open={open} onOpenChange={setOpen}>
        <DrawerTrigger asChild>
          <Button>
            <MessageCircle className="w-4 h-4 mr-2" /> Feedback
          </Button>
        </DrawerTrigger>
        <DrawerContent>
          <DrawerHeader>
            <DrawerTitle>Give us your feedback!</DrawerTitle>
            <DrawerDescription>
              We are always working to improve the user experience, and your
              feedback helps us a lot.
            </DrawerDescription>
          </DrawerHeader>
          <div className="px-4 pb-4">
            <FeedbackForm
              onSubmit={async (values) => {
                await onSubmit(values)
                setOpen(false)
              }}
            />
          </div>
        </DrawerContent>
      </Drawer>
    </div>
  )
}

function FeedbackDialog({
  onSubmit,
}: {
  onSubmit: (values: FormValues) => Promise<void>
}) {
  const [open, setOpen] = useState(false)

  return (
    <div className="fixed right-4 bottom-4">
      <Dialog open={open} onOpenChange={setOpen}>
        <DialogTrigger asChild>
          <Button>
            <MessageCircle className="w-4 h-4 mr-2" /> Feedback
          </Button>
        </DialogTrigger>
        <DialogContent>
          <DialogHeader>
            <DialogTitle>Give us your feedback!</DialogTitle>
            <DialogDescription>
              We are always working to improve the user experience, and your
              feedback helps us a lot.
            </DialogDescription>
          </DialogHeader>
          <FeedbackForm
            onSubmit={async (values) => {
              await onSubmit(values)
              setOpen(false)
            }}
          />
        </DialogContent>
      </Dialog>
    </div>
  )
}

function FeedbackForm({
  onSubmit,
}: {
  onSubmit: (values: FormValues) => Promise<void>
}) {
  const form = useForm<FormValues>({
    resolver: zodResolver(formSchema),
    defaultValues: { email: '', message: '' },
  })

  const isSubmitting = form.formState.isSubmitting
  return (
    <Form {...form}>
      <form onSubmit={form.handleSubmit(onSubmit)} className="grid gap-4">
        <div className="grid gap-4">
          <FormField
            control={form.control}
            name="email"
            render={({ field }) => (
              <FormItem>
                <FormLabel>Your email address</FormLabel>
                <FormControl>
                  <Input placeholder="your@email.com" {...field} />
                </FormControl>
                <FormDescription>
                  Optional. Provide it if you want us to get back to you.
                </FormDescription>
                <FormMessage />
              </FormItem>
            )}
          />
          <FormField
            control={form.control}
            name="message"
            render={({ field }) => (
              <FormItem>
                <FormLabel>Your feedback</FormLabel>
                <FormControl>
                  <Textarea placeholder="Enter your feedback" {...field} />
                </FormControl>
                <FormMessage />
              </FormItem>
            )}
          />
        </div>
        <Button type="submit" disabled={isSubmitting}>
          {isSubmitting ? (
            <>
              <Loader2 className="w-4 h-4 mr-2 animate-spin" /> Submitting…
            </>
          ) : (
            <>Submit</>
          )}
        </Button>
      </form>
    </Form>
  )
}