2022-01-11 09:14:08 +00:00
|
|
|
/*
|
|
|
|
* Global Captcha that is valid sitewide
|
|
|
|
* via api/captcha
|
|
|
|
* Displayed in an Alert
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { t } from 'ttag';
|
|
|
|
|
|
|
|
import Captcha from './Captcha';
|
2022-09-21 22:55:13 +00:00
|
|
|
import {
|
|
|
|
requestSolveCaptcha,
|
|
|
|
requestBanMe,
|
|
|
|
} from '../store/actions/fetch';
|
2022-01-11 09:14:08 +00:00
|
|
|
|
|
|
|
const GlobalCaptcha = ({ close }) => {
|
|
|
|
const [errors, setErrors] = useState([]);
|
2022-09-21 22:55:13 +00:00
|
|
|
const [legit, setLegit] = useState(false);
|
2022-01-11 09:14:08 +00:00
|
|
|
// used to be able to force Captcha rerender on error
|
|
|
|
const [captKey, setCaptKey] = useState(Date.now());
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form
|
|
|
|
onSubmit={async (e) => {
|
|
|
|
e.preventDefault();
|
2022-09-21 22:55:13 +00:00
|
|
|
// ----
|
|
|
|
const test = document.getElementById('void-bot');
|
|
|
|
if (!legit || test) {
|
|
|
|
await requestBanMe((legit) ? 1 : 2);
|
|
|
|
}
|
|
|
|
// ----
|
2022-01-11 09:14:08 +00:00
|
|
|
const text = e.target.captcha.value;
|
|
|
|
const captchaid = e.target.captchaid.value;
|
|
|
|
const { errors: resErrors } = await requestSolveCaptcha(
|
|
|
|
text,
|
|
|
|
captchaid,
|
|
|
|
);
|
|
|
|
if (resErrors) {
|
|
|
|
setCaptKey(Date.now());
|
|
|
|
setErrors(resErrors);
|
|
|
|
} else {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{errors.map((error) => (
|
|
|
|
<p key={error} className="errormessage">
|
|
|
|
<span>{t`Error`}</span>: {error}
|
|
|
|
</p>
|
|
|
|
))}
|
2022-09-21 22:55:13 +00:00
|
|
|
<Captcha autoload key={captKey} setLegit={setLegit} />
|
2022-01-11 09:14:08 +00:00
|
|
|
<p>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={close}
|
|
|
|
>
|
|
|
|
{t`Cancel`}
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
>
|
|
|
|
{t`Send`}
|
|
|
|
</button>
|
|
|
|
</p>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.memo(GlobalCaptcha);
|