UNPKG

@open-tender/utils

Version:

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

58 lines (57 loc) 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useSendResetForm = void 0; const react_1 = require("react"); const utils_1 = require("../utils"); const useSendResetForm = (loading, error, sendReset, callback, windowRef, linkUrlVal) => { const submitRef = (0, react_1.useRef)(null); const inputRef = (0, react_1.useRef)(null); const [data, setData] = (0, react_1.useState)({ email: '' }); const [errors, setErrors] = (0, react_1.useState)({}); const [submitting, setSubmitting] = (0, react_1.useState)(false); const fields = [ { label: 'Email', name: 'email', type: 'email', required: true } ]; (0, react_1.useEffect)(() => { var _a, _b; if (loading === 'idle') { setSubmitting(false); if (error) { const errs = (0, utils_1.makeFormErrors)(error); 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 (callback) { callback(); } } }, [loading, error, callback]); const handleChange = (name, value) => { setData(Object.assign(Object.assign({}, data), { [name]: value })); }; const handleSubmit = (evt) => { var _a, _b; evt === null || evt === void 0 ? void 0 : evt.preventDefault(); const linkUrl = windowRef ? `${windowRef.location.origin}/reset-password` : `${linkUrlVal}`; sendReset(data.email, linkUrl); ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur()); }; return { submitRef, inputRef, fields, data, errors, submitting, handleChange, handleSubmit }; }; exports.useSendResetForm = useSendResetForm;