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 {
77 checkout : any ,
88 updateLive : Function ,
99 createCheckout : Function ,
10+ countries : object [ ] | undefined ,
11+ setCountries : Function ,
1012}
1113
1214export const CheckoutContext = createContext < CheckoutContextInterface > ( {
1315 checkout : undefined ,
1416 updateLive : ( ) => { } ,
1517 createCheckout : ( ) => { } ,
18+ countries : undefined ,
19+ setCountries : ( ) => { } ,
1620} ) ;
1721
1822export enum CheckoutIdType {
@@ -34,6 +38,7 @@ export default function CheckoutProvider(
3438) {
3539 const commerce = useCommerce ( ) ;
3640 const [ checkout , setCheckout ] = useState < object | undefined > ( ) ;
41+ const [ countries , setCountries ] = useState < object [ ] > ( ) ;
3742 const createCheckout = async ( ) => setCheckout ( await commerce . checkout . generateTokenFrom ( type , id ) ) ;
3843
3944 useEffect ( ( ) => {
@@ -57,7 +62,13 @@ export default function CheckoutProvider(
5762 }
5863
5964 return (
60- < CheckoutContext . Provider value = { { checkout, updateLive, createCheckout } } >
65+ < CheckoutContext . Provider value = { {
66+ checkout,
67+ updateLive,
68+ createCheckout,
69+ countries,
70+ setCountries,
71+ } } >
6172 { children }
6273 </ CheckoutContext . Provider >
6374 ) ;
Original file line number Diff line number Diff line change 1- import { useEffect , useState } from 'react' ;
1+ import { useContext , useEffect } from 'react' ;
22import useCommerce from '../useCommerce' ;
3+ import { CheckoutContext } from "./CheckoutProvider" ;
34
45export default function useAllCountries ( ) {
5- const [ countries , setCountries ] = useState < object [ ] > ( ) ;
66 const commerce = useCommerce ( ) ;
7+ const { countries, setCountries } = useContext ( CheckoutContext ) ;
78
89 useEffect ( ( ) => {
9- if ( ! commerce ) {
10+ if ( ! commerce || countries !== undefined ) {
1011 return ;
1112 }
1213
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+ } ) ;
1420 } , [ commerce ] ) ;
1521
1622 return countries ;
Original file line number Diff line number Diff line change @@ -8,12 +8,19 @@ export default function useIsFree(): boolean|null {
88 const [ isFree , setIsFree ] = useState < boolean | null > ( null ) ;
99
1010 useEffect ( ( ) => {
11- if ( ! commerce || ! checkout ) {
11+ if ( ! commerce || ! checkout ?. live ?. total ) {
1212 setIsFree ( null ) ;
1313 return ;
1414 }
1515
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 ) ;
1724 } , [ commerce , checkout ] ) ;
1825
1926 return isFree ;
You can’t perform that action at this time.
0 commit comments