@open-tender/utils
Version:
A library of utils for use with Open Tender applications that utilize our cloud-based Order API.
203 lines (202 loc) • 7.51 kB
JavaScript
import { __awaiter } from "tslib";
import { useRef, useState, useEffect } from 'react';
import { makeBirthDate, makeFormErrors, makePhone, slashesToDashes } from '../utils';
const initState = {
first_name: '',
last_name: '',
email: '',
phone: ''
};
export const notificationChannelFields = [
{
label: 'Email',
name: 'EMAIL',
type: 'checkbox'
},
{
label: 'SMS / Text',
name: 'SMS',
type: 'checkbox'
},
{
label: 'Push Notifications',
name: 'PUSH',
type: 'checkbox'
}
];
export const useSignUpForm = (loading, error, signUp, callback, checkConfig, hidePassword = false, optionalFields, notificationChannels, passwordRequirements, tpls, includeRecaptcha, getRecaptchaToken, includeLoginRecaptcha, getLoginRecaptchaToken, require_home_store) => {
const submitRef = useRef(null);
const [data, setData] = useState(initState);
const [comms, setComms] = useState([]);
const [errors, setErrors] = useState({});
const [submitting, setSubmitting] = 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 = notificationChannelFields.filter(i => notificationChannels === null || notificationChannels === void 0 ? void 0 : notificationChannels.includes(i.name));
useEffect(() => {
return () => {
setData(initState);
setErrors({});
};
}, []);
useEffect(() => {
if (loading === 'idle') {
setSubmitting(false);
if (error) {
const errs = 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'
? makePhone((value !== null && value !== void 0 ? value : ''))
: name === 'birth_date'
? 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) => __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 = 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
};
};