UNPKG

@open-tender/utils

Version:

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

152 lines (151 loc) 5.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useSignUpGuestForm = void 0; const react_1 = require("react"); const utils_1 = require("../utils"); const useSignUpForm_1 = require("./useSignUpForm"); const initialData = { first_name: '', last_name: '', phone: '', email: '', password: '' }; const useSignUpGuestForm = (email, guest, loading, error, guestErrors, signUp, submitGuest, tpls, notificationChannels = []) => { const submitRef = (0, react_1.useRef)(null); const inputRef = (0, react_1.useRef)(null); const initData = Object.assign(Object.assign(Object.assign({}, initialData), guest), { email }); const [data, setData] = (0, react_1.useState)(initData); const [comms, setComms] = (0, react_1.useState)([]); const [errors, setErrors] = (0, react_1.useState)({}); const [submitting, setSubmitting] = (0, react_1.useState)(false); const { guestData, guestIncomplete } = (0, utils_1.checkGuestData)(data, email); const hasThanx = tpls === 'THANX'; const hasPunchh = tpls === 'PUNCHH'; const isIncomplete = hasThanx ? guestIncomplete : Object.values(data).filter(i => !i).length > 0; const isLoading = loading === 'pending'; const disabled = isIncomplete || isLoading; const guestDisabled = guestIncomplete || isLoading; const toRemove = []; if (!hasPunchh) toRemove.push('birth_date'); if (hasThanx) toRemove.push('password'); const allFields = [ { label: 'Email', name: 'email', type: 'email', required: true, disabled: true }, { label: 'First Name', name: 'first_name', type: 'text', required: true }, { label: 'Last Name', name: 'last_name', type: 'text', required: true }, { label: 'Phone', name: 'phone', type: 'tel', required: true }, { label: 'Birth Date (mm/dd/yyyy)', name: 'birth_date', type: 'tel', required: false }, { label: 'Password', name: 'password', type: 'password', autoComplete: 'new-password' } ]; const fields = allFields.filter(i => !toRemove.includes(i.name)); const notificationFields = useSignUpForm_1.notificationChannelFields.filter(i => notificationChannels === null || notificationChannels === void 0 ? void 0 : notificationChannels.includes(i.name)); const handleChange = (name, value) => { const val = name === 'phone' ? (0, utils_1.makePhone)(value) : name === 'birth_date' ? (0, utils_1.makeBirthDate)(value) : value; setData(Object.assign(Object.assign({}, data), { [name]: val })); }; const handleSubmit = (evt) => { var _a, _b; evt === null || evt === void 0 ? void 0 : evt.preventDefault(); if (!disabled) { setErrors({}); setSubmitting(true); const values = Object.assign(Object.assign({}, data), { email, communications: comms }); if (values.birth_date) { values.birth_date = (0, utils_1.slashesToDashes)(values.birth_date); } signUp(values); ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur()); } }; const handleGuest = () => { if (!guestDisabled) { setErrors({}); setSubmitting(true); submitGuest(Object.assign(Object.assign({}, guestData), { email })); } }; const handleComms = (name, value) => { const other = comms.filter(i => i !== name); const updated = value ? [...other, name] : other; setComms(updated); }; const resetState = (0, react_1.useCallback)(() => { var _a, _b; if (loading !== 'idle' || !submitting) return; if (error) { const errs = (0, utils_1.makeFormErrors)(error); if (errs.form.includes('parameters')) { errs.form = 'There are one or more errors below'; } setErrors(errs); ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus()); } else if (guestErrors) { setErrors(guestErrors); } setSubmitting(false); // eslint-disable-next-line react-hooks/exhaustive-deps }, [loading, error, guestErrors]); (0, react_1.useEffect)(() => { resetState(); }, [resetState]); (0, react_1.useEffect)(() => { return () => { setData(Object.assign(Object.assign({}, initialData), { email: '' })); setErrors({}); }; }, []); return { submitRef, inputRef, fields, notificationFields, data, comms, errors, disabled, guestDisabled, submitting, handleChange, handleSubmit, handleGuest, handleComms, resetState }; }; exports.useSignUpGuestForm = useSignUpGuestForm;