diff --git a/apps/portal/src/components/resumes/comments/CommentsForm.tsx b/apps/portal/src/components/resumes/comments/CommentsForm.tsx index b80af421..a20bfd0c 100644 --- a/apps/portal/src/components/resumes/comments/CommentsForm.tsx +++ b/apps/portal/src/components/resumes/comments/CommentsForm.tsx @@ -1,50 +1,56 @@ -import type { FormEvent } from 'react'; -import { useRef, useState } from 'react'; +import { useState } from 'react'; +import type { SubmitHandler } from 'react-hook-form'; +import { useForm } from 'react-hook-form'; import { Button, Dialog, TextInput } from '@tih/ui'; -import { COMMENTS_SECTIONS } from './constants'; - type CommentsFormProps = Readonly<{ setShowCommentsForm: (show: boolean) => void; }>; +type IFormInput = { + education: string; + experience: string; + general: string; + projects: string; + skills: string; +}; + +type InputKeys = keyof IFormInput; + export default function CommentsForm({ setShowCommentsForm, }: CommentsFormProps) { const [showDialog, setShowDialog] = useState(false); - const formRef = useRef(null); + const { + register, + handleSubmit, + setValue, + formState: { isDirty }, + } = useForm({ + defaultValues: { + education: '', + experience: '', + general: '', + projects: '', + skills: '', + }, + }); + + // TODO: Implement mutation to database + const onSubmit: SubmitHandler = (data) => { + alert(JSON.stringify(data)); + }; - // TODO: Implement form validation. Disable Submit button until one input field is filled up - const onFormSubmit = async (event: FormEvent) => { - event.preventDefault(); - if (!formRef.current) { - return; + const onCancel = () => { + if (isDirty) { + setShowDialog(true); + } else { + setShowCommentsForm(false); } - - const formData = new FormData(formRef.current); - - COMMENTS_SECTIONS.forEach(({ value }) => { - const comment = (formData.get(value) as string | null)?.trim(); - - if (!comment) { - return; - } - - // TODO: Store comment in database & handle errors - /* eslint-disable no-console */ - console.log(`Storing ${value} comment ${comment} in database`); - /* eslint-enable no-console */ - }); - - // TODO: Handle form submission success - // Show alert & goto CommentsList }; - // TODO: Form validation - // If form is empty, goto CommentsList - // If form not empty, show dialog - const onCancel = () => { - setShowDialog(true); + const onValueChange = (section: InputKeys, value: string) => { + setValue(section, value.trim(), { shouldDirty: true }); }; return ( @@ -52,49 +58,48 @@ export default function CommentsForm({

Add your review

+ onSubmit={handleSubmit(onSubmit)}> {/* TODO: Convert TextInput to TextArea */}
onValueChange('general', value)} /> onValueChange('education', value)} /> onValueChange('experience', value)} /> onValueChange('projects', value)} /> onValueChange('skills', value)} />
@@ -106,7 +111,12 @@ export default function CommentsForm({ onClick={onCancel} /> -