Skip to content

Commit

Permalink
chore(loader): update loader package to v2 (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
faris-imi authored Feb 26, 2025
1 parent 1597167 commit 0f80eb6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 58 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@
},
"dependencies": {
"@babel/runtime": "^7.17.9",
"@hcaptcha/loader": "^1.2.1"
"@hcaptcha/loader": "^2.0.0"
}
}
13 changes: 0 additions & 13 deletions src/constants.js

This file was deleted.

46 changes: 6 additions & 40 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as React from 'react';
import { hCaptchaLoader, initSentry } from '@hcaptcha/loader';
import { hCaptchaLoader } from '@hcaptcha/loader';

import { getFrame, getMountElement } from './utils.js';
import { breadcrumbMessages, scopeTag } from "./constants";


class HCaptcha extends React.Component {
Expand Down Expand Up @@ -53,13 +52,6 @@ class HCaptcha extends React.Component {

const isApiReady = typeof this._hcaptcha !== 'undefined';

this.sentryHub = initSentry(this.props.sentry, scopeTag);

this.sentryHub.addBreadcrumb({
category: scopeTag.value,
message: breadcrumbMessages.mounted,
});

/*
* Check if hCaptcha has already been loaded,
* If Yes, render the captcha
Expand Down Expand Up @@ -92,11 +84,6 @@ class HCaptcha extends React.Component {
// Reset any stored variables / timers when unmounting
hcaptcha.reset(captchaId);
hcaptcha.remove(captchaId);

this.sentryHub.addBreadcrumb({
category: scopeTag.value,
message: breadcrumbMessages.unmounted,
});
}

shouldComponentUpdate(nextProps, nextState) {
Expand Down Expand Up @@ -213,11 +200,6 @@ class HCaptcha extends React.Component {
}
// Reset captcha state, removes stored token and unticks checkbox
hcaptcha.reset(captchaId)

this.sentryHub.addBreadcrumb({
category: scopeTag.value,
message: breadcrumbMessages.reset,
});
}

removeCaptcha(callback) {
Expand All @@ -234,17 +216,10 @@ class HCaptcha extends React.Component {
hcaptcha.remove(captchaId);
callback && callback()
});


this.sentryHub.addBreadcrumb({
category: scopeTag.value,
message: breadcrumbMessages.removed,
});
}

handleOnLoad () {
this.setState({ isApiReady: true }, () => {
try {
const element = getMountElement(this.props.scriptLocation);
const frame = getFrame(element);

Expand All @@ -258,9 +233,6 @@ class HCaptcha extends React.Component {
const { onLoad } = this.props;
if (onLoad) onLoad();
});
} catch (error) {
this.sentryHub.captureException(error);
}
});
}

Expand Down Expand Up @@ -289,11 +261,6 @@ class HCaptcha extends React.Component {
hcaptcha.reset(captchaId) // If hCaptcha runs into error, reset captcha - hCaptcha

if (onExpire) onExpire();

this.sentryHub.addBreadcrumb({
category: scopeTag.value,
message: breadcrumbMessages.expired,
});
}

handleError (event) {
Expand Down Expand Up @@ -346,14 +313,14 @@ class HCaptcha extends React.Component {
try {
const hcaptcha = this._hcaptcha;
const captchaId = this.captchaId;

if (!this.isReady()) {
const onReady = new Promise((resolve, reject) => {

this._onReady = (id) => {
try {
const hcaptcha = this._hcaptcha;

if (opts && opts.async) {
hcaptcha.execute(id, opts).then(resolve).catch(reject);
} else {
Expand All @@ -364,13 +331,12 @@ class HCaptcha extends React.Component {
}
};
});

return opts?.async ? onReady : null;
}

return hcaptcha.execute(captchaId, opts);
} catch (error) {
this.sentryHub.captureException(error);
if (opts && opts.async) {
return Promise.reject(error);
}
Expand Down

0 comments on commit 0f80eb6

Please sign in to comment.