Skip to content

Commit

Permalink
update form
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh3dev committed Jan 26, 2025
1 parent 3678db4 commit acf9cd2
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 112 deletions.
158 changes: 74 additions & 84 deletions frontend/src/components/FeedbackForm.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Textarea, Input, Button } from "@chakra-ui/react"
import { Textarea, Input, Button } from '@chakra-ui/react'
import { zodResolver } from '@hookform/resolvers/zod'
import { postFeedback } from 'api/postFeedbackData'
import { useToast } from 'hooks/useToast'
import { useRef, useState } from 'react'
import ReCAPTCHA from 'react-google-recaptcha'
import { useForm } from 'react-hook-form'
import { Controller, useForm } from 'react-hook-form'
import { RECAPTCHA_SITE_KEY } from 'utils/credentials'
import {
anonymousFeedbackFormSchema,
feedbackFormSchema,
type FeedbackFormValues,
} from 'utils/helpers/schema'
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from 'components/ui/Form'
import { Switch } from 'components/ui/switch'
import { Switch } from 'components/ui/Switch'
import { Label } from './ui/Label'

export function FeedbackForm() {
const [isAnonymous, setIsAnonymous] = useState(false)
Expand Down Expand Up @@ -65,92 +65,82 @@ export function FeedbackForm() {
}

return (
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="space-y-8 rounded-lg border p-4 py-8 w-full max-w-screen-lg mx-auto"
>
<h1 className="w-full text-center font-bold">Feedback form</h1>
{!isAnonymous && (
<div className="w-full flex justify-between items-center gap-4">
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input
placeholder="Your name"
className="border border-border rounded-lg p-4"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
<form
onSubmit={form.handleSubmit(onSubmit)}
className="mx-auto w-full max-w-screen-lg space-y-8 rounded-lg border p-4 py-8"
>
<h1 className="w-full text-center font-bold">Feedback form</h1>
{!isAnonymous && (
<div className="flex w-full items-center justify-between gap-4">
<div className="">
<Label htmlFor="name">Name</Label>
<Input
placeholder="Your name"
id="name"
className="rounded-lg border border-border p-4"
{...form.register('name')}
/>
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input
placeholder="[email protected]"
className="border border-border rounded-lg p-4"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
{form.formState.errors.name && (
<p className="mt-1 text-xs text-red-500">{form.formState.errors.name.message}</p>
)}
</div>
<div>
<Label htmlFor="email">Email</Label>
<Input
placeholder="[email protected]"
id="email"
className="rounded-lg border border-border p-4"
{...form.register('email')}
/>
{form.formState.errors.email && (
<p className="mt-1 text-xs text-red-500">{form.formState.errors.email.message}</p>
)}
</div>
)}
<FormField
control={form.control}
name="message"
render={({ field }) => (
<FormItem>
<FormLabel>Message</FormLabel>
<FormControl>
<Textarea
placeholder="Your feedback here..."
className="border border-border rounded-lg p-4"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
</div>
)}
<div>
<Label htmlFor="message">Message</Label>
<Textarea
placeholder="Your feedback here..."
id="message"
className="rounded-lg border border-border p-4"
{...form.register('message')}
/>
<FormField
control={form.control}
{form.formState.errors.message && (
<p className="mt-1 text-xs text-red-500">{form.formState.errors.message.message}</p>
)}
</div>

<div className="flex flex-row items-center justify-between gap-4 rounded-lg border border-border p-4">
<div className="space-y-0.5">
<Label htmlFor="is_anonymous" className="text-base">
Anonymous Feedback
</Label>
</div>
<Controller
name="is_anonymous"
render={({ field }) => (
<FormItem className="flex flex-row items-center justify-between gap-4 rounded-lg border border-border p-4">
<div className="space-y-0.5">
<FormLabel className="text-base">Anonymous Feedback</FormLabel>
</div>
<FormControl>
<Switch
checked={field.value}
onCheckedChange={({ checked }: { checked: boolean }) => {
field.onChange(checked)
setIsAnonymous(checked)
}}
/>
</FormControl>
</FormItem>
control={form.control}
render={({ field: { onChange, value } }) => (
<Switch
checked={value}
onCheckedChange={({ checked }: { checked: boolean }) => {
onChange(checked)
setIsAnonymous(checked)
}}
/>
)}
/>
<ReCAPTCHA sitekey={RECAPTCHA_SITE_KEY} ref={captchaRef} />
<Button className="p-2 px-4 bg-primary text-white dark:text-gray-800" variant='solid' type="submit" aria-label="Submit Feedback" disabled={form.formState.isSubmitting}>
{form.formState.isSubmitting ? 'Submitting...' : 'Submit Feedback'}
</Button>
</form>
</Form>
</div>
<ReCAPTCHA sitekey={RECAPTCHA_SITE_KEY} ref={captchaRef} />
<Button
className="bg-primary p-2 px-4 text-white dark:text-gray-800"
variant="solid"
type="submit"
aria-label="Submit Feedback"
disabled={form.formState.isSubmitting}
>
{form.formState.isSubmitting ? 'Submitting...' : 'Submit Feedback'}
</Button>
</form>
)
}
49 changes: 21 additions & 28 deletions frontend/src/components/ui/Switch.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Switch as ChakraSwitch } from "@chakra-ui/react"
import * as React from "react"
import { Switch as ChakraSwitch } from '@chakra-ui/react'
import * as React from 'react'

export interface SwitchProps extends ChakraSwitch.RootProps {
inputProps?: React.InputHTMLAttributes<HTMLInputElement>
Expand All @@ -8,32 +8,25 @@ export interface SwitchProps extends ChakraSwitch.RootProps {
thumbLabel?: { on: React.ReactNode; off: React.ReactNode }
}

export const Switch = React.forwardRef<HTMLInputElement, SwitchProps>(
function Switch(props, ref) {
const { inputProps, children, rootRef, trackLabel, thumbLabel, ...rest } =
props
export const Switch = React.forwardRef<HTMLInputElement, SwitchProps>(function Switch(props, ref) {
const { inputProps, children, rootRef, trackLabel, thumbLabel, ...rest } = props

return (
<ChakraSwitch.Root ref={rootRef} {...rest}>
<ChakraSwitch.HiddenInput ref={ref} {...inputProps} />
<ChakraSwitch.Control>
<ChakraSwitch.Thumb>
{thumbLabel && (
<ChakraSwitch.ThumbIndicator fallback={thumbLabel?.off}>
{thumbLabel?.on}
</ChakraSwitch.ThumbIndicator>
)}
</ChakraSwitch.Thumb>
{trackLabel && (
<ChakraSwitch.Indicator fallback={trackLabel.off}>
{trackLabel.on}
</ChakraSwitch.Indicator>
return (
<ChakraSwitch.Root ref={rootRef} {...rest}>
<ChakraSwitch.HiddenInput ref={ref} {...inputProps} />
<ChakraSwitch.Control>
<ChakraSwitch.Thumb>
{thumbLabel && (
<ChakraSwitch.ThumbIndicator fallback={thumbLabel?.off}>
{thumbLabel?.on}
</ChakraSwitch.ThumbIndicator>
)}
</ChakraSwitch.Control>
{children != null && (
<ChakraSwitch.Label>{children}</ChakraSwitch.Label>
</ChakraSwitch.Thumb>
{trackLabel && (
<ChakraSwitch.Indicator fallback={trackLabel.off}>{trackLabel.on}</ChakraSwitch.Indicator>
)}
</ChakraSwitch.Root>
)
},
)
</ChakraSwitch.Control>
{children != null && <ChakraSwitch.Label>{children}</ChakraSwitch.Label>}
</ChakraSwitch.Root>
)
})

0 comments on commit acf9cd2

Please sign in to comment.