@open-tender/utils 0.1.59 → 0.1.61

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.
@@ -1,10 +1,12 @@
1
1
  /// <reference types="react" />
2
- import { LoginData, RequestStatus } from '@open-tender/types';
3
- export declare const useLoginForm: (loading: RequestStatus, error: any, login: (email?: string, password?: string) => void, callback?: () => void) => {
2
+ import { FormFields, LoginData, RequestStatus } from '@open-tender/types';
3
+ export declare const useLoginForm: (loading: RequestStatus, error: any, login: (email: string, password?: string) => void, callback?: () => void) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
+ fields: FormFields;
6
7
  data: Partial<LoginData>;
8
+ errors: Record<string, string>;
7
9
  submitting: boolean;
8
- handleChange: (name: string, value: string) => void;
9
- handleSubmit: (evt?: any) => void;
10
+ handleChange: (name: string, value: string | number | boolean) => void;
11
+ handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
10
12
  };
@@ -2,39 +2,58 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useLoginForm = void 0;
4
4
  const react_1 = require("react");
5
+ const utils_1 = require("../utils");
5
6
  const useLoginForm = (loading, error, login, callback) => {
6
7
  const submitRef = (0, react_1.useRef)(null);
7
8
  const inputRef = (0, react_1.useRef)(null);
8
- const [hasSubmit, setHasSubmit] = (0, react_1.useState)(false);
9
9
  const [data, setData] = (0, react_1.useState)({});
10
- const submitting = loading === 'pending';
10
+ const [errors, setErrors] = (0, react_1.useState)({});
11
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
12
+ const fields = [
13
+ {
14
+ label: 'Email',
15
+ name: 'email',
16
+ type: 'email',
17
+ required: true
18
+ },
19
+ {
20
+ label: 'Password',
21
+ name: 'password',
22
+ type: 'password',
23
+ required: true
24
+ }
25
+ ];
11
26
  (0, react_1.useEffect)(() => {
12
27
  var _a, _b;
13
- if (loading === 'idle' && hasSubmit) {
28
+ if (loading === 'idle') {
29
+ setSubmitting(false);
14
30
  if (error) {
15
- setHasSubmit(false);
31
+ setErrors((0, utils_1.makeFormErrors)(error));
16
32
  ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
17
33
  }
18
34
  else if (callback) {
19
35
  callback();
20
36
  }
21
37
  }
22
- }, [loading, error, hasSubmit, callback]);
38
+ }, [loading, error, callback]);
23
39
  const handleChange = (name, value) => {
24
40
  setData(Object.assign(Object.assign({}, data), { [name]: value }));
25
41
  };
26
42
  const handleSubmit = (evt) => {
27
43
  var _a, _b;
28
44
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
29
- setHasSubmit(true);
45
+ setSubmitting(true);
30
46
  const { email, password } = data;
31
- login(email, password);
47
+ if (email)
48
+ login(email, password);
32
49
  ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
33
50
  };
34
51
  return {
35
52
  submitRef,
36
53
  inputRef,
54
+ fields,
37
55
  data,
56
+ errors,
38
57
  submitting,
39
58
  handleChange,
40
59
  handleSubmit
@@ -4,7 +4,7 @@ export declare const useCreditCardForm: (loading: RequestStatus, error: RequestE
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  recaptchaRef: import("react").MutableRefObject<any>;
6
6
  data: CreditCardData | null;
7
- errors: CreditCardErrors | Record<string, string>;
7
+ errors: Record<string, string> | CreditCardErrors;
8
8
  submitting: boolean;
9
9
  handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
10
10
  };
@@ -25,6 +25,7 @@ export declare const checkGuestData: (data: CheckoutFormCustomer, email: string)
25
25
  };
26
26
  guestIncomplete: boolean;
27
27
  };
28
+ export declare const checkNotFound: (errors: Record<string, string>) => boolean | null;
28
29
  export declare const makeRandomNumberString: () => string;
29
30
  export declare const getWidth: () => number;
30
31
  export declare const getHeight: () => number;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hasEntities = exports.convertStringToArray = exports.formatDollars = exports.formatQuantity = exports.addCommas = exports.displayPrice = exports.makeSlides = exports.makeImageUrl = exports.makeImageMap = exports.getHeight = exports.getWidth = exports.makeRandomNumberString = exports.checkGuestData = exports.checkEmpty = exports.validateEmail = exports.dashesToSlashes = exports.slashesToDashes = exports.makeBirthDate = exports.validatePhone = exports.makePhone = exports.cleanPhone = exports.capitalize = exports.serialize = exports.stripTags = exports.slugify = exports.contains = exports.isNum = exports.isEmpty = exports.isObject = exports.isString = void 0;
3
+ exports.hasEntities = exports.convertStringToArray = exports.formatDollars = exports.formatQuantity = exports.addCommas = exports.displayPrice = exports.makeSlides = exports.makeImageUrl = exports.makeImageMap = exports.getHeight = exports.getWidth = exports.makeRandomNumberString = exports.checkNotFound = exports.checkGuestData = exports.checkEmpty = exports.validateEmail = exports.dashesToSlashes = exports.slashesToDashes = exports.makeBirthDate = exports.validatePhone = exports.makePhone = exports.cleanPhone = exports.capitalize = exports.serialize = exports.stripTags = exports.slugify = exports.contains = exports.isNum = exports.isEmpty = exports.isObject = exports.isString = void 0;
4
4
  const isString = (str) => {
5
5
  return typeof str === 'string';
6
6
  };
