Skip to content
This repository was archived by the owner on Oct 9, 2024. It is now read-only.

Commit b7eb66c

Browse files
Merge pull request #19 from chec/feature/api-call-optimisations
Update useAllCountries and useIsFree to make less API calls
2 parents 0c257a7 + 0de0859 commit b7eb66c

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/checkout/CheckoutProvider.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff 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

1214
export const CheckoutContext = createContext<CheckoutContextInterface>({
1315
checkout: undefined,
1416
updateLive: () => {},
1517
createCheckout: () => {},
18+
countries: undefined,
19+
setCountries: () => {},
1620
});
1721

1822
export 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
);

src/checkout/useAllCountries.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
import { useEffect, useState } from 'react';
1+
import { useContext, useEffect } from 'react';
22
import useCommerce from '../useCommerce';
3+
import { CheckoutContext } from "./CheckoutProvider";
34

45
export 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;

src/checkout/useIsFree.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff 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;

0 commit comments

Comments
 (0)