[resumes][feat] Add form validation

pull/306/head
Terence Ho 3 years ago committed by Wu Peirong
parent 0ede04b23c
commit ccdfd5642b

@ -1,50 +1,56 @@
import type { FormEvent } from 'react'; import { useState } from 'react';
import { useRef, useState } from 'react'; import type { SubmitHandler } from 'react-hook-form';
import { useForm } from 'react-hook-form';
import { Button, Dialog, TextInput } from '@tih/ui'; import { Button, Dialog, TextInput } from '@tih/ui';
import { COMMENTS_SECTIONS } from './constants';
type CommentsFormProps = Readonly<{ type CommentsFormProps = Readonly<{
setShowCommentsForm: (show: boolean) => void; setShowCommentsForm: (show: boolean) => void;
}>; }>;
type IFormInput = {
education: string;
experience: string;
general: string;
projects: string;
skills: string;
};
type InputKeys = keyof IFormInput;
export default function CommentsForm({ export default function CommentsForm({
setShowCommentsForm, setShowCommentsForm,
}: CommentsFormProps) { }: CommentsFormProps) {
const [showDialog, setShowDialog] = useState(false); const [showDialog, setShowDialog] = useState(false);
const formRef = useRef<HTMLFormElement | null>(null); const {
register,
handleSubmit,
setValue,
formState: { isDirty },
} = useForm<IFormInput>({
defaultValues: {
education: '',
experience: '',
general: '',
projects: '',
skills: '',
},
});
// TODO: Implement mutation to database
const onSubmit: SubmitHandler<IFormInput> = (data) => {
alert(JSON.stringify(data));
};
// TODO: Implement form validation. Disable Submit button until one input field is filled up const onCancel = () => {
const onFormSubmit = async (event: FormEvent<HTMLFormElement>) => { if (isDirty) {
event.preventDefault(); setShowDialog(true);
if (!formRef.current) { } else {
return; 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 const onValueChange = (section: InputKeys, value: string) => {
// If form is empty, goto CommentsList setValue(section, value.trim(), { shouldDirty: true });
// If form not empty, show dialog
const onCancel = () => {
setShowDialog(true);
}; };
return ( return (
@ -52,49 +58,48 @@ export default function CommentsForm({
<h2 className="text-xl font-semibold text-gray-800">Add your review</h2> <h2 className="text-xl font-semibold text-gray-800">Add your review</h2>
<form <form
ref={formRef}
className="w-full space-y-8 divide-y divide-gray-200" className="w-full space-y-8 divide-y divide-gray-200"
onSubmit={onFormSubmit}> onSubmit={handleSubmit(onSubmit)}>
{/* TODO: Convert TextInput to TextArea */} {/* TODO: Convert TextInput to TextArea */}
<div className="mt-4 space-y-4"> <div className="mt-4 space-y-4">
<TextInput <TextInput
id="general" {...(register('general'), {})}
label="General" label="General"
name="general"
placeholder="General comments about the resume" placeholder="General comments about the resume"
type="text" type="text"
onChange={(value) => onValueChange('general', value)}
/> />
<TextInput <TextInput
id="education" {...(register('education'), {})}
label="Education" label="Education"
name="education"
placeholder="Comments about the Education section" placeholder="Comments about the Education section"
type="text" type="text"
onChange={(value) => onValueChange('education', value)}
/> />
<TextInput <TextInput
id="experience" {...(register('experience'), {})}
label="Experience" label="Experience"
name="experience"
placeholder="Comments about the Experience section" placeholder="Comments about the Experience section"
type="text" type="text"
onChange={(value) => onValueChange('experience', value)}
/> />
<TextInput <TextInput
id="projects" {...(register('projects'), {})}
label="Projects" label="Projects"
name="projects"
placeholder="Comments about the Projects section" placeholder="Comments about the Projects section"
type="text" type="text"
onChange={(value) => onValueChange('projects', value)}
/> />
<TextInput <TextInput
id="skills" {...(register('skills'), {})}
label="Skills" label="Skills"
name="skills"
placeholder="Comments about the Skills section" placeholder="Comments about the Skills section"
type="text" type="text"
onChange={(value) => onValueChange('skills', value)}
/> />
</div> </div>
@ -106,7 +111,12 @@ export default function CommentsForm({
onClick={onCancel} onClick={onCancel}
/> />
<Button label="Submit" type="submit" variant="primary" /> <Button
disabled={!isDirty}
label="Submit"
type="submit"
variant="primary"
/>
</div> </div>
</form> </form>

Loading…
Cancel
Save