@open-tender/utils 0.1.24 → 0.1.26

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,12 +1,12 @@
1
1
  /// <reference types="react" />
2
- import { Address, FormFieldType, RequestError, RequestStatus } from '@open-tender/types';
2
+ import { Address, FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
3
  export declare const useAddressForm: (address: Address, loading: RequestStatus, error: RequestError, update: (addressId: number, updatedData: Address, callback: () => void) => void, callback: () => void) => {
4
- fields: FormFieldType[];
4
+ fields: FormFields;
5
5
  data: Address;
6
- errors: any;
6
+ errors: Record<string, string>;
7
7
  submitting: boolean;
8
8
  handleChange: (name: string, value: string | number | boolean) => void;
9
- handleSubmit: (evt?: any) => void;
9
+ handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
10
10
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
11
11
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
12
12
  };
@@ -46,7 +46,7 @@ const useAddressForm = (address, loading, error, update, callback) => {
46
46
  if (loading === 'idle') {
47
47
  setSubmitting(false);
48
48
  if (error) {
49
- setErrors(error);
49
+ setErrors((0, utils_1.makeFormErrors)(error));
50
50
  ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
51
51
  }
52
52
  }
@@ -57,7 +57,7 @@ const useAddressForm = (address, loading, error, update, callback) => {
57
57
  };
58
58
  const handleSubmit = (evt) => {
59
59
  var _a, _b;
60
- evt === null || evt === void 0 ? void 0 : evt.preventDefault();
60
+ evt.preventDefault();
61
61
  setSubmitting(true);
62
62
  const updatedData = Object.assign({}, data);
63
63
  delete updatedData.customer_address_id;
@@ -1,9 +1,9 @@
1
1
  /// <reference types="react" />
2
- import { CardType, CreditCardData, CreditCardSaved, CreditCards, DonationPurchase, Profile, RequestError, RequestStatus } from '@open-tender/types';
2
+ import { CardType, CreditCardData, CreditCardSaved, CreditCards, Customer, DonationPurchase, RequestError, RequestStatus } from '@open-tender/types';
3
3
  export declare const useDonationsForm: (purchase: (data: DonationPurchase, callback?: () => void) => void, reset: () => void, setAlert: (data: {
4
4
  type: string;
5
5
  args?: unknown;
6
- }) => void, loading: RequestStatus, error: RequestError, success: boolean, customer?: Partial<Profile>, creditCards?: CreditCards, recaptchaKey?: string | null, cardData?: CreditCardData, cardType?: CardType, windowRef?: Window) => {
6
+ }) => void, loading: RequestStatus, error: RequestError, success: boolean, customer?: Customer, creditCards?: CreditCards, recaptchaKey?: string | null, cardData?: CreditCardData, cardType?: CardType, windowRef?: Window) => {
7
7
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
8
8
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
9
9
  recaptchaRef: import("react").MutableRefObject<any>;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="react" />
2
- import { CreditCards, CustomerGiftCard, RequestStatus } from '@open-tender/types';
3
- export declare const useGiftCardForm: (giftCard: CustomerGiftCard, creditCards: CreditCards, loading: RequestStatus, error: any, update: (id: number, data: Partial<CustomerGiftCard>, callback: () => void) => void, add: (data: Partial<CustomerGiftCard>, callback: () => void) => void, callback: () => void) => {
2
+ import { CreditCards, GiftCard, GiftCardPurchase, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useGiftCardForm: (giftCard: GiftCard, creditCards: CreditCards, loading: RequestStatus, error: RequestError, update: (id: number, data: GiftCardPurchase, callback: () => void) => void, add: (data: GiftCardPurchase, callback: () => void) => void, callback: () => void) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
6
  options: {
@@ -8,8 +8,8 @@ export declare const useGiftCardForm: (giftCard: CustomerGiftCard, creditCards:
8
8
  value: number;
9
9
  }[];
10
10
  errors: {};
11
- data: Partial<CustomerGiftCard>;
11
+ data: GiftCardPurchase;
12
12
  submitting: boolean;
13
- handleChange: (name: number, type: string, value: string | null, checked: boolean | null) => void;
13
+ handleChange: (name: string, value: string | number | boolean) => void;
14
14
  handleSubmit: (evt?: any) => void;
15
15
  };
@@ -2,11 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useGiftCardForm = void 0;
4
4
  const react_1 = require("react");
5
+ const utils_1 = require("../utils");
5
6
  const useGiftCardForm = (giftCard, creditCards, loading, error, update, add, callback) => {
6
7
  const submitRef = (0, react_1.useRef)(null);
7
8
  const inputRef = (0, react_1.useRef)(null);
8
9
  const customerCardId = creditCards[0].customer_card_id;
9
10
  const [data, setData] = (0, react_1.useState)({
11
+ amount: '',
10
12
  customer_card_id: customerCardId
11
13
  });
12
14
  const [errors, setErrors] = (0, react_1.useState)({});
@@ -22,17 +24,17 @@ const useGiftCardForm = (giftCard, creditCards, loading, error, update, add, cal
22
24
  if (loading === 'idle') {
23
25
  setSubmitting(false);
24
26
  if (error) {
25
- if (error.amount && error.amount.includes('money')) {
26
- error.amount = 'Please enter a positive dollar amount';
27
+ const errs = (0, utils_1.makeFormErrors)(error);
28
+ if (errs.amount && errs.amount.includes('money')) {
29
+ errs.amount = 'Please enter a positive dollar amount';
27
30
  }
28
- setErrors(error);
31
+ setErrors(errs);
29
32
  ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
30
33
  }
31
34
  }
32
35
  }, [loading, error]);
33
- const handleChange = (name, type, value, checked) => {
34
- const inputValue = type === 'checkbox' ? checked : value;
35
- setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
36
+ const handleChange = (name, value) => {
37
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
36
38
  };
37
39
  const handleSubmit = (evt) => {
38
40
  var _a, _b;
@@ -1,22 +1,14 @@
1
1
  /// <reference types="react" />
2
- import { ConfigBrand, Profile, RequestStatus } from '@open-tender/types';
3
- export interface ProfileFormField {
4
- label: string;
5
- name: keyof Profile;
6
- type: string;
7
- required?: boolean;
8
- autoComplete?: string;
9
- }
10
- export declare const useProfileForm: (profile: Partial<Profile>, loading: RequestStatus, error: any, update: (data: Partial<Profile>) => void, optIns?: Partial<ConfigBrand>) => {
2
+ import { ConfigBrand, Customer, CustomerCreate, FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useProfileForm: (profile: Customer, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void, optIns?: Partial<ConfigBrand>) => {
11
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
12
5
  accepts_marketing: import("@open-tender/types").ConfigAcceptMarketing | null | undefined;
13
6
  order_notifications: import("@open-tender/types").ConfigOrderNotifications | null | undefined;
14
- data: Partial<Profile>;
15
- errors: any;
7
+ data: CustomerCreate;
8
+ errors: Record<string, string>;
16
9
  submitting: boolean;
17
- fields: Partial<ProfileFormField>[];
18
- errMsg: string | null;
19
- handleChange: (name: string, type: string, value?: string, checked?: boolean) => void;
10
+ fields: FormFields;
11
+ handleChange: (name: string, value: string | number | boolean) => void;
20
12
  handleRadio: (name: string, value: string) => void;
21
- handleSubmit: (evt?: any) => void;
13
+ handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
22
14
  };
@@ -6,10 +6,9 @@ const utils_1 = require("../utils");
6
6
  const useProfileForm = (profile, loading, error, update, optIns = {}) => {
7
7
  const submitRef = (0, react_1.useRef)(null);
8
8
  const { accepts_marketing, order_notifications } = optIns;
9
- const [data, setData] = (0, react_1.useState)({});
9
+ const [data, setData] = (0, react_1.useState)(profile);
10
10
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
11
- const errMsg = error ? 'There are one or more errors below.' : null;
12
- const errors = error || {};
11
+ const [errors, setErrors] = (0, react_1.useState)({});
13
12
  const fields = [
14
13
  { label: 'First Name', name: 'first_name', type: 'text', required: true },
15
14
  { label: 'Last Name', name: 'last_name', type: 'text', required: true },
@@ -27,21 +26,19 @@ const useProfileForm = (profile, loading, error, update, optIns = {}) => {
27
26
  if (loading === 'idle') {
28
27
  setSubmitting(false);
29
28
  if (error) {
30
- //handle form inputs[0] focus
29
+ setErrors((0, utils_1.makeFormErrors)(error));
31
30
  }
32
31
  }
33
32
  }, [loading, error]);
34
33
  (0, react_1.useEffect)(() => {
35
34
  setData(profile);
36
35
  }, [profile]);
37
- const handleChange = (name, type, value, checked) => {
38
- const inputValue = type === 'checkbox'
39
- ? checked
40
- : name === 'phone'
41
- ? (0, utils_1.makePhone)(value !== null && value !== void 0 ? value : '')
42
- : name === 'birth_date'
43
- ? (0, utils_1.makeBirthDate)(value !== null && value !== void 0 ? value : '')
44
- : value;
36
+ const handleChange = (name, value) => {
37
+ const inputValue = name === 'phone'
38
+ ? (0, utils_1.makePhone)((value !== null && value !== void 0 ? value : ''))
39
+ : name === 'birth_date'
40
+ ? (0, utils_1.makeBirthDate)(value)
41
+ : value;
45
42
  setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
46
43
  };
47
44
  const handleRadio = (name, value) => {
@@ -49,9 +46,16 @@ const useProfileForm = (profile, loading, error, update, optIns = {}) => {
49
46
  };
50
47
  const handleSubmit = (evt) => {
51
48
  var _a, _b;
52
- evt === null || evt === void 0 ? void 0 : evt.preventDefault();
49
+ evt.preventDefault();
53
50
  setSubmitting(true);
54
- const updatedData = fields.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: data[i.name] })), {});
51
+ // const updatedData: any = fields.reduce(
52
+ // (obj: Partial<ProfileFormField>, i: Partial<ProfileFormField>) => ({
53
+ // ...obj,
54
+ // [i.name!]: data[i.name!]
55
+ // }),
56
+ // {}
57
+ // )
58
+ const updatedData = Object.assign({}, data);
55
59
  updatedData.birth_date = data.birth_date
56
60
  ? (0, utils_1.slashesToDashes)(data.birth_date)
57
61
  : null;
@@ -67,7 +71,6 @@ const useProfileForm = (profile, loading, error, update, optIns = {}) => {
67
71
  errors,
68
72
  submitting,
69
73
  fields,
70
- errMsg,
71
74
  handleChange,
72
75
  handleRadio,
73
76
  handleSubmit
@@ -1,19 +1,12 @@
1
1
  /// <reference types="react" />
2
- import { CheckoutConfig, Profile } from '@open-tender/types';
3
- export interface SignUpFormField {
4
- label?: string;
5
- name: string;
6
- type?: string;
7
- required?: boolean;
8
- autoComplete?: string;
9
- }
10
- export declare const useSignUpForm: (loading: RequestInfo, error: any, signUp: (data: any, callback?: () => void) => void, callback?: () => void, checkConfig?: Partial<CheckoutConfig>, hasThanx?: boolean) => {
2
+ import { CheckoutConfig, CustomerCreate, FormFieldType, RequestError } from '@open-tender/types';
3
+ export declare const useSignUpForm: (loading: RequestInfo, error: RequestError, signUp: (data: CustomerCreate, callback?: () => void) => void, callback?: () => void, checkConfig?: Partial<CheckoutConfig>, hasThanx?: boolean) => {
11
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
12
- data: Partial<Profile>;
13
- errors: any;
5
+ data: CustomerCreate;
6
+ errors: Record<string, string>;
14
7
  submitting: boolean;
15
- formfields: SignUpFormField[];
8
+ formfields: FormFieldType[];
16
9
  errMsg: string | null;
17
- handleChange: (name: string, type: string, value: string | null, checked: boolean | null) => void;
10
+ handleChange: (name: string, value: string | number | boolean) => void;
18
11
  handleSubmit: (evt?: any) => void;
19
12
  };
@@ -3,12 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useSignUpForm = void 0;
4
4
  const react_1 = require("react");
5
5
  const utils_1 = require("../utils");
6
+ const initState = {
7
+ first_name: '',
8
+ last_name: '',
9
+ email: '',
10
+ phone: ''
11
+ };
6
12
  const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}, hasThanx = false) => {
7
13
  const { displayed, required } = checkConfig || {};
8
14
  const companyDisplayed = displayed && displayed.customer.includes('company');
9
15
  const companyRequired = required && required.customer.includes('company');
10
16
  const submitRef = (0, react_1.useRef)(null);
11
- const [data, setData] = (0, react_1.useState)({});
17
+ const [data, setData] = (0, react_1.useState)(initState);
12
18
  const [errors, setErrors] = (0, react_1.useState)({});
13
19
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
14
20
  const errMsg = error ? 'There are one or more errors below.' : null;
@@ -55,7 +61,7 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}, hasTh
55
61
  : fields;
56
62
  (0, react_1.useEffect)(() => {
57
63
  return () => {
58
- setData({});
64
+ setData(initState);
59
65
  setErrors({});
60
66
  };
61
67
  }, []);
@@ -63,16 +69,12 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}, hasTh
63
69
  if (loading === 'idle') {
64
70
  setSubmitting(false);
65
71
  if (error) {
66
- setErrors(error);
72
+ setErrors((0, utils_1.makeFormErrors)(error));
67
73
  }
68
74
  }
69
75
  }, [loading, error]);
70
- const handleChange = (name, type, value, checked) => {
71
- const inputValue = type === 'checkbox'
72
- ? checked
73
- : name === 'phone'
74
- ? (0, utils_1.makePhone)(value !== null && value !== void 0 ? value : '')
75
- : value;
76
+ const handleChange = (name, value) => {
77
+ const inputValue = name === 'phone' ? (0, utils_1.makePhone)((value !== null && value !== void 0 ? value : '')) : value;
76
78
  setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
77
79
  };
78
80
  const handleSubmit = (evt) => {
@@ -1,4 +1,4 @@
1
- import { Announcements, ContentImages, GuestData, Images, RequestError, RequestStatus } from '@open-tender/types';
1
+ import { Announcements, ContentImages, DateString, GuestData, Images, RequestError, RequestStatus } from '@open-tender/types';
2
2
  export declare const isString: (str: any) => boolean;
3
3
  export declare const isObject: (obj: any) => boolean;
4
4
  export declare const isEmpty: (obj?: object | null) => boolean;
@@ -11,8 +11,8 @@ export declare const capitalize: (s: string) => string;
11
11
  export declare const cleanPhone: (phone: string) => string;
12
12
  export declare const makePhone: (phone: string) => string;
13
13
  export declare const validatePhone: (phone: string) => boolean;
14
- export declare const makeBirthDate: (birthDate: string) => string;
15
- export declare const slashesToDashes: (birthDate: string) => string | null;
14
+ export declare const makeBirthDate: (birthDate: string) => string | null;
15
+ export declare const slashesToDashes: (birthDate: string) => DateString | null;
16
16
  export declare const dashesToSlashes: (birthDate?: string | null) => string | null;
17
17
  export declare const validateEmail: (email: string) => boolean;
18
18
  export declare const checkEmpty: (data: Record<string | number, unknown>) => boolean;
@@ -83,7 +83,7 @@ const validatePhone = (phone) => {
83
83
  exports.validatePhone = validatePhone;
84
84
  const makeBirthDate = (birthDate) => {
85
85
  if (!birthDate)
86
- return '';
86
+ return null;
87
87
  const b = birthDate.replace(/\D/g, '');
88
88
  if (b.length > 4) {
89
89
  return `${b.slice(0, 2)}/${b.slice(2, 4)}/${b.slice(4, 8)}`;
@@ -1,12 +1,12 @@
1
1
  /// <reference types="react" />
2
- import { Address, FormFieldType, RequestError, RequestStatus } from '@open-tender/types';
2
+ import { Address, FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
3
  export declare const useAddressForm: (address: Address, loading: RequestStatus, error: RequestError, update: (addressId: number, updatedData: Address, callback: () => void) => void, callback: () => void) => {
4
- fields: FormFieldType[];
4
+ fields: FormFields;
5
5
  data: Address;
6
- errors: any;
6
+ errors: Record<string, string>;
7
7
  submitting: boolean;
8
8
  handleChange: (name: string, value: string | number | boolean) => void;
9
- handleSubmit: (evt?: any) => void;
9
+ handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
10
10
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
11
11
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
12
12
  };
@@ -1,5 +1,5 @@
1
1
  import { useState, useEffect, useRef } from 'react';
2
- import { makePhone } from '../utils';
2
+ import { makeFormErrors, makePhone } from '../utils';
3
3
  export const useAddressForm = (address, loading, error, update, callback) => {
4
4
  const submitRef = useRef(null);
5
5
  const inputRef = useRef(null);
@@ -43,7 +43,7 @@ export const useAddressForm = (address, loading, error, update, callback) => {
43
43
  if (loading === 'idle') {
44
44
  setSubmitting(false);
45
45
  if (error) {
46
- setErrors(error);
46
+ setErrors(makeFormErrors(error));
47
47
  ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
48
48
  }
49
49
  }
@@ -54,7 +54,7 @@ export const useAddressForm = (address, loading, error, update, callback) => {
54
54
  };
55
55
  const handleSubmit = (evt) => {
56
56
  var _a, _b;
57
- evt === null || evt === void 0 ? void 0 : evt.preventDefault();
57
+ evt.preventDefault();
58
58
  setSubmitting(true);
59
59
  const updatedData = Object.assign({}, data);
60
60
  delete updatedData.customer_address_id;
@@ -1,9 +1,9 @@
1
1
  /// <reference types="react" />
2
- import { CardType, CreditCardData, CreditCardSaved, CreditCards, DonationPurchase, Profile, RequestError, RequestStatus } from '@open-tender/types';
2
+ import { CardType, CreditCardData, CreditCardSaved, CreditCards, Customer, DonationPurchase, RequestError, RequestStatus } from '@open-tender/types';
3
3
  export declare const useDonationsForm: (purchase: (data: DonationPurchase, callback?: () => void) => void, reset: () => void, setAlert: (data: {
4
4
  type: string;
5
5
  args?: unknown;
6
- }) => void, loading: RequestStatus, error: RequestError, success: boolean, customer?: Partial<Profile>, creditCards?: CreditCards, recaptchaKey?: string | null, cardData?: CreditCardData, cardType?: CardType, windowRef?: Window) => {
6
+ }) => void, loading: RequestStatus, error: RequestError, success: boolean, customer?: Customer, creditCards?: CreditCards, recaptchaKey?: string | null, cardData?: CreditCardData, cardType?: CardType, windowRef?: Window) => {
7
7
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
8
8
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
9
9
  recaptchaRef: import("react").MutableRefObject<any>;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="react" />
2
- import { CreditCards, CustomerGiftCard, RequestStatus } from '@open-tender/types';
3
- export declare const useGiftCardForm: (giftCard: CustomerGiftCard, creditCards: CreditCards, loading: RequestStatus, error: any, update: (id: number, data: Partial<CustomerGiftCard>, callback: () => void) => void, add: (data: Partial<CustomerGiftCard>, callback: () => void) => void, callback: () => void) => {
2
+ import { CreditCards, GiftCard, GiftCardPurchase, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useGiftCardForm: (giftCard: GiftCard, creditCards: CreditCards, loading: RequestStatus, error: RequestError, update: (id: number, data: GiftCardPurchase, callback: () => void) => void, add: (data: GiftCardPurchase, callback: () => void) => void, callback: () => void) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
6
  options: {
@@ -8,8 +8,8 @@ export declare const useGiftCardForm: (giftCard: CustomerGiftCard, creditCards:
8
8
  value: number;
9
9
  }[];
10
10
  errors: {};
11
- data: Partial<CustomerGiftCard>;
11
+ data: GiftCardPurchase;
12
12
  submitting: boolean;
13
- handleChange: (name: number, type: string, value: string | null, checked: boolean | null) => void;
13
+ handleChange: (name: string, value: string | number | boolean) => void;
14
14
  handleSubmit: (evt?: any) => void;
15
15
  };
@@ -1,9 +1,11 @@
1
1
  import { useState, useEffect, useRef } from 'react';
2
+ import { makeFormErrors } from '../utils';
2
3
  export const useGiftCardForm = (giftCard, creditCards, loading, error, update, add, callback) => {
3
4
  const submitRef = useRef(null);
4
5
  const inputRef = useRef(null);
5
6
  const customerCardId = creditCards[0].customer_card_id;
6
7
  const [data, setData] = useState({
8
+ amount: '',
7
9
  customer_card_id: customerCardId
8
10
  });
9
11
  const [errors, setErrors] = useState({});
@@ -19,17 +21,17 @@ export const useGiftCardForm = (giftCard, creditCards, loading, error, update, a
19
21
  if (loading === 'idle') {
20
22
  setSubmitting(false);
21
23
  if (error) {
22
- if (error.amount && error.amount.includes('money')) {
23
- error.amount = 'Please enter a positive dollar amount';
24
+ const errs = makeFormErrors(error);
25
+ if (errs.amount && errs.amount.includes('money')) {
26
+ errs.amount = 'Please enter a positive dollar amount';
24
27
  }
25
- setErrors(error);
28
+ setErrors(errs);
26
29
  ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
27
30
  }
28
31
  }
29
32
  }, [loading, error]);
30
- const handleChange = (name, type, value, checked) => {
31
- const inputValue = type === 'checkbox' ? checked : value;
32
- setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
33
+ const handleChange = (name, value) => {
34
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
33
35
  };
34
36
  const handleSubmit = (evt) => {
35
37
  var _a, _b;
@@ -1,22 +1,14 @@
1
1
  /// <reference types="react" />
2
- import { ConfigBrand, Profile, RequestStatus } from '@open-tender/types';
3
- export interface ProfileFormField {
4
- label: string;
5
- name: keyof Profile;
6
- type: string;
7
- required?: boolean;
8
- autoComplete?: string;
9
- }
10
- export declare const useProfileForm: (profile: Partial<Profile>, loading: RequestStatus, error: any, update: (data: Partial<Profile>) => void, optIns?: Partial<ConfigBrand>) => {
2
+ import { ConfigBrand, Customer, CustomerCreate, FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useProfileForm: (profile: Customer, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void, optIns?: Partial<ConfigBrand>) => {
11
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
12
5
  accepts_marketing: import("@open-tender/types").ConfigAcceptMarketing | null | undefined;
13
6
  order_notifications: import("@open-tender/types").ConfigOrderNotifications | null | undefined;
14
- data: Partial<Profile>;
15
- errors: any;
7
+ data: CustomerCreate;
8
+ errors: Record<string, string>;
16
9
  submitting: boolean;
17
- fields: Partial<ProfileFormField>[];
18
- errMsg: string | null;
19
- handleChange: (name: string, type: string, value?: string, checked?: boolean) => void;
10
+ fields: FormFields;
11
+ handleChange: (name: string, value: string | number | boolean) => void;
20
12
  handleRadio: (name: string, value: string) => void;
21
- handleSubmit: (evt?: any) => void;
13
+ handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
22
14
  };
@@ -1,12 +1,11 @@
1
1
  import { useRef, useState, useEffect } from 'react';
2
- import { makeBirthDate, makePhone, slashesToDashes } from '../utils';
2
+ import { makeBirthDate, makeFormErrors, makePhone, slashesToDashes } from '../utils';
3
3
  export const useProfileForm = (profile, loading, error, update, optIns = {}) => {
4
4
  const submitRef = useRef(null);
5
5
  const { accepts_marketing, order_notifications } = optIns;
6
- const [data, setData] = useState({});
6
+ const [data, setData] = useState(profile);
7
7
  const [submitting, setSubmitting] = useState(false);
8
- const errMsg = error ? 'There are one or more errors below.' : null;
9
- const errors = error || {};
8
+ const [errors, setErrors] = useState({});
10
9
  const fields = [
11
10
  { label: 'First Name', name: 'first_name', type: 'text', required: true },
12
11
  { label: 'Last Name', name: 'last_name', type: 'text', required: true },
@@ -24,21 +23,19 @@ export const useProfileForm = (profile, loading, error, update, optIns = {}) =>
24
23
  if (loading === 'idle') {
25
24
  setSubmitting(false);
26
25
  if (error) {
27
- //handle form inputs[0] focus
26
+ setErrors(makeFormErrors(error));
28
27
  }
29
28
  }
30
29
  }, [loading, error]);
31
30
  useEffect(() => {
32
31
  setData(profile);
33
32
  }, [profile]);
34
- const handleChange = (name, type, value, checked) => {
35
- const inputValue = type === 'checkbox'
36
- ? checked
37
- : name === 'phone'
38
- ? makePhone(value !== null && value !== void 0 ? value : '')
39
- : name === 'birth_date'
40
- ? makeBirthDate(value !== null && value !== void 0 ? value : '')
41
- : value;
33
+ const handleChange = (name, value) => {
34
+ const inputValue = name === 'phone'
35
+ ? makePhone((value !== null && value !== void 0 ? value : ''))
36
+ : name === 'birth_date'
37
+ ? makeBirthDate(value)
38
+ : value;
42
39
  setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
43
40
  };
44
41
  const handleRadio = (name, value) => {
@@ -46,9 +43,16 @@ export const useProfileForm = (profile, loading, error, update, optIns = {}) =>
46
43
  };
47
44
  const handleSubmit = (evt) => {
48
45
  var _a, _b;
49
- evt === null || evt === void 0 ? void 0 : evt.preventDefault();
46
+ evt.preventDefault();
50
47
  setSubmitting(true);
51
- const updatedData = fields.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: data[i.name] })), {});
48
+ // const updatedData: any = fields.reduce(
49
+ // (obj: Partial<ProfileFormField>, i: Partial<ProfileFormField>) => ({
50
+ // ...obj,
51
+ // [i.name!]: data[i.name!]
52
+ // }),
53
+ // {}
54
+ // )
55
+ const updatedData = Object.assign({}, data);
52
56
  updatedData.birth_date = data.birth_date
53
57
  ? slashesToDashes(data.birth_date)
54
58
  : null;
@@ -64,7 +68,6 @@ export const useProfileForm = (profile, loading, error, update, optIns = {}) =>
64
68
  errors,
65
69
  submitting,
66
70
  fields,
67
- errMsg,
68
71
  handleChange,
69
72
  handleRadio,
70
73
  handleSubmit
@@ -1,19 +1,12 @@
1
1
  /// <reference types="react" />
2
- import { CheckoutConfig, Profile } from '@open-tender/types';
3
- export interface SignUpFormField {
4
- label?: string;
5
- name: string;
6
- type?: string;
7
- required?: boolean;
8
- autoComplete?: string;
9
- }
10
- export declare const useSignUpForm: (loading: RequestInfo, error: any, signUp: (data: any, callback?: () => void) => void, callback?: () => void, checkConfig?: Partial<CheckoutConfig>, hasThanx?: boolean) => {
2
+ import { CheckoutConfig, CustomerCreate, FormFieldType, RequestError } from '@open-tender/types';
3
+ export declare const useSignUpForm: (loading: RequestInfo, error: RequestError, signUp: (data: CustomerCreate, callback?: () => void) => void, callback?: () => void, checkConfig?: Partial<CheckoutConfig>, hasThanx?: boolean) => {
11
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
12
- data: Partial<Profile>;
13
- errors: any;
5
+ data: CustomerCreate;
6
+ errors: Record<string, string>;
14
7
  submitting: boolean;
15
- formfields: SignUpFormField[];
8
+ formfields: FormFieldType[];
16
9
  errMsg: string | null;
17
- handleChange: (name: string, type: string, value: string | null, checked: boolean | null) => void;
10
+ handleChange: (name: string, value: string | number | boolean) => void;
18
11
  handleSubmit: (evt?: any) => void;
19
12
  };
@@ -1,11 +1,17 @@
1
1
  import { useRef, useState, useEffect } from 'react';
2
- import { makePhone } from '../utils';
2
+ import { makeFormErrors, makePhone } from '../utils';
3
+ const initState = {
4
+ first_name: '',
5
+ last_name: '',
6
+ email: '',
7
+ phone: ''
8
+ };
3
9
  export const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}, hasThanx = false) => {
4
10
  const { displayed, required } = checkConfig || {};
5
11
  const companyDisplayed = displayed && displayed.customer.includes('company');
6
12
  const companyRequired = required && required.customer.includes('company');
7
13
  const submitRef = useRef(null);
8
- const [data, setData] = useState({});
14
+ const [data, setData] = useState(initState);
9
15
  const [errors, setErrors] = useState({});
10
16
  const [submitting, setSubmitting] = useState(false);
11
17
  const errMsg = error ? 'There are one or more errors below.' : null;
@@ -52,7 +58,7 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}
52
58
  : fields;
53
59
  useEffect(() => {
54
60
  return () => {
55
- setData({});
61
+ setData(initState);
56
62
  setErrors({});
57
63
  };
58
64
  }, []);
@@ -60,16 +66,12 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}
60
66
  if (loading === 'idle') {
61
67
  setSubmitting(false);
62
68
  if (error) {
63
- setErrors(error);
69
+ setErrors(makeFormErrors(error));
64
70
  }
65
71
  }
66
72
  }, [loading, error]);
67
- const handleChange = (name, type, value, checked) => {
68
- const inputValue = type === 'checkbox'
69
- ? checked
70
- : name === 'phone'
71
- ? makePhone(value !== null && value !== void 0 ? value : '')
72
- : value;
73
+ const handleChange = (name, value) => {
74
+ const inputValue = name === 'phone' ? makePhone((value !== null && value !== void 0 ? value : '')) : value;
73
75
  setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
74
76
  };
75
77
  const handleSubmit = (evt) => {
@@ -1,4 +1,4 @@
1
- import { Announcements, ContentImages, GuestData, Images, RequestError, RequestStatus } from '@open-tender/types';
1
+ import { Announcements, ContentImages, DateString, GuestData, Images, RequestError, RequestStatus } from '@open-tender/types';
2
2
  export declare const isString: (str: any) => boolean;
3
3
  export declare const isObject: (obj: any) => boolean;
4
4
  export declare const isEmpty: (obj?: object | null) => boolean;
@@ -11,8 +11,8 @@ export declare const capitalize: (s: string) => string;
11
11
  export declare const cleanPhone: (phone: string) => string;
12
12
  export declare const makePhone: (phone: string) => string;
13
13
  export declare const validatePhone: (phone: string) => boolean;
14
- export declare const makeBirthDate: (birthDate: string) => string;
15
- export declare const slashesToDashes: (birthDate: string) => string | null;
14
+ export declare const makeBirthDate: (birthDate: string) => string | null;
15
+ export declare const slashesToDashes: (birthDate: string) => DateString | null;
16
16
  export declare const dashesToSlashes: (birthDate?: string | null) => string | null;
17
17
  export declare const validateEmail: (email: string) => boolean;
18
18
  export declare const checkEmpty: (data: Record<string | number, unknown>) => boolean;
@@ -68,7 +68,7 @@ export const validatePhone = (phone) => {
68
68
  };
69
69
  export const makeBirthDate = (birthDate) => {
70
70
  if (!birthDate)
71
- return '';
71
+ return null;
72
72
  const b = birthDate.replace(/\D/g, '');
73
73
  if (b.length > 4) {
74
74
  return `${b.slice(0, 2)}/${b.slice(2, 4)}/${b.slice(4, 8)}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.1.24",
3
+ "version": "0.1.26",
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",