Skip to content

Commit

Permalink
added custom recaptcha handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Coderamrin committed Sep 13, 2023
1 parent 15ac346 commit 5c005e4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build": "tsc"
},
"license": "MIT",
"keywords": [
"keywords": [
"forms",
"form",
"form-api",
Expand All @@ -23,8 +23,8 @@
"formbold"
],
"devDependencies": {
"typescript": "^5.1.6",
"@types/react-dom": "^18.2.6"
"@types/react-dom": "^18.2.6",
"typescript": "^5.1.6"
},
"peerDependencies": {
"@types/react": "^18.2.14",
Expand Down
16 changes: 12 additions & 4 deletions src/useForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ const useForm = (formId: string) => {
});
const [succeeded, setSucceeded] = useState(false);

const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
const handleSubmit = (
e: React.FormEvent<HTMLFormElement>,
recaptchaRef?: { current: { getValue: () => any } }
) => {
e.preventDefault();

// Getting the Form data
const data = new FormData(e.currentTarget);
//@ts-ignore
const value = Object.fromEntries(data.entries());
const finalData = { ...value };

// Conditionally add "g-recaptcha-response" if recaptchaRef is provided
if (recaptchaRef) {
finalData["g-recaptcha-response"] = recaptchaRef.current.getValue();
}

//check if the values is empty
//@ts-ignore
Expand All @@ -36,9 +45,8 @@ const useForm = (formId: string) => {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(value),
body: JSON.stringify(finalData),
})
//@ts-ignore
.then((res) => {
setSucceeded(true);
})
Expand All @@ -54,4 +62,4 @@ const useForm = (formId: string) => {
return [{ error, succeeded }, handleSubmit] as const;
};

export { useForm };
export { useForm };

0 comments on commit 5c005e4

Please sign in to comment.