@open-tender/utils 0.1.132 → 0.1.134

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,15 +1,15 @@
1
1
  /// <reference types="react" />
2
- import { Customer, CustomerCreate, FormFields, Gender, RequestError, RequestStatus } from '@open-tender/types';
2
+ import { Customer, CustomerCreate, FormFieldType, Gender, RequestError, RequestStatus } from '@open-tender/types';
3
3
  export declare const genderOptions: {
4
4
  name: string;
5
5
  value: Gender;
6
6
  }[];
7
- export declare const useProfileForm: (profile: Customer | null, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void, callback?: ((data: CustomerCreate) => void) | undefined) => {
7
+ export declare const useProfileForm: (profile: Customer | null, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void, callback?: ((data: CustomerCreate) => void) | undefined, optionalFields?: string[]) => {
8
8
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
9
9
  data: Customer | null;
10
10
  errors: Record<string, string>;
11
11
  submitting: boolean;
12
- fields: FormFields;
12
+ fields: FormFieldType[];
13
13
  handleChange: (name: string, value: string | number | boolean) => void;
14
14
  handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
15
15
  };
@@ -8,7 +8,7 @@ exports.genderOptions = [
8
8
  { name: 'Male', value: 'MALE' },
9
9
  { name: 'Female', value: 'FEMALE' }
10
10
  ];
11
- const fields = [
11
+ const allFields = [
12
12
  { label: 'First Name', name: 'first_name', type: 'text', required: true },
13
13
  { label: 'Last Name', name: 'last_name', type: 'text', required: true },
14
14
  {
@@ -37,17 +37,21 @@ const makePayload = (fields, data) => {
37
37
  rest.gender = data.gender || null;
38
38
  return rest;
39
39
  };
40
- const useProfileForm = (profile, loading, error, update, callback
41
- // optIns: Partial<ConfigBrand> = {}
42
- ) => {
40
+ const useProfileForm = (profile, loading, error, update, callback, optionalFields) => {
43
41
  const submitRef = (0, react_1.useRef)(null);
44
42
  const [data, setData] = (0, react_1.useState)(profile);
45
43
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
46
44
  const [errors, setErrors] = (0, react_1.useState)({});
47
- // const { accepts_marketing, order_notifications } = optIns
48
- // useEffect(() => {
49
- // setPayload(fields, data)
50
- // }, [fields, data])
45
+ const toRemove = [];
46
+ if (optionalFields && !optionalFields.includes('birth_date'))
47
+ toRemove.push('birth_date');
48
+ if (optionalFields && !optionalFields.includes('gender'))
49
+ toRemove.push('gender');
50
+ const fields = allFields.filter(i => !toRemove.includes(i.name));
51
+ // const fields = useMemo(
52
+ // () => allFields.filter(i => !toRemove.includes(i.name)),
53
+ // [toRemove]
54
+ // )
51
55
  (0, react_1.useEffect)(() => {
52
56
  if (loading === 'idle') {
53
57
  setSubmitting(false);
@@ -60,7 +64,7 @@ const useProfileForm = (profile, loading, error, update, callback
60
64
  callback(payload);
61
65
  }
62
66
  }
63
- }, [loading, error, callback, submitting, data]);
67
+ }, [loading, error, callback, submitting, fields, data]);
64
68
  (0, react_1.useEffect)(() => {
65
69
  if (profile)
66
70
  setData(profile);
@@ -84,16 +88,6 @@ const useProfileForm = (profile, loading, error, update, callback
84
88
  return;
85
89
  setErrors({});
86
90
  setSubmitting(true);
87
- // remove read-only properties
88
- // const rest: CustomerCreate = fields.reduce(
89
- // (obj, i: FormFieldType) => ({
90
- // ...obj,
91
- // [i.name]: data[i.name as keyof Customer]
92
- // }),
93
- // {} as CustomerCreate
94
- // )
95
- // rest.birth_date = data.birth_date ? slashesToDashes(data.birth_date) : null
96
- // rest.gender = data.gender || null
97
91
  const payload = makePayload(fields, data);
98
92
  if (payload)
99
93
  update(payload);
@@ -1,11 +1,20 @@
1
1
  /// <reference types="react" />
2
- import { CheckoutConfig, Customer, CustomerCreate, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useSignUpForm: (loading: RequestStatus, error: RequestError, signUp: (data: CustomerCreate, callback?: ((data: Customer) => void) | undefined) => void, callback?: ((data: Customer) => void) | undefined, checkConfig?: CheckoutConfig, hasThanx?: boolean) => {
2
+ import { CheckoutConfig, Customer, CustomerCreate, NotificationChannel, RequestError, RequestStatus } from '@open-tender/types';
3
+ interface NotificationCheckbox {
4
+ label: string;
5
+ name: NotificationChannel;
6
+ type: string;
7
+ }
8
+ export declare const useSignUpForm: (loading: RequestStatus, error: RequestError, signUp: (data: CustomerCreate, callback?: ((data: Customer) => void) | undefined) => void, callback?: ((data: Customer) => void) | undefined, checkConfig?: CheckoutConfig, hidePassword?: boolean, optionalFields?: string[], notificationChannels?: NotificationChannel[]) => {
4
9
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
10
  data: CustomerCreate;
11
+ comms: NotificationChannel[];
6
12
  errors: Record<string, string>;
7
13
  submitting: boolean;
8
14
  fields: import("@open-tender/types").FormFieldType[];
15
+ notificationFields: NotificationCheckbox[];
9
16
  handleChange: (name: string, value: string | number | boolean) => void;
17
+ handleComms: (name: NotificationChannel, value: boolean) => void;
10
18
  handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
11
19
  };
20
+ export {};
@@ -10,9 +10,27 @@ const initState = {
10
10
  email: '',
11
11
  phone: ''
12
12
  };
13
- const useSignUpForm = (loading, error, signUp, callback, checkConfig, hasThanx = false) => {
13
+ const notificationChannelFields = [
14
+ {
15
+ label: 'Email',
16
+ name: 'EMAIL',
17
+ type: 'checkbox'
18
+ },
19
+ {
20
+ label: 'SMS / Text',
21
+ name: 'SMS',
22
+ type: 'checkbox'
23
+ },
24
+ {
25
+ label: 'Push Notifications',
26
+ name: 'PUSH',
27
+ type: 'checkbox'
28
+ }
29
+ ];
30
+ const useSignUpForm = (loading, error, signUp, callback, checkConfig, hidePassword = false, optionalFields, notificationChannels) => {
14
31
  const submitRef = (0, react_1.useRef)(null);
15
32
  const [data, setData] = (0, react_1.useState)(initState);
33
+ const [comms, setComms] = (0, react_1.useState)(notificationChannels || []);
16
34
  const [errors, setErrors] = (0, react_1.useState)({});
17
35
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
18
36
  const { displayed, required } = checkConfig || {};
@@ -22,8 +40,12 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig, hasThanx =
22
40
  const toRemove = [];
23
41
  if (!showCompany)
24
42
  toRemove.push('company');
25
- if (hasThanx)
43
+ if (hidePassword)
26
44
  toRemove.push('password');
45
+ if (optionalFields && !optionalFields.includes('birth_date'))
46
+ toRemove.push('birth_date');
47
+ if (optionalFields && !optionalFields.includes('gender'))
48
+ toRemove.push('gender');
27
49
  const allFields = [
28
50
  {
29
51
  label: 'First Name',
@@ -68,6 +90,7 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig, hasThanx =
68
90
  }
69
91
  ];
70
92
  const fields = allFields.filter(i => !toRemove.includes(i.name));
93
+ const notificationFields = notificationChannelFields.filter(i => notificationChannels === null || notificationChannels === void 0 ? void 0 : notificationChannels.includes(i.name));
71
94
  (0, react_1.useEffect)(() => {
72
95
  return () => {
73
96
  setData(initState);
@@ -90,12 +113,17 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig, hasThanx =
90
113
  : value;
91
114
  setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
92
115
  };
116
+ const handleComms = (name, value) => {
117
+ const other = comms.filter(i => i !== name);
118
+ const updated = value ? [...other, name] : comms;
119
+ setComms(updated);
120
+ };
93
121
  const handleSubmit = (evt) => {
94
122
  var _a, _b;
95
123
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
96
124
  setErrors({});
97
125
  setSubmitting(true);
98
- const values = Object.assign({}, data);
126
+ const values = Object.assign(Object.assign({}, data), { communications: comms });
99
127
  if (values.birth_date) {
100
128
  values.birth_date = (0, utils_1.slashesToDashes)(values.birth_date);
101
129
  }
@@ -105,10 +133,13 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig, hasThanx =
105
133
  return {
106
134
  submitRef,
107
135
  data,
136
+ comms,
108
137
  errors,
109
138
  submitting,
110
139
  fields,
140
+ notificationFields,
111
141
  handleChange,
142
+ handleComms,
112
143
  handleSubmit
113
144
  };
114
145
  };
@@ -1,15 +1,15 @@
1
1
  /// <reference types="react" />
2
- import { Customer, CustomerCreate, FormFields, Gender, RequestError, RequestStatus } from '@open-tender/types';
2
+ import { Customer, CustomerCreate, FormFieldType, Gender, RequestError, RequestStatus } from '@open-tender/types';
3
3
  export declare const genderOptions: {
4
4
  name: string;
5
5
  value: Gender;
6
6
  }[];
7
- export declare const useProfileForm: (profile: Customer | null, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void, callback?: ((data: CustomerCreate) => void) | undefined) => {
7
+ export declare const useProfileForm: (profile: Customer | null, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void, callback?: ((data: CustomerCreate) => void) | undefined, optionalFields?: string[]) => {
8
8
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
9
9
  data: Customer | null;
10
10
  errors: Record<string, string>;
11
11
  submitting: boolean;
12
- fields: FormFields;
12
+ fields: FormFieldType[];
13
13
  handleChange: (name: string, value: string | number | boolean) => void;
14
14
  handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
15
15
  };
@@ -5,7 +5,7 @@ export const genderOptions = [
5
5
  { name: 'Male', value: 'MALE' },
6
6
  { name: 'Female', value: 'FEMALE' }
7
7
  ];
8
- const fields = [
8
+ const allFields = [
9
9
  { label: 'First Name', name: 'first_name', type: 'text', required: true },
10
10
  { label: 'Last Name', name: 'last_name', type: 'text', required: true },
11
11
  {
@@ -34,17 +34,21 @@ const makePayload = (fields, data) => {
34
34
  rest.gender = data.gender || null;
35
35
  return rest;
36
36
  };
37
- export const useProfileForm = (profile, loading, error, update, callback
38
- // optIns: Partial<ConfigBrand> = {}
39
- ) => {
37
+ export const useProfileForm = (profile, loading, error, update, callback, optionalFields) => {
40
38
  const submitRef = useRef(null);
41
39
  const [data, setData] = useState(profile);
42
40
  const [submitting, setSubmitting] = useState(false);
43
41
  const [errors, setErrors] = useState({});
44
- // const { accepts_marketing, order_notifications } = optIns
45
- // useEffect(() => {
46
- // setPayload(fields, data)
47
- // }, [fields, data])
42
+ const toRemove = [];
43
+ if (optionalFields && !optionalFields.includes('birth_date'))
44
+ toRemove.push('birth_date');
45
+ if (optionalFields && !optionalFields.includes('gender'))
46
+ toRemove.push('gender');
47
+ const fields = allFields.filter(i => !toRemove.includes(i.name));
48
+ // const fields = useMemo(
49
+ // () => allFields.filter(i => !toRemove.includes(i.name)),
50
+ // [toRemove]
51
+ // )
48
52
  useEffect(() => {
49
53
  if (loading === 'idle') {
50
54
  setSubmitting(false);
@@ -57,7 +61,7 @@ export const useProfileForm = (profile, loading, error, update, callback
57
61
  callback(payload);
58
62
  }
59
63
  }
60
- }, [loading, error, callback, submitting, data]);
64
+ }, [loading, error, callback, submitting, fields, data]);
61
65
  useEffect(() => {
62
66
  if (profile)
63
67
  setData(profile);
@@ -81,16 +85,6 @@ export const useProfileForm = (profile, loading, error, update, callback
81
85
  return;
82
86
  setErrors({});
83
87
  setSubmitting(true);
84
- // remove read-only properties
85
- // const rest: CustomerCreate = fields.reduce(
86
- // (obj, i: FormFieldType) => ({
87
- // ...obj,
88
- // [i.name]: data[i.name as keyof Customer]
89
- // }),
90
- // {} as CustomerCreate
91
- // )
92
- // rest.birth_date = data.birth_date ? slashesToDashes(data.birth_date) : null
93
- // rest.gender = data.gender || null
94
88
  const payload = makePayload(fields, data);
95
89
  if (payload)
96
90
  update(payload);
@@ -1,11 +1,20 @@
1
1
  /// <reference types="react" />
2
- import { CheckoutConfig, Customer, CustomerCreate, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useSignUpForm: (loading: RequestStatus, error: RequestError, signUp: (data: CustomerCreate, callback?: ((data: Customer) => void) | undefined) => void, callback?: ((data: Customer) => void) | undefined, checkConfig?: CheckoutConfig, hasThanx?: boolean) => {
2
+ import { CheckoutConfig, Customer, CustomerCreate, NotificationChannel, RequestError, RequestStatus } from '@open-tender/types';
3
+ interface NotificationCheckbox {
4
+ label: string;
5
+ name: NotificationChannel;
6
+ type: string;
7
+ }
8
+ export declare const useSignUpForm: (loading: RequestStatus, error: RequestError, signUp: (data: CustomerCreate, callback?: ((data: Customer) => void) | undefined) => void, callback?: ((data: Customer) => void) | undefined, checkConfig?: CheckoutConfig, hidePassword?: boolean, optionalFields?: string[], notificationChannels?: NotificationChannel[]) => {
4
9
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
10
  data: CustomerCreate;
11
+ comms: NotificationChannel[];
6
12
  errors: Record<string, string>;
7
13
  submitting: boolean;
8
14
  fields: import("@open-tender/types").FormFieldType[];
15
+ notificationFields: NotificationCheckbox[];
9
16
  handleChange: (name: string, value: string | number | boolean) => void;
17
+ handleComms: (name: NotificationChannel, value: boolean) => void;
10
18
  handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
11
19
  };
20
+ export {};
@@ -7,9 +7,27 @@ const initState = {
7
7
  email: '',
8
8
  phone: ''
9
9
  };
10
- export const useSignUpForm = (loading, error, signUp, callback, checkConfig, hasThanx = false) => {
10
+ const notificationChannelFields = [
11
+ {
12
+ label: 'Email',
13
+ name: 'EMAIL',
14
+ type: 'checkbox'
15
+ },
16
+ {
17
+ label: 'SMS / Text',
18
+ name: 'SMS',
19
+ type: 'checkbox'
20
+ },
21
+ {
22
+ label: 'Push Notifications',
23
+ name: 'PUSH',
24
+ type: 'checkbox'
25
+ }
26
+ ];
27
+ export const useSignUpForm = (loading, error, signUp, callback, checkConfig, hidePassword = false, optionalFields, notificationChannels) => {
11
28
  const submitRef = useRef(null);
12
29
  const [data, setData] = useState(initState);
30
+ const [comms, setComms] = useState(notificationChannels || []);
13
31
  const [errors, setErrors] = useState({});
14
32
  const [submitting, setSubmitting] = useState(false);
15
33
  const { displayed, required } = checkConfig || {};
@@ -19,8 +37,12 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig, has
19
37
  const toRemove = [];
20
38
  if (!showCompany)
21
39
  toRemove.push('company');
22
- if (hasThanx)
40
+ if (hidePassword)
23
41
  toRemove.push('password');
42
+ if (optionalFields && !optionalFields.includes('birth_date'))
43
+ toRemove.push('birth_date');
44
+ if (optionalFields && !optionalFields.includes('gender'))
45
+ toRemove.push('gender');
24
46
  const allFields = [
25
47
  {
26
48
  label: 'First Name',
@@ -65,6 +87,7 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig, has
65
87
  }
66
88
  ];
67
89
  const fields = allFields.filter(i => !toRemove.includes(i.name));
90
+ const notificationFields = notificationChannelFields.filter(i => notificationChannels === null || notificationChannels === void 0 ? void 0 : notificationChannels.includes(i.name));
68
91
  useEffect(() => {
69
92
  return () => {
70
93
  setData(initState);
@@ -87,12 +110,17 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig, has
87
110
  : value;
88
111
  setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
89
112
  };
113
+ const handleComms = (name, value) => {
114
+ const other = comms.filter(i => i !== name);
115
+ const updated = value ? [...other, name] : comms;
116
+ setComms(updated);
117
+ };
90
118
  const handleSubmit = (evt) => {
91
119
  var _a, _b;
92
120
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
93
121
  setErrors({});
94
122
  setSubmitting(true);
95
- const values = Object.assign({}, data);
123
+ const values = Object.assign(Object.assign({}, data), { communications: comms });
96
124
  if (values.birth_date) {
97
125
  values.birth_date = slashesToDashes(values.birth_date);
98
126
  }
@@ -102,10 +130,13 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig, has
102
130
  return {
103
131
  submitRef,
104
132
  data,
133
+ comms,
105
134
  errors,
106
135
  submitting,
107
136
  fields,
137
+ notificationFields,
108
138
  handleChange,
139
+ handleComms,
109
140
  handleSubmit
110
141
  };
111
142
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.1.132",
3
+ "version": "0.1.134",
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.62",
57
+ "@open-tender/types": "^0.2.64",
58
58
  "@reduxjs/toolkit": "^1.8.5",
59
59
  "react": "^18.2.0"
60
60
  },