({questionToUpdate}: Props)
| 22 | } |
| 23 | |
| 24 | export default function QuestionForm({questionToUpdate}: Props) { |
| 25 | const [pending, startTransition] = useTransition(); |
| 26 | const tags = useTagStore(state => state.tags); |
| 27 | const router = useRouter(); |
| 28 | const { register, control, reset, handleSubmit, formState: {isSubmitting, isValid, errors} } = useForm({ |
| 29 | mode: 'onTouched', |
| 30 | resolver: zodResolver(questionSchema) |
| 31 | }); |
| 32 | |
| 33 | useEffect(() => { |
| 34 | if (questionToUpdate) reset({ |
| 35 | ...questionToUpdate, |
| 36 | tags: questionToUpdate.tagSlugs |
| 37 | }) |
| 38 | }, [questionToUpdate, reset]) |
| 39 | |
| 40 | const onSubmit = (data: QuestionSchema) => { |
| 41 | startTransition(async () => { |
| 42 | if (questionToUpdate) { |
| 43 | const {error} = await updateQuestion(data, questionToUpdate.id); |
| 44 | if (error) handleError(error); |
| 45 | router.push(`/questions/${questionToUpdate.id}`); |
| 46 | } else { |
| 47 | const {data: question, error} = await postQuestion(data); |
| 48 | if (error) handleError(error); |
| 49 | if (question) router.push(`/questions/${question.id}`); |
| 50 | } |
| 51 | }) |
| 52 | } |
| 53 | |
| 54 | return ( |
| 55 | <Form onSubmit={handleSubmit(onSubmit)} |
| 56 | className='flex flex-col gap-3 p-6 shadow-xl bg-white dark:bg-black' > |
| 57 | <div className="flex flex-col gap-3 w-full"> |
| 58 | <h3 className='text-2xl font-semibold'>Title</h3> |
| 59 | <Input |
| 60 | {...register('title')} |
| 61 | type="text" |
| 62 | className="w-full" |
| 63 | label="Be specific and imagine you’re asking a question to another person" |
| 64 | labelPlacement='outside-top' |
| 65 | placeholder="e.g how would you truncate text in Tailwind?" |
| 66 | isInvalid={!!errors?.title} |
| 67 | errorMessage={errors.title?.message} |
| 68 | /> |
| 69 | </div> |
| 70 | <div className="flex flex-col gap-3 w-full"> |
| 71 | <h3 className='text-2xl font-semibold'>Body</h3> |
| 72 | <Controller |
| 73 | name='content' |
| 74 | control={control} |
| 75 | render={({field: {onChange, onBlur, value}, fieldState}) => ( |
| 76 | <> |
| 77 | <p className={`text-sm ${fieldState.error?.message && 'text-danger'}`}> |
| 78 | Include all the information someone would need to answer your question |
| 79 | </p> |
| 80 | <RichTextEditor |
| 81 | onChange={onChange} |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…