Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make field level error work #3

Open
wants to merge 2 commits into
base: contactus
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/components/ContactForm/ContactForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface ContactFormInput {
export function ContactForm(props: ContactFormProps) {
const { inputs, button } = props;
const [captchaToken, setCaptchaToken] = useState<string | null>(null);
const { message, onSubmit } = useContactForm(captchaToken);
const { message, onSubmit, onError } = useContactForm(captchaToken);

const contactFormDefaults: ContactFormInput = {
suppliedname: "",
Expand All @@ -51,14 +51,13 @@ export function ContactForm(props: ContactFormProps) {
[captchaToken, button]
);

// const onError = (errors) => console.log(errors);

return (
<Form>
<FormContent
defaultValues={contactFormDefaults}
inputs={inputs}
// onError={onError}
onError={onError}
onSubmit={onSubmit}
/>
<Recaptcha onChange={handleCaptchaTokenChange} />
Expand All @@ -67,7 +66,7 @@ export function ContactForm(props: ContactFormProps) {
defaultValues={contactFormDefaults}
inputs={inputs.slice(inputs.length)}
buttons={modifiedButtonProps}
// onError={onError}
onError={onError}
onSubmit={onSubmit}
/>
</Form>
Expand Down
2 changes: 1 addition & 1 deletion src/components/ContactForm/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './ContactForm';
export * from './ContactForm';
25 changes: 24 additions & 1 deletion src/hooks/contact/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,33 @@ import { FieldValues } from "react-hook-form";
import { useRouter } from "next/router";
import { postContactData } from "../../lib";


interface ErrorDetail {
type: string;
message: string;
ref: {
name: string;
};
}

interface Errors<T extends ErrorDetail> {
[key: string]: T;
}

export const useContactForm = (captchaToken: string | null) => {
const [message, setMessage] = useState<string>("");
const router = useRouter();

const onError = <T extends ErrorDetail>(errors: Errors<T>): void => {
const errorKeys = Object.keys(errors);
errorKeys.forEach((key) => {
const error = errors[key];
const name = error?.ref?.name || '';
const message = error?.message || '';
console.log("Error", message, name);
})
};

const onSubmit = async (data: FieldValues) => {
if (!captchaToken) {
setMessage("Please verify you are human");
Expand All @@ -30,5 +53,5 @@ export const useContactForm = (captchaToken: string | null) => {
}
};

return { message, onSubmit };
return { message, onSubmit , onError };
};