@open-tender/utils 0.1.37 → 0.1.39

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.
@@ -35,7 +35,7 @@ const useCreditCard = (initState, cardErrors = {}, disabled = false) => {
35
35
  setErrors(Object.assign(Object.assign({}, errors), { [name]: error }));
36
36
  }
37
37
  else {
38
- setErrors(Object.assign(Object.assign({}, errors), { [name]: null }));
38
+ setErrors({});
39
39
  }
40
40
  };
41
41
  return {
@@ -1,9 +1,10 @@
1
1
  /// <reference types="react" />
2
- import { CardType, CreditCardData, RequestStatus } from '@open-tender/types';
3
- export declare const useCreditCardForm: (loading: RequestStatus, error: any, data: CreditCardData, cardType: CardType, setErrors: (errors: any) => void, addCard: (data: CreditCardData, callback: () => void) => void, callback: () => void, recaptchaKey: string | null, revenue_center_id: number | null, windowRef?: Window) => {
2
+ import { CardType, CreditCardData, CreditCardErrors, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useCreditCardForm: (loading: RequestStatus, error: RequestError, data: CreditCardData | null, cardType: CardType, addCard: (data: CreditCardData, callback: () => void) => void, callback: () => void, recaptchaKey: string | null, revenue_center_id: number | null) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  recaptchaRef: import("react").MutableRefObject<any>;
6
- data: CreditCardData;
6
+ data: CreditCardData | null;
7
+ errors: CreditCardErrors | Record<string, string>;
7
8
  submitting: boolean;
8
- handleSubmit: (evt?: any) => void;
9
+ handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
9
10
  };
@@ -3,32 +3,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useCreditCardForm = void 0;
4
4
  const react_1 = require("react");
5
5
  const __1 = require("../");
6
- const useCreditCardForm = (loading, error, data, cardType, setErrors, addCard, callback, recaptchaKey, revenue_center_id, windowRef) => {
6
+ const useCreditCardForm = (loading, error, data, cardType, addCard, callback, recaptchaKey, revenue_center_id) => {
7
7
  const submitRef = (0, react_1.useRef)(null);
8
8
  const recaptchaRef = (0, react_1.useRef)(null);
9
9
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
10
+ const [errors, setErrors] = (0, react_1.useState)({});
10
11
  (0, react_1.useEffect)(() => {
11
12
  if (loading === 'idle') {
12
13
  setSubmitting(false);
13
14
  if (error) {
15
+ setErrors((0, __1.makeFormErrors)(error));
14
16
  if (recaptchaRef.current)
15
17
  recaptchaRef.current.reset();
16
- setErrors(error);
17
- if (windowRef)
18
- windowRef.scrollTo({ top: 0 });
19
18
  }
20
19
  }
21
- }, [windowRef, loading, error, setErrors]);
20
+ }, [loading, error]);
22
21
  const handleSubmit = (evt) => {
23
22
  var _a, _b, _c;
24
23
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
25
24
  setErrors({});
25
+ if (!data)
26
+ return;
26
27
  const { card, errors: cardErrors } = (0, __1.validateCreditCard)(data, cardType);
27
28
  if (cardErrors) {
28
29
  setErrors(cardErrors);
29
30
  setSubmitting(false);
30
- if (windowRef)
31
- windowRef.scrollTo({ top: 0 });
32
31
  }
33
32
  else {
34
33
  const cardRc = revenue_center_id ? Object.assign(Object.assign({}, card), { revenue_center_id }) : card;
@@ -40,8 +39,6 @@ const useCreditCardForm = (loading, error, data, cardType, setErrors, addCard, c
40
39
  setErrors({
41
40
  form: 'Please complete the recaptcha before submitting'
42
41
  });
43
- if (windowRef)
44
- windowRef.scrollTo({ top: 0 });
45
42
  }
46
43
  else {
47
44
  setSubmitting(true);
@@ -53,8 +50,6 @@ const useCreditCardForm = (loading, error, data, cardType, setErrors, addCard, c
53
50
  setErrors({
54
51
  form: 'Please complete the recaptcha before submitting'
55
52
  });
56
- if (windowRef)
57
- windowRef.scrollTo({ top: 0 });
58
53
  }
59
54
  }
60
55
  else {
@@ -68,6 +63,7 @@ const useCreditCardForm = (loading, error, data, cardType, setErrors, addCard, c
68
63
  submitRef,
69
64
  recaptchaRef,
70
65
  data,
66
+ errors,
71
67
  submitting,
72
68
  handleSubmit
73
69
  };
@@ -47,9 +47,6 @@ const useProfileForm = (profile, loading, error, update
47
47
  }
48
48
  setErrors(errs);
49
49
  }
50
- else {
51
- setErrors({});
52
- }
53
50
  }
54
51
  }, [loading, error]);
55
52
  (0, react_1.useEffect)(() => {
@@ -32,7 +32,7 @@ export const useCreditCard = (initState, cardErrors = {}, disabled = false) => {
32
32
  setErrors(Object.assign(Object.assign({}, errors), { [name]: error }));
33
33
  }
34
34
  else {
35
- setErrors(Object.assign(Object.assign({}, errors), { [name]: null }));
35
+ setErrors({});
36
36
  }
37
37
  };
38
38
  return {
@@ -1,9 +1,10 @@
1
1
  /// <reference types="react" />
2
- import { CardType, CreditCardData, RequestStatus } from '@open-tender/types';
3
- export declare const useCreditCardForm: (loading: RequestStatus, error: any, data: CreditCardData, cardType: CardType, setErrors: (errors: any) => void, addCard: (data: CreditCardData, callback: () => void) => void, callback: () => void, recaptchaKey: string | null, revenue_center_id: number | null, windowRef?: Window) => {
2
+ import { CardType, CreditCardData, CreditCardErrors, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useCreditCardForm: (loading: RequestStatus, error: RequestError, data: CreditCardData | null, cardType: CardType, addCard: (data: CreditCardData, callback: () => void) => void, callback: () => void, recaptchaKey: string | null, revenue_center_id: number | null) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  recaptchaRef: import("react").MutableRefObject<any>;
6
- data: CreditCardData;
6
+ data: CreditCardData | null;
7
+ errors: CreditCardErrors | Record<string, string>;
7
8
  submitting: boolean;
8
- handleSubmit: (evt?: any) => void;
9
+ handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
9
10
  };
@@ -1,31 +1,30 @@
1
1
  import { useState, useEffect, useRef } from 'react';
2
- import { validateCreditCard } from '../';
3
- export const useCreditCardForm = (loading, error, data, cardType, setErrors, addCard, callback, recaptchaKey, revenue_center_id, windowRef) => {
2
+ import { makeFormErrors, validateCreditCard } from '../';
3
+ export const useCreditCardForm = (loading, error, data, cardType, addCard, callback, recaptchaKey, revenue_center_id) => {
4
4
  const submitRef = useRef(null);
5
5
  const recaptchaRef = useRef(null);
6
6
  const [submitting, setSubmitting] = useState(false);
7
+ const [errors, setErrors] = useState({});
7
8
  useEffect(() => {
8
9
  if (loading === 'idle') {
9
10
  setSubmitting(false);
10
11
  if (error) {
12
+ setErrors(makeFormErrors(error));
11
13
  if (recaptchaRef.current)
12
14
  recaptchaRef.current.reset();
13
- setErrors(error);
14
- if (windowRef)
15
- windowRef.scrollTo({ top: 0 });
16
15
  }
17
16
  }
18
- }, [windowRef, loading, error, setErrors]);
17
+ }, [loading, error]);
19
18
  const handleSubmit = (evt) => {
20
19
  var _a, _b, _c;
21
20
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
22
21
  setErrors({});
22
+ if (!data)
23
+ return;
23
24
  const { card, errors: cardErrors } = validateCreditCard(data, cardType);
24
25
  if (cardErrors) {
25
26
  setErrors(cardErrors);
26
27
  setSubmitting(false);
27
- if (windowRef)
28
- windowRef.scrollTo({ top: 0 });
29
28
  }
30
29
  else {
31
30
  const cardRc = revenue_center_id ? Object.assign(Object.assign({}, card), { revenue_center_id }) : card;
@@ -37,8 +36,6 @@ export const useCreditCardForm = (loading, error, data, cardType, setErrors, add
37
36
  setErrors({
38
37
  form: 'Please complete the recaptcha before submitting'
39
38
  });
40
- if (windowRef)
41
- windowRef.scrollTo({ top: 0 });
42
39
  }
43
40
  else {
44
41
  setSubmitting(true);
@@ -50,8 +47,6 @@ export const useCreditCardForm = (loading, error, data, cardType, setErrors, add
50
47
  setErrors({
51
48
  form: 'Please complete the recaptcha before submitting'
52
49
  });
53
- if (windowRef)
54
- windowRef.scrollTo({ top: 0 });
55
50
  }
56
51
  }
57
52
  else {
@@ -65,6 +60,7 @@ export const useCreditCardForm = (loading, error, data, cardType, setErrors, add
65
60
  submitRef,
66
61
  recaptchaRef,
67
62
  data,
63
+ errors,
68
64
  submitting,
69
65
  handleSubmit
70
66
  };
@@ -44,9 +44,6 @@ export const useProfileForm = (profile, loading, error, update
44
44
  }
45
45
  setErrors(errs);
46
46
  }
47
- else {
48
- setErrors({});
49
- }
50
47
  }
51
48
  }, [loading, error]);
52
49
  useEffect(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.1.37",
3
+ "version": "0.1.39",
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",