@open-tender/utils 0.1.60 → 0.1.62

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, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useLoginForm: (loading: RequestStatus, error: RequestError, 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
@@ -41,6 +41,7 @@ const useGuestForm = (email, loading, error, checkGuest) => {
41
41
  const handleSubmit = (evt) => {
42
42
  var _a, _b;
43
43
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
44
+ setSubmitting(true);
44
45
  if (data.email)
45
46
  checkGuest(data.email);
46
47
  ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
@@ -1,10 +1,14 @@
1
1
  /// <reference types="react" />
2
- import { RequestStatus } from '@open-tender/types';
3
- export declare const useSendResetForm: (loading: RequestStatus, error: any, sendReset: (email: string, linkUrl: string) => Promise<void>, callback?: () => void, windowref?: any, linkUrlVal?: string) => {
2
+ import { FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useSendResetForm: (loading: RequestStatus, error: RequestError, sendReset: (email: string, linkUrl: string) => Promise<void>, callback?: () => void, windowRef?: Window, linkUrlVal?: string) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
- data: any;
7
- errMsg: any;
8
- handleChange: (name: string, value: string) => void;
9
- handleSubmit: (evt?: any) => void;
6
+ fields: FormFields;
7
+ data: {
8
+ email: string;
9
+ };
10
+ errors: Record<string, string>;
11
+ submitting: boolean;
12
+ handleChange: (name: string, value: string | number | boolean) => void;
13
+ handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
10
14
  };
@@ -2,15 +2,30 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useSendResetForm = void 0;
4
4
  const react_1 = require("react");
5
- const useSendResetForm = (loading, error, sendReset, callback, windowref, linkUrlVal) => {
5
+ const utils_1 = require("../utils");
6
+ const useSendResetForm = (loading, error, sendReset, callback, windowRef, linkUrlVal) => {
6
7
  const submitRef = (0, react_1.useRef)(null);
7
8
  const inputRef = (0, react_1.useRef)(null);
8
- const [data, setData] = (0, react_1.useState)({});
9
- const errMsg = error && error.email ? error.email : null;
9
+ const [data, setData] = (0, react_1.useState)({ email: '' });
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
+ ];
10
20
  (0, react_1.useEffect)(() => {
11
21
  var _a, _b;
12
- if (loading === 'idle' && error) {
13
- ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
22
+ if (loading === 'idle') {
23
+ setSubmitting(false);
24
+ if (error) {
25
+ const errs = (0, utils_1.makeFormErrors)(error);
26
+ setErrors(errs);
27
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
28
+ }
14
29
  }
15
30
  }, [loading, error]);
16
31
  const handleChange = (name, value) => {
@@ -19,8 +34,8 @@ const useSendResetForm = (loading, error, sendReset, callback, windowref, linkUr
19
34
  const handleSubmit = (evt) => {
20
35
  var _a, _b;
21
36
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
22
- const linkUrl = windowref
23
- ? `${windowref.location.origin}/reset-password`
37
+ const linkUrl = windowRef
38
+ ? `${windowRef.location.origin}/reset-password`
24
39
  : `${linkUrlVal}`;
25
40
  sendReset(data.email, linkUrl).then(() => {
26
41
  if (callback)
@@ -31,8 +46,10 @@ const useSendResetForm = (loading, error, sendReset, callback, windowref, linkUr
31
46
  return {
32
47
  submitRef,
33
48
  inputRef,
49
+ fields,
34
50
  data,
35
- errMsg,
51
+ errors,
52
+ submitting,
36
53
  handleChange,
37
54
  handleSubmit
38
55
  };
@@ -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, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useLoginForm: (loading: RequestStatus, error: RequestError, 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
@@ -38,6 +38,7 @@ export const useGuestForm = (email, loading, error, checkGuest) => {
38
38
  const handleSubmit = (evt) => {
39
39
  var _a, _b;
40
40
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
41
+ setSubmitting(true);
41
42
  if (data.email)
42
43
  checkGuest(data.email);
43
44
  ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
@@ -1,10 +1,14 @@
1
1
  /// <reference types="react" />
2
- import { RequestStatus } from '@open-tender/types';
3
- export declare const useSendResetForm: (loading: RequestStatus, error: any, sendReset: (email: string, linkUrl: string) => Promise<void>, callback?: () => void, windowref?: any, linkUrlVal?: string) => {
2
+ import { FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useSendResetForm: (loading: RequestStatus, error: RequestError, sendReset: (email: string, linkUrl: string) => Promise<void>, callback?: () => void, windowRef?: Window, linkUrlVal?: string) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
- data: any;
7
- errMsg: any;
8
- handleChange: (name: string, value: string) => void;
9
- handleSubmit: (evt?: any) => void;
6
+ fields: FormFields;
7
+ data: {
8
+ email: string;
9
+ };
10
+ errors: Record<string, string>;
11
+ submitting: boolean;
12
+ handleChange: (name: string, value: string | number | boolean) => void;
13
+ handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
10
14
  };
@@ -1,13 +1,28 @@
1
1
  import { useState, useEffect, useRef } from 'react';
2
- export const useSendResetForm = (loading, error, sendReset, callback, windowref, linkUrlVal) => {
2
+ import { makeFormErrors } from '../utils';
3
+ export const useSendResetForm = (loading, error, sendReset, callback, windowRef, linkUrlVal) => {
3
4
  const submitRef = useRef(null);
4
5
  const inputRef = useRef(null);
5
- const [data, setData] = useState({});
6
- const errMsg = error && error.email ? error.email : null;
6
+ const [data, setData] = useState({ email: '' });
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
+ ];
7
17
  useEffect(() => {
8
18
  var _a, _b;
9
- if (loading === 'idle' && error) {
10
- ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
19
+ if (loading === 'idle') {
20
+ setSubmitting(false);
21
+ if (error) {
22
+ const errs = makeFormErrors(error);
23
+ setErrors(errs);
24
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
25
+ }
11
26
  }
12
27
  }, [loading, error]);
13
28
  const handleChange = (name, value) => {
@@ -16,8 +31,8 @@ export const useSendResetForm = (loading, error, sendReset, callback, windowref,
16
31
  const handleSubmit = (evt) => {
17
32
  var _a, _b;
18
33
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
19
- const linkUrl = windowref
20
- ? `${windowref.location.origin}/reset-password`
34
+ const linkUrl = windowRef
35
+ ? `${windowRef.location.origin}/reset-password`
21
36
  : `${linkUrlVal}`;
22
37
  sendReset(data.email, linkUrl).then(() => {
23
38
  if (callback)
@@ -28,8 +43,10 @@ export const useSendResetForm = (loading, error, sendReset, callback, windowref,
28
43
  return {
29
44
  submitRef,
30
45
  inputRef,
46
+ fields,
31
47
  data,
32
- errMsg,
48
+ errors,
49
+ submitting,
33
50
  handleChange,
34
51
  handleSubmit
35
52
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.1.60",
3
+ "version": "0.1.62",
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",