@akinon/pz-gpay 2.0.0-beta.13 → 2.0.0-beta.15

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @akinon/pz-gpay
2
2
 
3
+ ## 2.0.0-beta.15
4
+
5
+ ## 2.0.0-beta.14
6
+
7
+ ### Minor Changes
8
+
9
+ - 6ad72e8d: ZERO-4032: Add loading state management for payment submissions across multiple components and add safe guarding
10
+
3
11
  ## 2.0.0-beta.13
4
12
 
5
13
  ## 1.118.0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akinon/pz-gpay",
3
- "version": "2.0.0-beta.13",
3
+ "version": "2.0.0-beta.15",
4
4
  "license": "MIT",
5
5
  "main": "src/index.tsx",
6
6
  "peerDependencies": {
@@ -19,7 +19,7 @@ import {
19
19
  } from '../endpoints';
20
20
  import { twMerge } from 'tailwind-merge';
21
21
  import { Button } from '@akinon/next/components/button';
22
- import { getPosError } from '@akinon/next/utils';
22
+ import { getPosError, checkPaymentWillRedirect } from '@akinon/next/utils';
23
23
 
24
24
  type GPayForm = Record<string, never>;
25
25
 
@@ -61,6 +61,7 @@ const defaultTranslations: GPayTranslations = {
61
61
  export function GPayOption(props: GPayOptionProps) {
62
62
  const [formError, setFormError] = useState<string | null>(null);
63
63
  const [errors, setErrors] = useState<string[]>([]);
64
+ const [isProcessing, setIsProcessing] = useState(false);
64
65
 
65
66
  const translations = { ...defaultTranslations, ...props.translations };
66
67
 
@@ -72,25 +73,46 @@ export function GPayOption(props: GPayOptionProps) {
72
73
  const {
73
74
  handleSubmit,
74
75
  control,
75
- formState: { errors: formErrors }
76
+ formState: { errors: formErrors, isSubmitting }
76
77
  } = useForm<GPayForm>({
77
78
  resolver: yupResolver(gpayFormSchema) as any
78
79
  });
79
80
 
80
81
  const [setPaymentOption] = useSetPaymentOptionMutation();
81
- const [setGPayMethod] = useSetGPayMethodMutation();
82
- const [setCompleteGPay] = useSetCompleteGPayMutation();
82
+ const [setGPayMethod, { isLoading: isSettingMethod }] =
83
+ useSetGPayMethodMutation();
84
+ const [setCompleteGPay, { isLoading: isCompleting }] =
85
+ useSetCompleteGPayMutation();
86
+
87
+ const isButtonDisabled =
88
+ isSubmitting ||
89
+ isPaymentStepBusy ||
90
+ isSettingMethod ||
91
+ isCompleting ||
92
+ isProcessing;
83
93
 
84
94
  const onSubmit: SubmitHandler<GPayForm> = async () => {
85
- if (isPaymentStepBusy) return;
95
+ if (isButtonDisabled) return;
86
96
 
97
+ setIsProcessing(true);
87
98
  setFormError(null);
88
- await setGPayMethod();
89
99
 
90
- const response = await setCompleteGPay().unwrap();
100
+ try {
101
+ await setGPayMethod();
91
102
 
92
- if (response?.errors?.non_field_errors) {
93
- setFormError(response.errors.non_field_errors);
103
+ const response = await setCompleteGPay().unwrap();
104
+
105
+ if (response?.errors?.non_field_errors) {
106
+ setFormError(response.errors.non_field_errors);
107
+ setIsProcessing(false);
108
+ return;
109
+ }
110
+
111
+ if (response?.errors || !checkPaymentWillRedirect(response)) {
112
+ setIsProcessing(false);
113
+ }
114
+ } catch (error) {
115
+ setIsProcessing(false);
94
116
  }
95
117
  };
96
118
 
@@ -137,6 +159,7 @@ export function GPayOption(props: GPayOptionProps) {
137
159
  data-testid="gpay-payment-button"
138
160
  className={twMerge('w-full mt-3', props.buttonClassName)}
139
161
  type="submit"
162
+ disabled={isButtonDisabled}
140
163
  >
141
164
  {translations.button}
142
165
  </Button>