Skip to content

Commit

Permalink
Merge pull request #33 from collective/honeypot
Browse files Browse the repository at this point in the history
feat: added honeypot field type
  • Loading branch information
SaraBianchi committed Sep 21, 2022
2 parents d8414a8 + 300d6c9 commit 3f72328
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/components/Field.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import PropTypes from 'prop-types';
import { useIntl, defineMessages } from 'react-intl';

import WysiwygWidget from '@plone/volto/components/manage/Widgets/WysiwygWidget';

import EmailWidget from './Widget/EmailWidget';
import FileWidget from './Widget/FileWidget';
import DatetimeWidget from './Widget/DatetimeWidget';
Expand Down
6 changes: 5 additions & 1 deletion src/components/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,13 @@ const View = ({ data, id, path }) => {
.then(() => {
if (isValidForm()) {
let attachments = {};
const captcha = {
let captcha = {
provider: data.captcha,
token: captchaToken.current,
};
if (data.captcha === 'honeypot') {
captcha.value = formData[data.captcha_props.id]?.value ?? '';
}

let formattedFormData = { ...formData };
data.subblocks.forEach((subblock) => {
Expand Down Expand Up @@ -194,6 +197,7 @@ const View = ({ data, id, path }) => {
captchaToken,
captcha: data.captcha,
captcha_props: data.captcha_props,
onChangeFormData,
});

useEffect(() => {
Expand Down
19 changes: 18 additions & 1 deletion src/components/Widget/Captcha.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { createRef } from 'react';
import GoogleReCaptchaWidget from './GoogleReCaptchaWidget';
import HCaptchaWidget from './HCaptchaWidget';
import NoRobotsCaptchaWidget from './NoRobotsCaptchaWidget';
import HoneypotCaptchaWidget from './HoneypotCaptchaWidget';

class Captcha extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -39,7 +40,13 @@ class Captcha extends React.Component {
}

render() {
const { captchaToken, captcha, captcha_props } = this.props;
const {
captchaToken,
captcha,
captcha_props,
onChangeFormData,
} = this.props;

const captchaRef = this.captchaRef;
if (captcha === 'recaptcha') {
return (
Expand Down Expand Up @@ -77,6 +84,16 @@ class Captcha extends React.Component {
captchaToken={captchaToken}
></NoRobotsCaptchaWidget>
);
} else if (captcha === 'honeypot') {
return (
<HoneypotCaptchaWidget
id={captcha_props.id}
title={captcha_props.id}
captchaRef={captchaRef}
captchaToken={captchaToken}
onChangeFormData={onChangeFormData}
/>
);
} else {
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions src/components/Widget/HoneypotCaptchaWidget.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.public-ui .ui.grid > .row.honey-wrapper {
display: none;
}
62 changes: 62 additions & 0 deletions src/components/Widget/HoneypotCaptchaWidget.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* HoneypotCaptchaWidget component.
* @module components/manage/Widgets/HoneypotCaptchaWidget
*/

import React, { useState, useEffect } from 'react';

import TextWidget from '@plone/volto/components/manage/Widgets/TextWidget';
import { Grid } from 'semantic-ui-react';
import './HoneypotCaptchaWidget.css';

/**
* HoneypotCaptchaWidget component class.
* @function HoneypotCaptchaWidget
* @returns {string} Markup of the component.
*/

/*By default, captcha token is setted, and becames empty if user/bot fills the field.
*/
const HoneypotCaptchaWidget = ({
id,
title,
captchaToken,
onChangeFormData,
}) => {
const createToken = (id, value) => {
const token = {
id: id,
value: value,
};
return JSON.stringify(token);
};

useEffect(() => {
captchaToken.current = createToken(id, new Date().toString());
}, [captchaToken, id]);

useEffect(() => {
onChangeFormData(id, id, '', { label: id });
}, []);

const [value, setValue] = useState();
return (
<Grid.Row className="honey-wrapper" key={'honeypot-captcha'}>
<TextWidget
id={id}
name={id}
label={title}
title={title}
onChange={(field, value) => {
//captchaToken.current = createToken(id, value);
captchaToken.current = undefined;
setValue(value);
onChangeFormData(id, field, value, {});
}}
value={value}
/>
</Grid.Row>
);
};

export default HoneypotCaptchaWidget;

0 comments on commit 3f72328

Please sign in to comment.