@@ -129,6 +129,13 @@ const checkGuestData = (data, email) => {
129
129
  return { guestData, guestIncomplete };
130
130
  };
131
131
  exports.checkGuestData = checkGuestData;
132
+ const checkNotFound = (errors) => {
133
+ const errMsg = errors ? errors.email || errors.form : null;
134
+ if (!errMsg)
135
+ return null;
136
+ return (errMsg.includes('not associated') || errMsg.includes('Account not found'));
137
+ };
138
+ exports.checkNotFound = checkNotFound;
132
139
  const makeRandomNumberString = () => Math.floor(Math.random() * 1000000000).toString();
133
140
  exports.makeRandomNumberString = makeRandomNumberString;
134
141
  const getWidth = () => {
@@ -1,10 +1,12 @@
1
1
  /// <reference types="react" />
2
- import { LoginData, RequestStatus } from '@open-tender/types';
3
- export declare const useLoginForm: (loading: RequestStatus, error: any, login: (email?: string, password?: string) => void, callback?: () => void) => {
2
+ import { FormFields, LoginData, RequestStatus } from '@open-tender/types';
3
+ export declare const useLoginForm: (loading: RequestStatus, error: any, login: (email: string, password?: string) => void, callback?: () => void) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
+ fields: FormFields;
6
7
  data: Partial<LoginData>;
8
+ errors: Record<string, string>;
7
9
  submitting: boolean;
8
- handleChange: (name: string, value: string) => void;
9
- handleSubmit: (evt?: any) => void;
10
+ handleChange: (name: string, value: string | number | boolean) => void;
11
+ handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
10
12
  };
@@ -1,37 +1,56 @@
1
1
  import { useState, useEffect, useRef } from 'react';
2
+ import { makeFormErrors } from '../utils';
2
3
  export const useLoginForm = (loading, error, login, callback) => {
3
4
  const submitRef = useRef(null);
4
5
  const inputRef = useRef(null);
5
- const [hasSubmit, setHasSubmit] = useState(false);
6
6
  const [data, setData] = useState({});
7
- const submitting = loading === 'pending';
7
+ const [errors, setErrors] = useState({});
8
+ const [submitting, setSubmitting] = useState(false);
9
+ const fields = [
10
+ {
11
+ label: 'Email',
12
+ name: 'email',
13
+ type: 'email',
14
+ required: true
15
+ },
16
+ {
17
+ label: 'Password',
18
+ name: 'password',
19
+ type: 'password',
20
+ required: true
21
+ }
22
+ ];
8
23
  useEffect(() => {
9
24
  var _a, _b;
10
- if (loading === 'idle' && hasSubmit) {
25
+ if (loading === 'idle') {
26
+ setSubmitting(false);
11
27
  if (error) {
12
- setHasSubmit(false);
28
+ setErrors(makeFormErrors(error));
13
29
  ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
14
30
  }
15
31
  else if (callback) {
16
32
  callback();
17
33
  }
18
34
  }
19
- }, [loading, error, hasSubmit, callback]);
35
+ }, [loading, error, callback]);
20
36
  const handleChange = (name, value) => {
21
37
  setData(Object.assign(Object.assign({}, data), { [name]: value }));
22
38
  };
23
39
  const handleSubmit = (evt) => {
24
40
  var _a, _b;
25
41
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
26
- setHasSubmit(true);
42
+ setSubmitting(true);
27
43
  const { email, password } = data;
28
- login(email, password);
44
+ if (email)
45
+ login(email, password);
29
46
  ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
30
47
  };
31
48
  return {
32
49
  submitRef,
33
50
  inputRef,
51
+ fields,
34
52
  data,
53
+ errors,
35
54
  submitting,
36
55
  handleChange,
37
56
  handleSubmit
@@ -4,7 +4,7 @@ export declare const useCreditCardForm: (loading: RequestStatus, error: RequestE
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  recaptchaRef: import("react").MutableRefObject<any>;
6
6
  data: CreditCardData | null;
7
- errors: CreditCardErrors | Record<string, string>;
7
+ errors: Record<string, string> | CreditCardErrors;
8
8
  submitting: boolean;
9
9
  handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
10
10
  };
@@ -25,6 +25,7 @@ export declare const checkGuestData: (data: CheckoutFormCustomer, email: string)
25
25
  };
26
26
  guestIncomplete: boolean;
27
27
  };
28
+ export declare const checkNotFound: (errors: Record<string, string>) => boolean | null;
28
29
  export declare const makeRandomNumberString: () => string;
29
30
  export declare const getWidth: () => number;
30
31
  export declare const getHeight: () => number;
@@ -108,6 +108,12 @@ export const checkGuestData = (data, email) => {
108
108
  const guestIncomplete = checkEmpty(Object.assign(Object.assign({}, guestData), { phone: validPhone }));
109
109
  return { guestData, guestIncomplete };
110
110
  };
111
+ export const checkNotFound = (errors) => {
112
+ const errMsg = errors ? errors.email || errors.form : null;
113
+ if (!errMsg)
114
+ return null;
115
+ return (errMsg.includes('not associated') || errMsg.includes('Account not found'));
116
+ };
111
117
  export const makeRandomNumberString = () => Math.floor(Math.random() * 1000000000).toString();
112
118
  export const getWidth = () => {
113
119
  return Math.max(document.body.scrollWidth, document.documentElement.scrollWidth, document.body.offsetWidth, document.documentElement.offsetWidth, document.documentElement.clientWidth);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.1.59",
3
+ "version": "0.1.61",
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",