This repository was archived by the owner on Oct 9, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +31
-7
lines changed Expand file tree Collapse file tree 3 files changed +31
-7
lines changed Original file line number Diff line number Diff line change @@ -7,12 +7,16 @@ export interface CheckoutContextInterface {
7
7
checkout : any ,
8
8
updateLive : Function ,
9
9
createCheckout : Function ,
10
+ countries : object [ ] | undefined ,
11
+ setCountries : Function ,
10
12
}
11
13
12
14
export const CheckoutContext = createContext < CheckoutContextInterface > ( {
13
15
checkout : undefined ,
14
16
updateLive : ( ) => { } ,
15
17
createCheckout : ( ) => { } ,
18
+ countries : undefined ,
19
+ setCountries : ( ) => { } ,
16
20
} ) ;
17
21
18
22
export enum CheckoutIdType {
@@ -34,6 +38,7 @@ export default function CheckoutProvider(
34
38
) {
35
39
const commerce = useCommerce ( ) ;
36
40
const [ checkout , setCheckout ] = useState < object | undefined > ( ) ;
41
+ const [ countries , setCountries ] = useState < object [ ] > ( ) ;
37
42
const createCheckout = async ( ) => setCheckout ( await commerce . checkout . generateTokenFrom ( type , id ) ) ;
38
43
39
44
useEffect ( ( ) => {
@@ -57,7 +62,13 @@ export default function CheckoutProvider(
57
62
}
58
63
59
64
return (
60
- < CheckoutContext . Provider value = { { checkout, updateLive, createCheckout } } >
65
+ < CheckoutContext . Provider value = { {
66
+ checkout,
67
+ updateLive,
68
+ createCheckout,
69
+ countries,
70
+ setCountries,
71
+ } } >
61
72
{ children }
62
73
</ CheckoutContext . Provider >
63
74
) ;
Original file line number Diff line number Diff line change 1
- import { useEffect , useState } from 'react' ;
1
+ import { useContext , useEffect } from 'react' ;
2
2
import useCommerce from '../useCommerce' ;
3
+ import { CheckoutContext } from "./CheckoutProvider" ;
3
4
4
5
export default function useAllCountries ( ) {
5
- const [ countries , setCountries ] = useState < object [ ] > ( ) ;
6
6
const commerce = useCommerce ( ) ;
7
+ const { countries, setCountries } = useContext ( CheckoutContext ) ;
7
8
8
9
useEffect ( ( ) => {
9
- if ( ! commerce ) {
10
+ if ( ! commerce || countries !== undefined ) {
10
11
return ;
11
12
}
12
13
13
- commerce . services . localeListCountries ( ) . then ( ( response : any ) => setCountries ( response . countries ) ) ;
14
+ // Set an initial value for countries to prevent more than one instance of this hook queuing the same API call
15
+ setCountries ( [ ] ) ;
16
+
17
+ commerce . services . localeListCountries ( ) . then ( ( response : any ) => {
18
+ setCountries ( response . countries )
19
+ } ) ;
14
20
} , [ commerce ] ) ;
15
21
16
22
return countries ;
Original file line number Diff line number Diff line change @@ -8,12 +8,19 @@ export default function useIsFree(): boolean|null {
8
8
const [ isFree , setIsFree ] = useState < boolean | null > ( null ) ;
9
9
10
10
useEffect ( ( ) => {
11
- if ( ! commerce || ! checkout ) {
11
+ if ( ! commerce || ! checkout ?. live ?. total ) {
12
12
setIsFree ( null ) ;
13
13
return ;
14
14
}
15
15
16
- commerce . checkout . isFree ( checkout . id ) . then ( ( response : any ) => setIsFree ( response . is_free ) ) ;
16
+ const total = checkout . live . total . raw ;
17
+
18
+ if ( typeof total !== 'number' ) {
19
+ setIsFree ( null ) ;
20
+ return ;
21
+ }
22
+
23
+ setIsFree ( total < 0.01 ) ;
17
24
} , [ commerce , checkout ] ) ;
18
25
19
26
return isFree ;
You can’t perform that action at this time.
0 commit comments