UNPKG

@open-tender/utils

Version:

A library of utils for use with Open Tender applications that utilize our cloud-based Order API.

207 lines (206 loc) 7.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useSignUpForm = exports.notificationChannelFields = void 0; const tslib_1 = require("tslib"); const react_1 = require("react"); const utils_1 = require("../utils"); const initState = { first_name: '', last_name: '', email: '', phone: '' }; exports.notificationChannelFields = [ { label: 'Email', name: 'EMAIL', type: 'checkbox' }, { label: 'SMS / Text', name: 'SMS', type: 'checkbox' }, { label: 'Push Notifications', name: 'PUSH', type: 'checkbox' } ]; const useSignUpForm = (loading, error, signUp, callback, checkConfig, hidePassword = false, optionalFields, notificationChannels, passwordRequirements, tpls, includeRecaptcha, getRecaptchaToken, includeLoginRecaptcha, getLoginRecaptchaToken, require_home_store) => { const submitRef = (0, react_1.useRef)(null); const [data, setData] = (0, react_1.useState)(initState); const [comms, setComms] = (0, react_1.useState)([]); const [errors, setErrors] = (0, react_1.useState)({}); const [submitting, setSubmitting] = (0, react_1.useState)(false); const { displayed, required } = checkConfig || {}; const hasPunchh = tpls === 'PUNCHH'; const companyDisplayed = displayed && displayed.customer.includes('company'); const companyRequired = required && required.customer.includes('company'); const showCompany = companyDisplayed || companyRequired; const toRemove = []; if (!showCompany) toRemove.push('company'); if (hidePassword) toRemove.push('password'); if (optionalFields && !optionalFields.includes('birth_date')) toRemove.push('birth_date'); const allFields = [ { label: 'First Name', name: 'first_name', type: 'text', required: true }, { label: 'Last Name', name: 'last_name', type: 'text', required: true }, { label: 'Email', name: 'email', type: 'email', required: true, autoComplete: 'email' }, { label: `Password${passwordRequirements || ''}`, name: 'password', type: 'password', required: true, autoComplete: 'new-password' }, { label: 'Phone', name: 'phone', type: 'tel', required: true }, { label: 'Company', name: 'company', type: 'text', required: companyRequired }, { label: 'Birth Date (mm/dd/yyyy)', name: 'birth_date', type: 'tel', required: hasPunchh } ]; const fields = allFields.filter(i => !toRemove.includes(i.name)); const notificationFields = exports.notificationChannelFields.filter(i => notificationChannels === null || notificationChannels === void 0 ? void 0 : notificationChannels.includes(i.name)); (0, react_1.useEffect)(() => { return () => { setData(initState); setErrors({}); }; }, []); (0, react_1.useEffect)(() => { if (loading === 'idle') { setSubmitting(false); if (error) { const errs = (0, utils_1.makeFormErrors)(error); if (errs.birth_date) { errs.birth_date = 'Invalid date. Please enter in mm/dd/yyyy format or leave blank.'; } setErrors(errs); } } }, [loading, error]); const validateRequiredFields = (values) => { var _a, _b, _c, _d, _e, _f; const validationErrors = {}; // Check non-optional fields from CustomerCreate if (!((_a = values.first_name) === null || _a === void 0 ? void 0 : _a.trim())) { validationErrors.first_name = 'First name is required'; } if (!((_b = values.last_name) === null || _b === void 0 ? void 0 : _b.trim())) { validationErrors.last_name = 'Last name is required'; } if (!((_c = values.email) === null || _c === void 0 ? void 0 : _c.trim())) { validationErrors.email = 'Email is required'; } if (!((_d = values.phone) === null || _d === void 0 ? void 0 : _d.trim())) { validationErrors.phone = 'Phone is required'; } // Check password if not hidden if (!((_e = values.password) === null || _e === void 0 ? void 0 : _e.trim())) { validationErrors.password = 'Password is required'; } // Check company if required if (companyRequired && !((_f = values.company) === null || _f === void 0 ? void 0 : _f.trim())) { validationErrors.company = 'Company is required'; } return validationErrors; }; const handleChange = (name, value) => { const inputValue = name === 'phone' ? (0, utils_1.makePhone)((value !== null && value !== void 0 ? value : '')) : name === 'birth_date' ? (0, utils_1.makeBirthDate)(value) : value; setData(Object.assign(Object.assign({}, data), { [name]: inputValue })); }; const handleComms = (name, value) => { const other = comms.filter(i => i !== name); const updated = value ? [...other, name] : other; setComms(updated); }; const handleSubmit = (evt) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c; evt === null || evt === void 0 ? void 0 : evt.preventDefault(); setErrors({}); const values = Object.assign(Object.assign({}, data), { communications: comms }); if (values.birth_date) { values.birth_date = (0, utils_1.slashesToDashes)(values.birth_date); } const validationErrors = validateRequiredFields(values); if (require_home_store && !data.home_store) { validationErrors.home_store = 'Please select a home store'; } if (Object.keys(validationErrors).length > 0) { setErrors(validationErrors); return; } const loginToken = includeLoginRecaptcha ? (_a = (yield (getLoginRecaptchaToken === null || getLoginRecaptchaToken === void 0 ? void 0 : getLoginRecaptchaToken()))) !== null && _a !== void 0 ? _a : undefined : undefined; if (includeRecaptcha) { try { const token = yield (getRecaptchaToken === null || getRecaptchaToken === void 0 ? void 0 : getRecaptchaToken()); if (!token) { setErrors({ form: 'Please complete the recaptcha before submitting' }); } else { setSubmitting(true); signUp(values, callback, token, loginToken); } } catch (err) { setErrors({ form: 'Please complete the recaptcha before submitting' }); } } else { setSubmitting(true); signUp(values, callback, undefined, loginToken); } ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur()); }); return { submitRef, data, comms, errors, submitting, fields, notificationFields, handleChange, handleComms, handleSubmit }; }; exports.useSignUpForm = useSignUpForm;