@@ -14,8 +14,10 @@ import { logoTransformer } from '~/data-transformers/logo-transformer';
14
14
import { routing } from '~/i18n/routing' ;
15
15
16
16
import { search } from './_actions/search' ;
17
+ import { switchCurrency } from './_actions/switch-currency' ;
17
18
import { switchLocale } from './_actions/switch-locale' ;
18
19
import { HeaderFragment } from './fragment' ;
20
+ import { getPreferredCurrencyCode } from '~/lib/currency' ;
19
21
20
22
const GetCartCountQuery = graphql ( `
21
23
query GetCartCountQuery($cartId: String) {
@@ -35,6 +37,7 @@ const getLayoutData = cache(async () => {
35
37
36
38
const { data : response } = await client . fetch ( {
37
39
document : LayoutQuery ,
40
+ customerAccessToken,
38
41
fetchOptions : customerAccessToken ? { cache : 'no-store' } : { next : { revalidate } } ,
39
42
} ) ;
40
43
@@ -80,12 +83,7 @@ const getCartCount = async () => {
80
83
document : GetCartCountQuery ,
81
84
variables : { cartId } ,
82
85
customerAccessToken,
83
- fetchOptions : {
84
- cache : 'no-store' ,
85
- next : {
86
- tags : [ TAGS . cart ] ,
87
- } ,
88
- } ,
86
+ fetchOptions : { cache : 'no-store' } ,
89
87
} ) ;
90
88
91
89
if ( ! response . data . site . cart ) {
@@ -95,15 +93,35 @@ const getCartCount = async () => {
95
93
return response . data . site . cart . lineItems . totalQuantity ;
96
94
} ;
97
95
96
+ const getCurrencies = async ( ) => {
97
+ const data = await getLayoutData ( ) ;
98
+
99
+ if ( ! data . currencies ?. edges ) {
100
+ return [ ] ;
101
+ }
102
+
103
+ const currencies = data . currencies . edges
104
+ . map ( ( { node } ) => ( {
105
+ code : node . code ,
106
+ } ) )
107
+
108
+ return currencies ;
109
+ } ;
110
+
98
111
export const Header = async ( ) => {
99
112
const t = await getTranslations ( 'Components.Header' ) ;
100
113
const locale = await getLocale ( ) ;
114
+ const currencyCode = await getPreferredCurrencyCode ( ) ;
101
115
102
116
const locales = routing . locales . map ( ( enabledLocales ) => ( {
103
117
id : enabledLocales ,
104
118
label : enabledLocales . toLocaleUpperCase ( ) ,
105
119
} ) ) ;
106
120
121
+ const currencies = await getCurrencies ( ) ;
122
+ // todo handle default currency properly once added to API
123
+ const activeCurrencyCode = currencyCode ?? currencies [ 0 ] ?. code ;
124
+
107
125
return (
108
126
< HeaderSection
109
127
navigation = { {
@@ -124,6 +142,9 @@ export const Header = async () => {
124
142
activeLocaleId : locale ,
125
143
locales,
126
144
localeAction : switchLocale ,
145
+ currencies,
146
+ activeCurrencyCode,
147
+ currencyAction : switchCurrency ,
127
148
} }
128
149
/>
129
150
) ;
0 commit comments