@open-tender/utils 0.1.131 → 0.1.133

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,26 @@ 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
126
  const values = Object.assign({}, data);
127
+ // const communications = []
128
+ // const values = Object.entries(data).reduce((obj, [key, value]) => {
129
+ // if (key.startsWith('comms')) {
130
+ // const [, notificationArea] = key.split('-')
131
+ // communications.push(notificationArea.toUpperCase())
132
+ // return obj
133
+ // }
134
+ // return { ...obj, [key]: value }
135
+ // }, {} as CustomerCreate)
99
136
  if (values.birth_date) {
100
137
  values.birth_date = (0, utils_1.slashesToDashes)(values.birth_date);
101
138
  }
@@ -105,10 +142,13 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig, hasThanx =
105
142
  return {
106
143
  submitRef,
107
144
  data,
145
+ comms,
108
146
  errors,
109
147
  submitting,
110
148
  fields,
149
+ notificationFields,
111
150
  handleChange,
151
+ handleComms,
112
152
  handleSubmit
113
153
  };
114
154
  };
@@ -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,26 @@ 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
123
  const values = Object.assign({}, data);
124
+ // const communications = []
125
+ // const values = Object.entries(data).reduce((obj, [key, value]) => {
126
+ // if (key.startsWith('comms')) {
127
+ // const [, notificationArea] = key.split('-')
128
+ // communications.push(notificationArea.toUpperCase())
129
+ // return obj
130
+ // }
131
+ // return { ...obj, [key]: value }
132
+ // }, {} as CustomerCreate)
96
133
  if (values.birth_date) {
97
134
  values.birth_date = slashesToDashes(values.birth_date);
98
135
  }
@@ -102,10 +139,13 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig, has
102
139
  return {
103
140
  submitRef,
104
141
  data,
142
+ comms,
105
143
  errors,
106
144
  submitting,
107
145
  fields,
146
+ notificationFields,
108
147
  handleChange,
148
+ handleComms,
109
149
  handleSubmit
110
150
  };
111
151
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.1.131",
3
+ "version": "0.1.133",
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",