Skip to content

Commit

Permalink
Merge pull request #270 from dozoisch/enterprise-refactor
Browse files Browse the repository at this point in the history
Refactor enterprise script
  • Loading branch information
dozoisch authored Jun 1, 2023
2 parents 6a2ca83 + 35fdca9 commit c723a18
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
46 changes: 20 additions & 26 deletions src/recaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ export default class ReCAPTCHA extends React.Component {
this.handleRecaptchaRef = this.handleRecaptchaRef.bind(this);
}

getValue() {
let getResponse = this.props.grecaptcha && this.props.grecaptcha.getResponse;
if (this.props.grecaptcha.enterprise) {
getResponse = this.props.grecaptcha.enterprise.getResponse();
getCaptchaFunction(fnName) {
if (this.props.grecaptcha) {
if (this.props.grecaptcha.enterprise) {
return this.props.grecaptcha.enterprise[fnName];
}
return this.props.grecaptcha[fnName];
}
return null;
}

getValue() {
const getResponse = this.getCaptchaFunction("getResponse");
if (getResponse && this._widgetId !== undefined) {
return getResponse(this._widgetId);
}
Expand All @@ -29,13 +36,9 @@ export default class ReCAPTCHA extends React.Component {
}

execute() {
const { grecaptcha } = this.props;
let executer = grecaptcha && grecaptcha.execute;
if (grecaptcha && grecaptcha.enterprise && grecaptcha.enterprise.execute) {
executer = grecaptcha.enterprise.execute;
}
if (executer && this._widgetId !== undefined) {
return executer(this._widgetId);
const execute = this.getCaptchaFunction("execute");
if (execute && this._widgetId !== undefined) {
return execute(this._widgetId);
} else {
this._executeRequested = true;
}
Expand All @@ -50,18 +53,16 @@ export default class ReCAPTCHA extends React.Component {
}

reset() {
let resetter = this.props.grecaptcha.reset;
if (this.props.grecaptcha.enterprise.reset) {
resetter = this.props.grecaptcha.enterprise.reset;
}
if (this.props.grecaptcha && this._widgetId !== undefined) {
const resetter = this.getCaptchaFunction("reset");
if (resetter && this._widgetId !== undefined) {
resetter(this._widgetId);
}
}

forceReset() {
if (this.props.grecaptcha) {
this.props.grecaptcha.reset();
const resetter = this.getCaptchaFunction("reset");
if (resetter) {
resetter();
}
}

Expand Down Expand Up @@ -96,14 +97,7 @@ export default class ReCAPTCHA extends React.Component {
}

explicitRender() {
let render = this.props.grecaptcha && this.props.grecaptcha.render;
if (
this.props.grecaptcha &&
this.props.grecaptcha.enterprise &&
this.props.grecaptcha.enterprise.render
) {
render = this.props.grecaptcha.enterprise.render;
}
const render = this.getCaptchaFunction("render");
if (render && this._widgetId === undefined) {
const wrapper = document.createElement("div");
this._widgetId = render(wrapper, {
Expand Down
21 changes: 12 additions & 9 deletions test/recaptcha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,18 @@ describe("ReCAPTCHA", () => {
it("executeAsync, should return a promise that resolves with the token", () => {
const WIDGET_ID = "someWidgetId";
const TOKEN = "someToken";
const grecaptchaMock = {
render(_, { callback }) {
this.callback = callback;
return WIDGET_ID;
},
execute() {
this.callback(TOKEN);
},
};
const grecaptchaMock = (() => {
let _callback;
return {
render(_, { callback }) {
_callback = callback;
return WIDGET_ID;
},
execute() {
_callback(TOKEN);
},
};
})();
// wrapping component example that applies a ref to ReCAPTCHA
class WrappingComponent extends React.Component {
constructor(props) {
Expand Down

0 comments on commit c723a18

Please sign in to comment.