@open-tender/utils
Version:
A library of utils for use with Open Tender applications that utilize our cloud-based Order API.
176 lines (175 loc) • 7.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDonationsForm = void 0;
const tslib_1 = require("tslib");
const react_1 = require("react");
const utils_1 = require("../utils");
const useDonationsForm = (purchase, reset, setAlert, loading, error, success, customer, creditCards, includeRecaptcha, cardData, cardType, windowRef, kountSessionId, requireCardholderName = false) => {
const submitRef = (0, react_1.useRef)(null);
const inputRef = (0, react_1.useRef)(null);
const recaptchaRef = (0, react_1.useRef)(null);
const [amount, setAmount] = (0, react_1.useState)('');
const [email, setEmail] = (0, react_1.useState)('');
const [isNewCard, setIsNewCard] = (0, react_1.useState)(true);
const [creditCard, setCreditCard] = (0, react_1.useState)(null);
const [creditCardOptions, setCreditCardOptions] = (0, react_1.useState)([]);
const [errors, setErrors] = (0, react_1.useState)({});
const [submitting, setSubmitting] = (0, react_1.useState)(false);
const errMsg = errors.form && errors.form.includes('parameters')
? 'There are one or more errors below'
: errors.form || null;
const newCardErrors = (0, react_1.useMemo)(() => errors
? Object.entries(errors)
.filter(([key]) => key !== 'form')
.reduce((obj, [key, value]) => (Object.assign(Object.assign({}, obj), { [key]: value })), {})
: {}, [errors]);
(0, react_1.useEffect)(() => {
var _a, _b;
if (loading === 'idle') {
setSubmitting(false);
setAlert({ type: 'close' });
if (error) {
if (recaptchaRef.current)
recaptchaRef.current.reset();
setErrors((0, utils_1.makeFormErrors)(error));
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
}
else if (success) {
windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
}
}
}, [windowRef, loading, error, setAlert, success]);
(0, react_1.useEffect)(() => {
if ((creditCards === null || creditCards === void 0 ? void 0 : creditCards.length) && loading !== 'pending') {
const options = creditCards.map(i => ({
name: `${i.card_type_name} ending in ${i.last4}`,
value: i.customer_card_id
}));
setCreditCardOptions(options);
const defaultCard = creditCards.length
? { customer_card_id: creditCards[0].customer_card_id }
: null;
setCreditCard(defaultCard);
setIsNewCard(false);
}
}, [creditCards, loading]);
(0, react_1.useEffect)(() => {
setEmail((customer === null || customer === void 0 ? void 0 : customer.email) || '');
}, [customer === null || customer === void 0 ? void 0 : customer.email]);
const handleAmount = (value) => {
const cleanValue = (0, utils_1.makeNumeric)(value);
setAmount(cleanValue);
};
const handleEmail = (value) => {
setEmail(value);
};
const handleCreditCard = (value) => {
const customerCardId = parseInt(value);
setCreditCard({ customer_card_id: customerCardId });
};
const purchaseWithCaptcha = (credit_card) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c;
const alert = {
type: 'working',
args: { text: 'Submitting your contribution...' }
};
const formData = {
amount: amount,
email: email,
credit_card,
token: ''
};
if (kountSessionId)
formData.kount_device_session_id = kountSessionId;
if (includeRecaptcha) {
try {
const isEnterpriseRecaptcha = ((_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.props.size) === 'invisible';
const token = isEnterpriseRecaptcha
? yield ((_b = recaptchaRef.current) === null || _b === void 0 ? void 0 : _b.executeAsync())
: (_c = recaptchaRef.current) === null || _c === void 0 ? void 0 : _c.getValue();
if (!token) {
setSubmitting(false);
setErrors({ form: 'Please complete the recaptcha before submitting' });
windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
}
else {
setSubmitting(true);
setAlert(alert);
purchase(Object.assign(Object.assign({}, formData), { token }));
}
}
catch (err) {
setSubmitting(false);
setErrors({ form: 'Please complete the recaptcha before submitting' });
windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
}
}
else {
setSubmitting(true);
setAlert(alert);
purchase(formData);
}
});
const handleSubmit = (evt) => {
var _a, _b, _c;
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
if (!amount || !email) {
setErrors({ form: 'Both amount and email are required' });
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && inputRef.current.focus();
windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
}
else {
if (isNewCard) {
if (cardData && cardType) {
const { card, errors: errs } = (0, utils_1.validateCreditCard)(cardData, cardType, requireCardholderName);
if (errs) {
setErrors(Object.assign(Object.assign({}, errs), { form: 'There are one or more credit card errors below' }));
setSubmitting(false);
windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
}
else {
purchaseWithCaptcha(card);
}
}
}
else {
if (creditCard)
purchaseWithCaptcha(creditCard);
}
((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
}
};
const handleReset = () => {
setErrors({});
setAmount(null);
if (!customer)
setEmail(null);
if (!customer)
setIsNewCard(true);
setCreditCard(null);
setCreditCardOptions([]);
reset();
};
return {
inputRef,
submitRef,
recaptchaRef,
amount,
handleAmount,
email,
handleEmail,
errors,
errMsg,
newCardErrors,
submitting,
isNewCard,
creditCard,
creditCardOptions,
handleCreditCard,
handleSubmit,
handleReset,
setErrors
};
};
exports.useDonationsForm = useDonationsForm;