@open-tender/utils 0.5.5 → 0.5.6

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.
@@ -5,6 +5,7 @@ export * from './useAllergenForm';
5
5
  export * from './useBuilder';
6
6
  export * from './useCartGuestForm';
7
7
  export * from './useCartPoints';
8
+ export * from './useClaimRewardCodeForm';
8
9
  export * from './useLoginForm';
9
10
  export * from './useCreditCard';
10
11
  export * from './useCreditCardForm';
@@ -8,6 +8,7 @@ tslib_1.__exportStar(require("./useAllergenForm"), exports);
8
8
  tslib_1.__exportStar(require("./useBuilder"), exports);
9
9
  tslib_1.__exportStar(require("./useCartGuestForm"), exports);
10
10
  tslib_1.__exportStar(require("./useCartPoints"), exports);
11
+ tslib_1.__exportStar(require("./useClaimRewardCodeForm"), exports);
11
12
  tslib_1.__exportStar(require("./useLoginForm"), exports);
12
13
  tslib_1.__exportStar(require("./useCreditCard"), exports);
13
14
  tslib_1.__exportStar(require("./useCreditCardForm"), exports);
@@ -0,0 +1,14 @@
1
+ /// <reference types="react" />
2
+ import { FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useClaimRewardCoderForm: (loading: RequestStatus, error: RequestError, claim: (reward_code: string, callback: () => void) => void, callback: () => void) => {
4
+ submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
+ inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
+ fields: FormFields;
7
+ data: {
8
+ reward_code: string;
9
+ };
10
+ errors: Record<string, string>;
11
+ submitting: boolean;
12
+ handleChange: (name: string, value: string | number | boolean) => void;
13
+ handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
14
+ };
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useClaimRewardCoderForm = void 0;
4
+ const react_1 = require("react");
5
+ const utils_1 = require("../utils");
6
+ const useClaimRewardCoderForm = (loading, error, claim, callback) => {
7
+ const submitRef = (0, react_1.useRef)(null);
8
+ const inputRef = (0, react_1.useRef)(null);
9
+ const [data, setData] = (0, react_1.useState)({ reward_code: '' });
10
+ const [errors, setErrors] = (0, react_1.useState)({});
11
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
12
+ const fields = [
13
+ {
14
+ label: 'Reward Code',
15
+ name: 'reward_code',
16
+ type: 'text'
17
+ }
18
+ ];
19
+ (0, react_1.useEffect)(() => {
20
+ var _a, _b;
21
+ if (loading === 'idle') {
22
+ setSubmitting(false);
23
+ if (error) {
24
+ setErrors((0, utils_1.makeFormErrors)(error));
25
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
26
+ }
27
+ }
28
+ }, [loading, error]);
29
+ const handleChange = (name, value) => {
30
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
31
+ };
32
+ const handleSubmit = (evt) => {
33
+ var _a, _b;
34
+ evt === null || evt === void 0 ? void 0 : evt.preventDefault();
35
+ setSubmitting(true);
36
+ claim(data.reward_code, callback);
37
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
38
+ };
39
+ return {
40
+ submitRef,
41
+ inputRef,
42
+ fields,
43
+ data,
44
+ errors,
45
+ submitting,
46
+ handleChange,
47
+ handleSubmit
48
+ };
49
+ };
50
+ exports.useClaimRewardCoderForm = useClaimRewardCoderForm;
@@ -5,6 +5,7 @@ export * from './useAllergenForm';
5
5
  export * from './useBuilder';
6
6
  export * from './useCartGuestForm';
7
7
  export * from './useCartPoints';
8
+ export * from './useClaimRewardCodeForm';
8
9
  export * from './useLoginForm';
9
10
  export * from './useCreditCard';
10
11
  export * from './useCreditCardForm';
@@ -5,6 +5,7 @@ export * from './useAllergenForm';
5
5
  export * from './useBuilder';
6
6
  export * from './useCartGuestForm';
7
7
  export * from './useCartPoints';
8
+ export * from './useClaimRewardCodeForm';
8
9
  export * from './useLoginForm';
9
10
  export * from './useCreditCard';
10
11
  export * from './useCreditCardForm';
@@ -0,0 +1,14 @@
1
+ /// <reference types="react" />
2
+ import { FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useClaimRewardCoderForm: (loading: RequestStatus, error: RequestError, claim: (reward_code: string, callback: () => void) => void, callback: () => void) => {
4
+ submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
+ inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
+ fields: FormFields;
7
+ data: {
8
+ reward_code: string;
9
+ };
10
+ errors: Record<string, string>;
11
+ submitting: boolean;
12
+ handleChange: (name: string, value: string | number | boolean) => void;
13
+ handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
14
+ };
@@ -0,0 +1,46 @@
1
+ import { useState, useEffect, useRef } from 'react';
2
+ import { makeFormErrors } from '../utils';
3
+ export const useClaimRewardCoderForm = (loading, error, claim, callback) => {
4
+ const submitRef = useRef(null);
5
+ const inputRef = useRef(null);
6
+ const [data, setData] = useState({ reward_code: '' });
7
+ const [errors, setErrors] = useState({});
8
+ const [submitting, setSubmitting] = useState(false);
9
+ const fields = [
10
+ {
11
+ label: 'Reward Code',
12
+ name: 'reward_code',
13
+ type: 'text'
14
+ }
15
+ ];
16
+ useEffect(() => {
17
+ var _a, _b;
18
+ if (loading === 'idle') {
19
+ setSubmitting(false);
20
+ if (error) {
21
+ setErrors(makeFormErrors(error));
22
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
23
+ }
24
+ }
25
+ }, [loading, error]);
26
+ const handleChange = (name, value) => {
27
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
28
+ };
29
+ const handleSubmit = (evt) => {
30
+ var _a, _b;
31
+ evt === null || evt === void 0 ? void 0 : evt.preventDefault();
32
+ setSubmitting(true);
33
+ claim(data.reward_code, callback);
34
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
35
+ };
36
+ return {
37
+ submitRef,
38
+ inputRef,
39
+ fields,
40
+ data,
41
+ errors,
42
+ submitting,
43
+ handleChange,
44
+ handleSubmit
45
+ };
46
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.5.5",
3
+ "version": "0.5.6",
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",
@@ -67,4 +67,4 @@
67
67
  "date-fns-tz": "^1.3.7"
68
68
  },
69
69
  "packageManager": "yarn@1.22.22"
70
- }
70
+ }