@open-tender/utils 0.1.27 → 0.1.29

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,11 @@
1
1
  /// <reference types="react" />
2
2
  import { Customer, CustomerCreate, FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useProfileForm: (profile: Customer, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void) => {
3
+ export declare const useProfileForm: (profile: Customer | null, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
- data: CustomerCreate;
5
+ data: Customer | null;
6
6
  errors: Record<string, string>;
7
7
  submitting: boolean;
8
8
  fields: FormFields;
9
9
  handleChange: (name: string, value: string | number | boolean) => void;
10
- handleRadio: (name: string, value: string) => void;
11
10
  handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
12
11
  };
@@ -35,12 +35,16 @@ const useProfileForm = (profile, loading, error, update
35
35
  if (loading === 'idle') {
36
36
  setSubmitting(false);
37
37
  if (error) {
38
- setErrors((0, utils_1.makeFormErrors)(error));
38
+ const errs = (0, utils_1.makeFormErrors)(error);
39
+ if (errs)
40
+ errs.form = 'There are one or more errors below.';
41
+ setErrors(errs);
39
42
  }
40
43
  }
41
44
  }, [loading, error]);
42
45
  (0, react_1.useEffect)(() => {
43
- setData(profile);
46
+ if (profile)
47
+ setData(profile);
44
48
  }, [profile]);
45
49
  const handleChange = (name, value) => {
46
50
  const inputValue = name === 'phone'
@@ -48,28 +52,25 @@ const useProfileForm = (profile, loading, error, update
48
52
  : name === 'birth_date'
49
53
  ? (0, utils_1.makeBirthDate)(value)
50
54
  : value;
51
- setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
52
- };
53
- const handleRadio = (name, value) => {
54
- setData(Object.assign(Object.assign({}, data), { [name]: value }));
55
+ if (data)
56
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
55
57
  };
58
+ // const handleRadio = (name: string, value: string) => {
59
+ // if (data) setData({ ...data, [name]: value })
60
+ // }
56
61
  const handleSubmit = (evt) => {
57
62
  var _a, _b;
58
63
  evt.preventDefault();
64
+ if (!data)
65
+ return;
59
66
  setSubmitting(true);
60
- // const updatedData: any = fields.reduce(
61
- // (obj: Partial<ProfileFormField>, i: Partial<ProfileFormField>) => ({
62
- // ...obj,
63
- // [i.name!]: data[i.name!]
64
- // }),
65
- // {}
66
- // )
67
- const updatedData = Object.assign({}, data);
68
- updatedData.birth_date = data.birth_date
69
- ? (0, utils_1.slashesToDashes)(data.birth_date)
70
- : null;
71
- updatedData.gender = data.gender || null;
72
- update(updatedData);
67
+ // remove read-only properties
68
+ const rest = fields.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: data[i.name] })), {});
69
+ // const rest = { ...data }
70
+ // const { customer_id, is_notification_set, is_verified, ...rest } = data
71
+ rest.birth_date = data.birth_date ? (0, utils_1.slashesToDashes)(data.birth_date) : null;
72
+ rest.gender = data.gender || null;
73
+ update(rest);
73
74
  ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
74
75
  };
75
76
  return {
@@ -81,7 +82,7 @@ const useProfileForm = (profile, loading, error, update
81
82
  submitting,
82
83
  fields,
83
84
  handleChange,
84
- handleRadio,
85
+ // handleRadio,
85
86
  handleSubmit
86
87
  };
87
88
  };
@@ -1,12 +1,11 @@
1
1
  /// <reference types="react" />
2
2
  import { Customer, CustomerCreate, FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useProfileForm: (profile: Customer, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void) => {
3
+ export declare const useProfileForm: (profile: Customer | null, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
- data: CustomerCreate;
5
+ data: Customer | null;
6
6
  errors: Record<string, string>;
7
7
  submitting: boolean;
8
8
  fields: FormFields;
9
9
  handleChange: (name: string, value: string | number | boolean) => void;
10
- handleRadio: (name: string, value: string) => void;
11
10
  handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
12
11
  };
@@ -32,12 +32,16 @@ export const useProfileForm = (profile, loading, error, update
32
32
  if (loading === 'idle') {
33
33
  setSubmitting(false);
34
34
  if (error) {
35
- setErrors(makeFormErrors(error));
35
+ const errs = makeFormErrors(error);
36
+ if (errs)
37
+ errs.form = 'There are one or more errors below.';
38
+ setErrors(errs);
36
39
  }
37
40
  }
38
41
  }, [loading, error]);
39
42
  useEffect(() => {
40
- setData(profile);
43
+ if (profile)
44
+ setData(profile);
41
45
  }, [profile]);
42
46
  const handleChange = (name, value) => {
43
47
  const inputValue = name === 'phone'
@@ -45,28 +49,25 @@ export const useProfileForm = (profile, loading, error, update
45
49
  : name === 'birth_date'
46
50
  ? makeBirthDate(value)
47
51
  : value;
48
- setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
49
- };
50
- const handleRadio = (name, value) => {
51
- setData(Object.assign(Object.assign({}, data), { [name]: value }));
52
+ if (data)
53
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
52
54
  };
55
+ // const handleRadio = (name: string, value: string) => {
56
+ // if (data) setData({ ...data, [name]: value })
57
+ // }
53
58
  const handleSubmit = (evt) => {
54
59
  var _a, _b;
55
60
  evt.preventDefault();
61
+ if (!data)
62
+ return;
56
63
  setSubmitting(true);
57
- // const updatedData: any = fields.reduce(
58
- // (obj: Partial<ProfileFormField>, i: Partial<ProfileFormField>) => ({
59
- // ...obj,
60
- // [i.name!]: data[i.name!]
61
- // }),
62
- // {}
63
- // )
64
- const updatedData = Object.assign({}, data);
65
- updatedData.birth_date = data.birth_date
66
- ? slashesToDashes(data.birth_date)
67
- : null;
68
- updatedData.gender = data.gender || null;
69
- update(updatedData);
64
+ // remove read-only properties
65
+ const rest = fields.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: data[i.name] })), {});
66
+ // const rest = { ...data }
67
+ // const { customer_id, is_notification_set, is_verified, ...rest } = data
68
+ rest.birth_date = data.birth_date ? slashesToDashes(data.birth_date) : null;
69
+ rest.gender = data.gender || null;
70
+ update(rest);
70
71
  ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
71
72
  };
72
73
  return {
@@ -78,7 +79,7 @@ export const useProfileForm = (profile, loading, error, update
78
79
  submitting,
79
80
  fields,
80
81
  handleChange,
81
- handleRadio,
82
+ // handleRadio,
82
83
  handleSubmit
83
84
  };
84
85
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.1.27",
3
+ "version": "0.1.29",
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",