@open-tender/utils 0.1.125 → 0.1.127

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ import { OneTimePasscode, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useOneTimePasscodeForm: (otpSent: boolean, loading: RequestStatus, error: RequestError, submit: (data: OneTimePasscode) => void, callback?: () => void) => {
4
+ submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
+ inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
+ fields: import("@open-tender/types").FormFieldType[];
7
+ data: OneTimePasscode;
8
+ errors: Record<string, string>;
9
+ setErrors: import("react").Dispatch<import("react").SetStateAction<Record<string, string>>>;
10
+ submitting: boolean;
11
+ handleChange: (name: string, value: string | number | boolean) => void;
12
+ handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
13
+ };
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useOneTimePasscodeForm = void 0;
4
+ const react_1 = require("react");
5
+ const utils_1 = require("../utils");
6
+ const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback) => {
7
+ const submitRef = (0, react_1.useRef)(null);
8
+ const inputRef = (0, react_1.useRef)(null);
9
+ const [data, setData] = (0, react_1.useState)({
10
+ phone: '',
11
+ one_time_passcode: ''
12
+ });
13
+ const [errors, setErrors] = (0, react_1.useState)({});
14
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
15
+ const toRemove = otpSent ? ['phone'] : ['one_time_passcode'];
16
+ const allFields = [
17
+ { label: 'Phone', name: 'phone', type: 'tel', required: true },
18
+ {
19
+ label: 'One Time Passcode',
20
+ name: 'one_time_passcode',
21
+ type: 'number',
22
+ required: true
23
+ }
24
+ ];
25
+ const fields = allFields.filter(i => !toRemove.includes(i.name));
26
+ (0, react_1.useEffect)(() => {
27
+ var _a, _b;
28
+ if (loading === 'idle' && submitting) {
29
+ setSubmitting(false);
30
+ if (error) {
31
+ setErrors((0, utils_1.makeFormErrors)(error));
32
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
33
+ }
34
+ else if (callback && submitting && otpSent) {
35
+ setErrors({});
36
+ callback();
37
+ }
38
+ else {
39
+ setErrors({});
40
+ }
41
+ }
42
+ }, [loading, error, callback, submitting, otpSent]);
43
+ const handleChange = (name, value) => {
44
+ const inputValue = name === 'phone' ? (0, utils_1.makePhone)((value !== null && value !== void 0 ? value : '')) : value;
45
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
46
+ };
47
+ const handleSubmit = (evt) => {
48
+ var _a, _b;
49
+ evt === null || evt === void 0 ? void 0 : evt.preventDefault();
50
+ setSubmitting(true);
51
+ const { phone, one_time_passcode } = data;
52
+ otpSent ? submit({ one_time_passcode }) : submit({ phone });
53
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
54
+ };
55
+ return {
56
+ submitRef,
57
+ inputRef,
58
+ fields,
59
+ data,
60
+ errors,
61
+ setErrors,
62
+ submitting,
63
+ handleChange,
64
+ handleSubmit
65
+ };
66
+ };
67
+ exports.useOneTimePasscodeForm = useOneTimePasscodeForm;
@@ -25,6 +25,11 @@ const useSignInForm = (email, loading, error, login, callback) => {
25
25
  autoComplete: 'new-password'
26
26
  }
27
27
  ];
28
+ (0, react_1.useEffect)(() => {
29
+ if (!data.email && email) {
30
+ setData({ email, password: data.password });
31
+ }
32
+ }, [email, data.email, data.password]);
28
33
  (0, react_1.useEffect)(() => {
29
34
  if (loading === 'idle') {
30
35
  setSubmitting(false);
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ import { OneTimePasscode, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useOneTimePasscodeForm: (otpSent: boolean, loading: RequestStatus, error: RequestError, submit: (data: OneTimePasscode) => void, callback?: () => void) => {
4
+ submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
+ inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
+ fields: import("@open-tender/types").FormFieldType[];
7
+ data: OneTimePasscode;
8
+ errors: Record<string, string>;
9
+ setErrors: import("react").Dispatch<import("react").SetStateAction<Record<string, string>>>;
10
+ submitting: boolean;
11
+ handleChange: (name: string, value: string | number | boolean) => void;
12
+ handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
13
+ };
@@ -0,0 +1,63 @@
1
+ import { useState, useEffect, useRef } from 'react';
2
+ import { makeFormErrors, makePhone } from '../utils';
3
+ export const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback) => {
4
+ const submitRef = useRef(null);
5
+ const inputRef = useRef(null);
6
+ const [data, setData] = useState({
7
+ phone: '',
8
+ one_time_passcode: ''
9
+ });
10
+ const [errors, setErrors] = useState({});
11
+ const [submitting, setSubmitting] = useState(false);
12
+ const toRemove = otpSent ? ['phone'] : ['one_time_passcode'];
13
+ const allFields = [
14
+ { label: 'Phone', name: 'phone', type: 'tel', required: true },
15
+ {
16
+ label: 'One Time Passcode',
17
+ name: 'one_time_passcode',
18
+ type: 'number',
19
+ required: true
20
+ }
21
+ ];
22
+ const fields = allFields.filter(i => !toRemove.includes(i.name));
23
+ useEffect(() => {
24
+ var _a, _b;
25
+ if (loading === 'idle' && submitting) {
26
+ setSubmitting(false);
27
+ if (error) {
28
+ setErrors(makeFormErrors(error));
29
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
30
+ }
31
+ else if (callback && submitting && otpSent) {
32
+ setErrors({});
33
+ callback();
34
+ }
35
+ else {
36
+ setErrors({});
37
+ }
38
+ }
39
+ }, [loading, error, callback, submitting, otpSent]);
40
+ const handleChange = (name, value) => {
41
+ const inputValue = name === 'phone' ? makePhone((value !== null && value !== void 0 ? value : '')) : value;
42
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
43
+ };
44
+ const handleSubmit = (evt) => {
45
+ var _a, _b;
46
+ evt === null || evt === void 0 ? void 0 : evt.preventDefault();
47
+ setSubmitting(true);
48
+ const { phone, one_time_passcode } = data;
49
+ otpSent ? submit({ one_time_passcode }) : submit({ phone });
50
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
51
+ };
52
+ return {
53
+ submitRef,
54
+ inputRef,
55
+ fields,
56
+ data,
57
+ errors,
58
+ setErrors,
59
+ submitting,
60
+ handleChange,
61
+ handleSubmit
62
+ };
63
+ };
@@ -22,6 +22,11 @@ export const useSignInForm = (email, loading, error, login, callback) => {
22
22
  autoComplete: 'new-password'
23
23
  }
24
24
  ];
25
+ useEffect(() => {
26
+ if (!data.email && email) {
27
+ setData({ email, password: data.password });
28
+ }
29
+ }, [email, data.email, data.password]);
25
30
  useEffect(() => {
26
31
  if (loading === 'idle') {
27
32
  setSubmitting(false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.1.125",
3
+ "version": "0.1.127",
4
4
  "description": "A library of utils for use with Open Tender applications that utilize our cloud-based Order API.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -54,7 +54,7 @@
54
54
  "typescript": "^4.8.2"
55
55
  },
56
56
  "peerDependencies": {
57
- "@open-tender/types": "^0.2.55",
57
+ "@open-tender/types": "^0.2.57",
58
58
  "@reduxjs/toolkit": "^1.8.5",
59
59
  "react": "^18.2.0"
60
60
  },