Skip to content

Commit

Permalink
fix: send captcha to backend to make it effective
Browse files Browse the repository at this point in the history
  • Loading branch information
mamico committed Apr 8, 2022
1 parent a11c5cf commit a9f422b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const SUBMIT_FORM_ACTION = 'SUBMIT_FORM_ACTION';
* @param {Object} data
* @returns {Object} attachments
*/
export function submitForm(path = '', block_id, data, attachments) {
export function submitForm(path = '', block_id, data, attachments, captcha) {
return {
type: SUBMIT_FORM_ACTION,
request: {
Expand All @@ -23,6 +23,7 @@ export function submitForm(path = '', block_id, data, attachments) {
block_id,
data,
attachments,
captcha,
},
},
};
Expand Down
16 changes: 8 additions & 8 deletions src/components/FormView.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useRef } from 'react';
import React, { useCallback } from 'react';
import { useIntl, defineMessages } from 'react-intl';
import {
Segment,
Expand Down Expand Up @@ -46,21 +46,21 @@ const FormView = ({
data,
onSubmit,
resetFormState,
captcha,
}) => {
const intl = useIntl();

const captcha = !!process.env.RAZZLE_HCAPTCHA_KEY
const captcha_provider = !!process.env.RAZZLE_HCAPTCHA_KEY
? 'HCaptcha'
: !!process.env.RAZZLE_RECAPTCHA_KEY
? 'GoogleReCaptcha'
: null;

let validToken = useRef('');
const onVerifyCaptcha = useCallback(
(token) => {
validToken.current = token;
captcha.current = { captcha_provider, token };
},
[validToken],
[captcha],
);

const isValidField = (field) => {
Expand Down Expand Up @@ -165,11 +165,11 @@ const FormView = ({
);
})}

{captcha === 'GoogleReCaptcha' && (
{captcha_provider === 'GoogleReCaptcha' && (
<GoogleReCaptchaWidget onVerify={onVerifyCaptcha} />
)}

{captcha === 'HCaptcha' && (
{captcha_provider === 'HCaptcha' && (
<HCaptchaWidget
sitekey={process.env.RAZZLE_HCAPTCHA_KEY}
onVerify={onVerifyCaptcha}
Expand All @@ -192,7 +192,7 @@ const FormView = ({
primary
type="submit"
disabled={
(captcha && !validToken?.current) || formState.loading
(captcha_provider && !captcha?.current?.token) || formState.loading
}
>
{data.submit_label ||
Expand Down
5 changes: 4 additions & 1 deletion src/components/View.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useReducer } from 'react';
import React, { useState, useEffect, useReducer, useRef } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import PropTypes from 'prop-types';
import { useIntl, defineMessages } from 'react-intl';
Expand Down Expand Up @@ -77,6 +77,7 @@ const View = ({ data, id, path }) => {
const [formState, setFormState] = useReducer(formStateReducer, initialState);
const [formErrors, setFormErrors] = useState([]);
const submitResults = useSelector((state) => state.submitForm);
const captcha = useRef();

const onChangeFormData = (field_id, field, value, extras) => {
setFormData({ field, value: { field_id, value, ...extras } });
Expand Down Expand Up @@ -151,6 +152,7 @@ const View = ({ data, id, path }) => {
...formData[name]
})),
attachments,
captcha.current,
),
);
setFormState({ type: FORM_STATES.loading });
Expand Down Expand Up @@ -189,6 +191,7 @@ const View = ({ data, id, path }) => {
formState={formState}
formErrors={formErrors}
formData={formData}
captcha={captcha}
onChangeFormData={onChangeFormData}
data={data}
onSubmit={submit}
Expand Down

0 comments on commit a9f422b

Please sign in to comment.