11import { useState , useEffect } from 'react' ;
22import constate from 'constate' ;
33import { Category , loadCategoryTree , Product } from './service' ;
4+ import * as service from './service' ;
45import { config } from './config' ;
56
67import en from './locales/en.json' ;
@@ -11,7 +12,7 @@ const translations: { [lang: string]: { [name: string]: string } } = {
1112 fr,
1213} ;
1314
14- const fallbackLanguage = 'en' ;
15+ const defaultLanguage = 'en' ;
1516
1617function getInitialLanguage ( ) : string {
1718 const savedLanguage = localStorage . getItem ( 'selectedLanguage' ) ;
@@ -44,7 +45,7 @@ function getInitialLanguage(): string {
4445 }
4546 }
4647
47- return fallbackLanguage ;
48+ return defaultLanguage ;
4849}
4950
5051function checkTranslations ( ) {
@@ -101,6 +102,48 @@ function useTranslationState() {
101102 } ;
102103}
103104
105+ const defaultCurrency = 'USD' ;
106+
107+ function useCurrencyState ( ) {
108+ const [ allCurrencies , setAllCurrencies ] = useState < service . Currency [ ] > ( [ ] ) ;
109+ // Set previously saved or defautlt currency before fetching the list of supported ones
110+ const [ selectedCurrency , setSelectedCurrency ] = useState ( localStorage . getItem ( 'selectedCurrency' ) ?? defaultCurrency ) ;
111+
112+ const setCurrency = ( newCurrency : string ) => {
113+ localStorage . setItem ( 'selectedCurrency' , newCurrency ) ;
114+ setSelectedCurrency ( newCurrency ) ;
115+ } ;
116+
117+ useEffect ( ( ) => {
118+ // Only fetch currencies once
119+ if ( allCurrencies . length > 0 ) {
120+ return ;
121+ }
122+
123+ service . loadEnabledCurrencies ( ) . then ( currencies => {
124+ // Check if we need to update selectedCurrency
125+ const selected = currencies . find ( c => c . code === selectedCurrency ) ;
126+
127+ if ( ! selected ) {
128+ // Saved or default currency we initially selected was not found in the list of server currencies
129+ // Switch selectedCurrency to server default one if exist or first one in the list
130+ setSelectedCurrency ( currencies . find ( c => c . default ) ?. code ?? currencies [ 0 ] . code ) ;
131+
132+ // Clear selection in local storage
133+ localStorage . removeItem ( 'selectedCurrency' ) ;
134+ }
135+
136+ setAllCurrencies ( currencies ) ;
137+ } ) ;
138+ } , [ allCurrencies . length , selectedCurrency ] ) ;
139+
140+ return {
141+ allCurrencies,
142+ selectedCurrency,
143+ setCurrency,
144+ }
145+ }
146+
104147function getCategoryPaths ( categories : Category [ ] ) : { [ categoryId : string ] : Category [ ] } {
105148 const lastCat = categories [ categories . length - 1 ] ;
106149
@@ -177,9 +220,11 @@ function useCompareProductsState() {
177220
178221function useGlobalState ( ) {
179222 const translation = useTranslationState ( ) ;
223+ const currency = useCurrencyState ( ) ;
180224
181225 return {
182226 translation,
227+ currency,
183228 categories : useCategoriesState ( translation . selectedLanguage ) ,
184229 compareProducts : useCompareProductsState ( ) ,
185230 } ;
@@ -188,11 +233,13 @@ function useGlobalState() {
188233export const [
189234 AppStateProvider ,
190235 useTranslation ,
236+ useCurrency ,
191237 useCategories ,
192238 useCompareProducts ,
193239] = constate (
194240 useGlobalState ,
195241 value => value . translation ,
242+ value => value . currency ,
196243 value => value . categories ,
197244 value => value . compareProducts
198245) ;
0 commit comments