-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathindex.tsx
More file actions
186 lines (160 loc) · 6.28 KB
/
Copy pathindex.tsx
File metadata and controls
186 lines (160 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { getLocale, getTranslations } from 'next-intl/server';
import { cache } from 'react';
import { Streamable } from '@/vibes/soul/lib/streamable';
import { GetLinksAndSectionsQuery, LayoutQuery } from '~/app/[locale]/(default)/page-data';
import { getSessionCustomerAccessToken } from '~/auth';
import { client } from '~/client';
import { graphql, readFragment } from '~/client/graphql';
import { revalidate } from '~/client/revalidate-target';
import { TAGS } from '~/client/tags';
import { logoTransformer } from '~/data-transformers/logo-transformer';
import { routing } from '~/i18n/routing';
import { getCartId } from '~/lib/cart';
import { getPreferredCurrencyCode } from '~/lib/currency';
import { SiteHeader as HeaderSection } from '~/lib/makeswift/components/site-header';
import { search } from './_actions/search';
import { switchCurrency } from './_actions/switch-currency';
import { CurrencyCode, HeaderFragment, HeaderLinksFragment } from './fragment';
const GetCartCountQuery = graphql(`
query GetCartCountQuery($cartId: String) {
site {
cart(entityId: $cartId) {
entityId
lineItems {
totalQuantity
}
}
}
}
`);
const getCartCount = cache(async (cartId: string, customerAccessToken?: string) => {
const response = await client.fetch({
document: GetCartCountQuery,
variables: { cartId },
customerAccessToken,
fetchOptions: {
cache: 'no-store',
next: {
tags: [TAGS.cart],
},
},
});
return response.data.site.cart?.lineItems.totalQuantity ?? null;
});
const getHeaderLinks = cache(async (customerAccessToken?: string, currencyCode?: CurrencyCode) => {
const { data: response } = await client.fetch({
document: GetLinksAndSectionsQuery,
customerAccessToken,
variables: { currencyCode },
// Since this query is needed on every page, it's a good idea not to validate the customer access token.
// The 'cache' function also caches errors, so we might get caught in a redirect loop if the cache saves an invalid token error response.
validateCustomerAccessToken: false,
fetchOptions: customerAccessToken ? { cache: 'no-store' } : { next: { revalidate } },
});
return readFragment(HeaderLinksFragment, response).site;
});
const getHeaderData = cache(async () => {
const { data: response } = await client.fetch({
document: LayoutQuery,
fetchOptions: { next: { revalidate } },
});
return readFragment(HeaderFragment, response).site;
});
export const Header = async () => {
const t = await getTranslations('Components.Header');
const locale = await getLocale();
const data = await getHeaderData();
const logo = data.settings ? logoTransformer(data.settings) : '';
const locales = routing.locales.map((enabledLocales) => ({
id: enabledLocales,
label: enabledLocales.toLocaleUpperCase(),
}));
const currencies = data.currencies.edges
? data.currencies.edges
// only show transactional currencies for now until cart prices can be rendered in display currencies
.filter(({ node }) => node.isTransactional)
.map(({ node }) => ({
id: node.code,
label: node.code,
isDefault: node.isDefault,
}))
: [];
const streamableLinks = Streamable.from(async () => {
const [customerAccessToken, currencyCode] = await Promise.all([
getSessionCustomerAccessToken(),
getPreferredCurrencyCode(),
]);
// const customerAccessToken = await getSessionCustomerAccessToken();
// const currencyCode = await getPreferredCurrencyCode();
const categoryTree = (await getHeaderLinks(customerAccessToken, currencyCode)).categoryTree;
/** To prevent the navigation menu from overflowing, we limit the number of categories to 6.
To show a full list of categories, modify the `slice` method to remove the limit.
Will require modification of navigation menu styles to accommodate the additional categories.
*/
const slicedTree = categoryTree.slice(0, 6);
return slicedTree.map(({ name, path, children }) => ({
label: name,
href: path,
groups: children.map((firstChild) => ({
label: firstChild.name,
href: firstChild.path,
links: firstChild.children.map((secondChild) => ({
label: secondChild.name,
href: secondChild.path,
})),
})),
}));
});
const streamableGiftCertificatesEnabled = Streamable.from(async () => {
const [customerAccessToken, currencyCode] = await Promise.all([
getSessionCustomerAccessToken(),
getPreferredCurrencyCode(),
]);
const giftCertificateSettings = (await getHeaderLinks(customerAccessToken, currencyCode))
.settings?.giftCertificates;
return giftCertificateSettings?.isEnabled ?? false;
});
const streamableCartCount = Streamable.from(async () => {
const cartId = await getCartId();
const customerAccessToken = await getSessionCustomerAccessToken();
if (!cartId) {
return null;
}
return getCartCount(cartId, customerAccessToken);
});
const streamableActiveCurrencyId = Streamable.from(async (): Promise<string | undefined> => {
const currencyCode = await getPreferredCurrencyCode();
const defaultCurrency = currencies.find(({ isDefault }) => isDefault);
return currencyCode ?? defaultCurrency?.id;
});
return (
<HeaderSection
navigation={{
accountHref: '/login',
accountLabel: t('Icons.account'),
cartHref: '/cart',
cartLabel: t('Icons.cart'),
giftCertificatesLabel: t('Icons.giftCertificates'),
giftCertificatesHref: '/gift-certificates',
giftCertificatesEnabled: streamableGiftCertificatesEnabled,
searchHref: '/search',
searchParamName: 'term',
searchAction: search,
searchInputPlaceholder: t('Search.inputPlaceholder'),
searchSubmitLabel: t('Search.submitLabel'),
links: streamableLinks,
logo,
mobileMenuTriggerLabel: t('toggleNavigation'),
openSearchPopupLabel: t('Icons.search'),
logoLabel: t('home'),
cartCount: streamableCartCount,
activeLocaleId: locale,
locales,
currencies,
activeCurrencyId: streamableActiveCurrencyId,
currencyAction: switchCurrency,
switchCurrencyLabel: t('SwitchCurrency.label'),
}}
/>
);
};