|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { zodResolver } from '@hookform/resolvers/zod' |
| 4 | +import { Controller, useForm } from 'react-hook-form' |
| 5 | +import { z } from 'zod' |
| 6 | +import { Form } from '../ui/form' |
| 7 | +import { toast } from 'sonner' |
| 8 | +import { Button } from '../ui/button' |
| 9 | +import { useCallback, useTransition } from 'react' |
| 10 | +import { Spinner } from '../ui/spinner' |
| 11 | +import { BorrowDetail } from '@/lib/types/borrow' |
| 12 | +import { Field, FieldDescription, FieldError, FieldLabel } from '../ui/field' |
| 13 | +import { Textarea } from '../ui/textarea' |
| 14 | +import { reviewBorrowAction } from '@/lib/actions/review-borrow' |
| 15 | +import { Star } from 'lucide-react' |
| 16 | + |
| 17 | +const REVIEW_MAX_LENGTH = 512 |
| 18 | + |
| 19 | +const BaseReviewSchema = z.object({ |
| 20 | + rating: z.coerce.number<number>().min(0).max(5), |
| 21 | + comment: z.string().max(REVIEW_MAX_LENGTH).optional(), |
| 22 | +}) |
| 23 | +const UpdateReviewSchema = BaseReviewSchema.extend({ |
| 24 | + id: z.string(), |
| 25 | +}) |
| 26 | +const CreateReviewSchema = BaseReviewSchema.extend({ |
| 27 | + borrow_id: z.uuid(), |
| 28 | +}) |
| 29 | +const FormSchema = z.union([UpdateReviewSchema, CreateReviewSchema]) |
| 30 | + |
| 31 | +export const FormReview: React.FC<{ |
| 32 | + borrow: BorrowDetail |
| 33 | +}> = ({ borrow }) => { |
| 34 | + const form = useForm<z.infer<typeof FormSchema>>({ |
| 35 | + resolver: zodResolver(FormSchema), |
| 36 | + mode: 'onChange', |
| 37 | + defaultValues: borrow.review |
| 38 | + ? { |
| 39 | + id: borrow.review.id, |
| 40 | + rating: borrow.review.rating, |
| 41 | + comment: borrow.review.comment, |
| 42 | + } |
| 43 | + : { |
| 44 | + borrow_id: borrow.id, |
| 45 | + }, |
| 46 | + }) |
| 47 | + |
| 48 | + const [isPending, startTransition] = useTransition() |
| 49 | + |
| 50 | + function onSubmit(data: z.infer<typeof FormSchema>) { |
| 51 | + startTransition(async () => { |
| 52 | + const msg = await reviewBorrowAction( |
| 53 | + borrow.id, |
| 54 | + 'id' in data |
| 55 | + ? { |
| 56 | + id: data.id, |
| 57 | + rating: data.rating, |
| 58 | + comment: data.comment, |
| 59 | + } |
| 60 | + : { |
| 61 | + borrowing_id: data.borrow_id, |
| 62 | + rating: data.rating, |
| 63 | + comment: data.comment, |
| 64 | + } |
| 65 | + ) |
| 66 | + if ('error' in msg) { |
| 67 | + toast.error(msg.error, { richColors: true }) |
| 68 | + } else { |
| 69 | + toast.success(msg.message) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + const onReset = useCallback(() => { |
| 75 | + form.reset() |
| 76 | + }, [form]) |
| 77 | + |
| 78 | + return ( |
| 79 | + <Form {...form}> |
| 80 | + <form onSubmit={form.handleSubmit(onSubmit)} className="grid gap-4"> |
| 81 | + <Controller |
| 82 | + control={form.control} |
| 83 | + name="rating" |
| 84 | + render={({ field, fieldState }) => ( |
| 85 | + <Field data-invalid={fieldState.invalid}> |
| 86 | + <FieldLabel>Rating</FieldLabel> |
| 87 | + <div className="flex items-center gap-1"> |
| 88 | + {[1, 2, 3, 4, 5].map((star) => ( |
| 89 | + <button |
| 90 | + key={star} |
| 91 | + type="button" |
| 92 | + onClick={() => field.onChange(star)} |
| 93 | + className="focus:outline-none transition-transform hover:scale-110" |
| 94 | + > |
| 95 | + <Star |
| 96 | + className={`h-6 w-6 ${ |
| 97 | + star <= field.value |
| 98 | + ? 'fill-yellow-400 text-yellow-400' |
| 99 | + : 'text-gray-300' |
| 100 | + }`} |
| 101 | + /> |
| 102 | + </button> |
| 103 | + ))} |
| 104 | + {field.value > 0 && ( |
| 105 | + <span className="ml-2 text-sm text-muted-foreground"> |
| 106 | + {field.value} {field.value === 1 ? 'star' : 'stars'} |
| 107 | + </span> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + </Field> |
| 111 | + )} |
| 112 | + /> |
| 113 | + |
| 114 | + <Controller |
| 115 | + control={form.control} |
| 116 | + name="comment" |
| 117 | + render={({ field, fieldState }) => ( |
| 118 | + <Field |
| 119 | + data-invalid={fieldState.invalid} |
| 120 | + className="flex flex-col col-span-2" |
| 121 | + > |
| 122 | + <FieldLabel>Your Review</FieldLabel> |
| 123 | + <Textarea |
| 124 | + placeholder="What did you think about the book?" |
| 125 | + {...field} |
| 126 | + onChange={field.onChange} |
| 127 | + rows={4} |
| 128 | + /> |
| 129 | + {fieldState.error && <FieldError errors={[fieldState.error]} />} |
| 130 | + <FieldDescription> |
| 131 | + {field.value?.length ?? 0}/{REVIEW_MAX_LENGTH} |
| 132 | + </FieldDescription> |
| 133 | + </Field> |
| 134 | + )} |
| 135 | + /> |
| 136 | + |
| 137 | + <Button type="reset" variant="ghost" onClick={onReset}> |
| 138 | + Reset |
| 139 | + </Button> |
| 140 | + <Button type="submit" disabled={isPending}> |
| 141 | + {isPending && <Spinner />} |
| 142 | + Submit Review |
| 143 | + </Button> |
| 144 | + </form> |
| 145 | + </Form> |
| 146 | + ) |
| 147 | +} |
0 commit comments