@faststore/core 2.2.0-alpha.7 → 2.2.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +2 -2
- package/.turbo/turbo-lint.log +5 -0
- package/.turbo/turbo-test.log +9 -9
- package/@generated/graphql/index.ts +30 -14
- package/@generated/graphql/persisted.json +5 -5
- package/package.json +4 -4
- package/src/components/product/ProductGrid/ProductGrid.tsx +2 -2
- package/src/components/search/SearchInput/SearchInput.tsx +12 -1
- package/src/components/search/SearchTop/SearchTop.tsx +7 -1
- package/src/components/sections/ProductTiles/ProductTiles.tsx +3 -2
- package/src/components/ui/ShippingSimulation/ShippingSimulation.tsx +11 -33
- package/src/customizations/fragments/{ClientProducts.ts → ClientManyProducts.ts} +1 -1
- package/src/customizations/fragments/ClientSearchSuggestions.ts +13 -0
- package/src/customizations/fragments/ClientShippingSimulation.ts +11 -0
- package/src/customizations/fragments/ClientTopSearchSuggestions.ts +13 -0
- package/src/customizations/fragments/{ServerProductPage.ts → ServerProduct.ts} +1 -1
- package/src/pages/[slug]/p.tsx +6 -7
- package/src/sdk/overrides/PageProvider.tsx +9 -5
- package/src/sdk/product/useLocalizedVariables.ts +2 -2
- package/src/sdk/product/usePageProductsQuery.ts +8 -8
- package/src/sdk/product/useProductsPrefetch.ts +4 -4
- package/src/sdk/product/useProductsQuery.ts +7 -7
- package/src/sdk/search/useSuggestions.ts +7 -9
- package/src/sdk/search/useTopSearch.ts +7 -12
- package/src/sdk/shipping/index.ts +6 -4
- package/src/sdk/shipping/useShippingSimulation.ts +29 -54
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
$ yarn partytown & yarn generate && next build
|
|
2
2
|
$ partytown copylib ./public/~partytown
|
|
3
|
-
Partytown lib copied to: /home/runner/work/faststore/faststore/packages/core/public/~partytown
|
|
4
3
|
$ faststore generate-graphql -c
|
|
4
|
+
Partytown lib copied to: /home/runner/work/faststore/faststore/packages/core/public/~partytown
|
|
5
5
|
success - GraphQL schema, types, and optimizations successfully generated 🎉
|
|
6
6
|
warn - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
|
|
7
7
|
Attention: Next.js now collects completely anonymous telemetry regarding usage.
|
|
@@ -43,7 +43,7 @@ Route (pages) Size First Load JS
|
|
|
43
43
|
├ chunks/framework-dfd14d7ce6600b03.js 45.3 kB
|
|
44
44
|
├ chunks/main-fd466221927468fd.js 23.9 kB
|
|
45
45
|
├ chunks/pages/_app-7db7de3d205714be.js 6.45 kB
|
|
46
|
-
├ chunks/webpack-
|
|
46
|
+
├ chunks/webpack-f745954873a4b6d7.js 2.25 kB
|
|
47
47
|
└ css/9e76fef1c9ca89af.css 3.06 kB
|
|
48
48
|
|
|
49
49
|
λ (Server) server-side renders at runtime (uses getInitialProps or getServerSideProps)
|
package/.turbo/turbo-lint.log
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
1
|
$ next lint
|
|
2
|
+
Attention: Next.js now collects completely anonymous telemetry regarding usage.
|
|
3
|
+
This information is used to shape Next.js' roadmap and prioritize features.
|
|
4
|
+
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
|
|
5
|
+
https://nextjs.org/telemetry
|
|
6
|
+
|
|
2
7
|
✔ No ESLint warnings or errors
|
package/.turbo/turbo-test.log
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
$ tsdx test
|
|
2
2
|
ts-jest[versions] (WARN) Version 29.1.0 of jest installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=25.0.0 <26.0.0). Please do not report issues in ts-jest if you are using unsupported versions.
|
|
3
3
|
ts-jest[versions] (WARN) Version 4.9.4 of typescript installed has not been tested with ts-jest. If you're experiencing issues, consider using a supported version (>=3.4.0 <4.0.0). Please do not report issues in ts-jest if you are using unsupported versions.
|
|
4
|
-
PASS test/server/index.test.ts (
|
|
4
|
+
PASS test/server/index.test.ts (10.363s)
|
|
5
5
|
FastStore GraphQL Layer
|
|
6
6
|
@faststore/api
|
|
7
|
-
✓ should return a valid GraphQL schema (
|
|
8
|
-
✓ should return a valid GraphQL schema contain all expected types (
|
|
9
|
-
✓ should return a valid GraphQL schema contain all expected queries (
|
|
7
|
+
✓ should return a valid GraphQL schema (8ms)
|
|
8
|
+
✓ should return a valid GraphQL schema contain all expected types (20ms)
|
|
9
|
+
✓ should return a valid GraphQL schema contain all expected queries (3ms)
|
|
10
10
|
✓ should return a valid GraphQL schema contain all expected mutations
|
|
11
11
|
VTEX API Extension
|
|
12
|
-
✓ getTypeDefsFromFolder function should return an Array (
|
|
12
|
+
✓ getTypeDefsFromFolder function should return an Array (8ms)
|
|
13
13
|
Third Party API Extension
|
|
14
|
-
✓ getTypeDefsFromFolder function should return an Array (
|
|
14
|
+
✓ getTypeDefsFromFolder function should return an Array (6ms)
|
|
15
15
|
Final Schema after merging
|
|
16
16
|
✓ should return a valid merged GraphQL schema (35ms)
|
|
17
17
|
Envelop
|
|
18
|
-
✓ should exist with its plugins (
|
|
19
|
-
✓ should handle options and execute (
|
|
18
|
+
✓ should exist with its plugins (38ms)
|
|
19
|
+
✓ should handle options and execute (413ms)
|
|
20
20
|
|
|
21
21
|
Test Suites: 1 passed, 1 total
|
|
22
22
|
Tests: 9 passed, 9 total
|
|
23
23
|
Snapshots: 0 total
|
|
24
|
-
Time:
|
|
24
|
+
Time: 11.351s
|
|
25
25
|
Ran all test suites.
|
|
@@ -1150,19 +1150,31 @@ export type ProductDetailsFragment_ProductFragment = {
|
|
|
1150
1150
|
}>
|
|
1151
1151
|
}
|
|
1152
1152
|
|
|
1153
|
+
export type ClientManyProductsFragment = {
|
|
1154
|
+
search: { products: { pageInfo: { totalCount: number } } }
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1153
1157
|
export type ClientProductFragment = { product: { id: string } }
|
|
1154
1158
|
|
|
1155
1159
|
export type ClientProductGalleryFragment = {
|
|
1156
1160
|
search: { products: { pageInfo: { totalCount: number } } }
|
|
1157
1161
|
}
|
|
1158
1162
|
|
|
1159
|
-
export type
|
|
1160
|
-
search: {
|
|
1163
|
+
export type ClientSearchSuggestionsFragment = {
|
|
1164
|
+
search: { suggestions: { terms: Array<{ value: string }> } }
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
export type ClientShippingSimulationFragment = {
|
|
1168
|
+
shipping: { address: { city: string | null } | null } | null
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
export type ClientTopSearchSuggestionsFragment = {
|
|
1172
|
+
search: { suggestions: { terms: Array<{ value: string }> } }
|
|
1161
1173
|
}
|
|
1162
1174
|
|
|
1163
1175
|
export type ServerCollectionPageFragment = { collection: { id: string } }
|
|
1164
1176
|
|
|
1165
|
-
export type
|
|
1177
|
+
export type ServerProductFragment = { product: { id: string } }
|
|
1166
1178
|
|
|
1167
1179
|
export type ServerCollectionPageQueryQueryVariables = Exact<{
|
|
1168
1180
|
slug: Scalars['String']
|
|
@@ -1179,11 +1191,11 @@ export type ServerCollectionPageQueryQuery = {
|
|
|
1179
1191
|
}
|
|
1180
1192
|
}
|
|
1181
1193
|
|
|
1182
|
-
export type
|
|
1194
|
+
export type ServerProductQueryQueryVariables = Exact<{
|
|
1183
1195
|
locator: Array<IStoreSelectedFacet> | IStoreSelectedFacet
|
|
1184
1196
|
}>
|
|
1185
1197
|
|
|
1186
|
-
export type
|
|
1198
|
+
export type ServerProductQueryQuery = {
|
|
1187
1199
|
product: {
|
|
1188
1200
|
sku: string
|
|
1189
1201
|
gtin: string
|
|
@@ -1333,7 +1345,7 @@ export type SubscribeToNewsletterMutation = {
|
|
|
1333
1345
|
subscribeToNewsletter: { id: string } | null
|
|
1334
1346
|
}
|
|
1335
1347
|
|
|
1336
|
-
export type
|
|
1348
|
+
export type ClientManyProductsQueryQueryVariables = Exact<{
|
|
1337
1349
|
first: Scalars['Int']
|
|
1338
1350
|
after: InputMaybe<Scalars['String']>
|
|
1339
1351
|
sort: StoreSort
|
|
@@ -1341,7 +1353,7 @@ export type ClientProductsQueryQueryVariables = Exact<{
|
|
|
1341
1353
|
selectedFacets: Array<IStoreSelectedFacet> | IStoreSelectedFacet
|
|
1342
1354
|
}>
|
|
1343
1355
|
|
|
1344
|
-
export type
|
|
1356
|
+
export type ClientManyProductsQueryQuery = {
|
|
1345
1357
|
search: {
|
|
1346
1358
|
products: {
|
|
1347
1359
|
pageInfo: { totalCount: number }
|
|
@@ -1451,12 +1463,12 @@ export type ClientProductQueryQuery = {
|
|
|
1451
1463
|
}
|
|
1452
1464
|
}
|
|
1453
1465
|
|
|
1454
|
-
export type
|
|
1466
|
+
export type ClientSearchSuggestionsQueryQueryVariables = Exact<{
|
|
1455
1467
|
term: Scalars['String']
|
|
1456
1468
|
selectedFacets: InputMaybe<Array<IStoreSelectedFacet> | IStoreSelectedFacet>
|
|
1457
1469
|
}>
|
|
1458
1470
|
|
|
1459
|
-
export type
|
|
1471
|
+
export type ClientSearchSuggestionsQueryQuery = {
|
|
1460
1472
|
search: {
|
|
1461
1473
|
suggestions: {
|
|
1462
1474
|
terms: Array<{ value: string }>
|
|
@@ -1486,12 +1498,12 @@ export type SearchSuggestionsQueryQuery = {
|
|
|
1486
1498
|
}
|
|
1487
1499
|
}
|
|
1488
1500
|
|
|
1489
|
-
export type
|
|
1501
|
+
export type ClientTopSearchSuggestionsQueryQueryVariables = Exact<{
|
|
1490
1502
|
term: Scalars['String']
|
|
1491
1503
|
selectedFacets: InputMaybe<Array<IStoreSelectedFacet> | IStoreSelectedFacet>
|
|
1492
1504
|
}>
|
|
1493
1505
|
|
|
1494
|
-
export type
|
|
1506
|
+
export type ClientTopSearchSuggestionsQueryQuery = {
|
|
1495
1507
|
search: { suggestions: { terms: Array<{ value: string }> } }
|
|
1496
1508
|
}
|
|
1497
1509
|
|
|
@@ -1523,13 +1535,13 @@ export type ValidateSessionMutation = {
|
|
|
1523
1535
|
} | null
|
|
1524
1536
|
}
|
|
1525
1537
|
|
|
1526
|
-
export type
|
|
1538
|
+
export type ClientShippingSimulationQueryQueryVariables = Exact<{
|
|
1527
1539
|
postalCode: Scalars['String']
|
|
1528
1540
|
country: Scalars['String']
|
|
1529
1541
|
items: Array<IShippingItem> | IShippingItem
|
|
1530
1542
|
}>
|
|
1531
1543
|
|
|
1532
|
-
export type
|
|
1544
|
+
export type ClientShippingSimulationQueryQuery = {
|
|
1533
1545
|
shipping: {
|
|
1534
1546
|
logisticsInfo: Array<{
|
|
1535
1547
|
slas: Array<{
|
|
@@ -1545,6 +1557,10 @@ export type ShippingSimulationQueryQuery = {
|
|
|
1545
1557
|
} | null> | null
|
|
1546
1558
|
} | null> | null
|
|
1547
1559
|
} | null> | null
|
|
1548
|
-
address: {
|
|
1560
|
+
address: {
|
|
1561
|
+
city: string | null
|
|
1562
|
+
neighborhood: string | null
|
|
1563
|
+
state: string | null
|
|
1564
|
+
} | null
|
|
1549
1565
|
} | null
|
|
1550
1566
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"ServerCollectionPageQuery": "query ServerCollectionPageQuery($slug: String!) {\n collection(slug: $slug) {\n id\n seo {\n title\n description\n }\n breadcrumbList {\n itemListElement {\n item\n name\n position\n }\n }\n meta {\n selectedFacets {\n key\n value\n }\n }\n }\n}\n",
|
|
3
|
-
"
|
|
3
|
+
"ServerProductQuery": "query ServerProductQuery($locator: [IStoreSelectedFacet!]!) {\n product(locator: $locator) {\n id: productID\n seo {\n title\n description\n canonical\n }\n brand {\n name\n }\n sku\n gtin\n name\n description\n releaseDate\n breadcrumbList {\n itemListElement {\n item\n name\n position\n }\n }\n image {\n url\n alternateName\n }\n offers {\n lowPrice\n highPrice\n priceCurrency\n offers {\n availability\n price\n priceValidUntil\n priceCurrency\n itemCondition\n seller {\n identifier\n }\n listPrice\n }\n }\n isVariantOf {\n productGroupID\n name\n skuVariants {\n activeVariations\n slugsMap\n availableVariations\n }\n }\n additionalProperty {\n propertyID\n name\n value\n valueReference\n }\n }\n}\n",
|
|
4
4
|
"ValidateCartMutation": "mutation ValidateCartMutation($cart: IStoreCart!, $session: IStoreSession!) {\n validateCart(cart: $cart, session: $session) {\n order {\n orderNumber\n acceptedOffer {\n seller {\n identifier\n }\n quantity\n price\n listPrice\n itemOffered {\n sku\n name\n image {\n url\n alternateName\n }\n brand {\n name\n }\n isVariantOf {\n productGroupID\n name\n skuVariants {\n activeVariations\n slugsMap\n availableVariations\n }\n }\n gtin\n additionalProperty {\n propertyID\n name\n value\n valueReference\n }\n }\n }\n }\n messages {\n text\n status\n }\n }\n}\n",
|
|
5
5
|
"SubscribeToNewsletter": "mutation SubscribeToNewsletter($data: IPersonNewsletter!) {\n subscribeToNewsletter(data: $data) {\n id\n }\n}\n",
|
|
6
|
-
"
|
|
6
|
+
"ClientManyProductsQuery": "query ClientManyProductsQuery($first: Int!, $after: String, $sort: StoreSort!, $term: String!, $selectedFacets: [IStoreSelectedFacet!]!) {\n search(\n first: $first\n after: $after\n sort: $sort\n term: $term\n selectedFacets: $selectedFacets\n ) {\n products {\n pageInfo {\n totalCount\n }\n edges {\n node {\n id: productID\n slug\n sku\n brand {\n brandName: name\n name\n }\n name\n gtin\n isVariantOf {\n productGroupID\n name\n }\n image {\n url\n alternateName\n }\n offers {\n lowPrice\n offers {\n availability\n price\n listPrice\n quantity\n seller {\n identifier\n }\n }\n }\n }\n }\n }\n }\n}\n",
|
|
7
7
|
"ClientProductGalleryQuery": "query ClientProductGalleryQuery($first: Int!, $after: String!, $sort: StoreSort!, $term: String!, $selectedFacets: [IStoreSelectedFacet!]!) {\n search(\n first: $first\n after: $after\n sort: $sort\n term: $term\n selectedFacets: $selectedFacets\n ) {\n products {\n pageInfo {\n totalCount\n }\n }\n facets {\n ... on StoreFacetRange {\n key\n label\n min {\n selected\n absolute\n }\n max {\n selected\n absolute\n }\n __typename\n }\n ... on StoreFacetBoolean {\n key\n label\n values {\n label\n value\n selected\n quantity\n }\n __typename\n }\n }\n metadata {\n isTermMisspelled\n logicalOperator\n }\n }\n}\n",
|
|
8
8
|
"ClientProductQuery": "query ClientProductQuery($locator: [IStoreSelectedFacet!]!) {\n product(locator: $locator) {\n id: productID\n sku\n name\n gtin\n description\n isVariantOf {\n name\n productGroupID\n skuVariants {\n activeVariations\n slugsMap\n availableVariations\n }\n }\n image {\n url\n alternateName\n }\n brand {\n name\n }\n offers {\n lowPrice\n offers {\n availability\n price\n listPrice\n seller {\n identifier\n }\n }\n }\n additionalProperty {\n propertyID\n name\n value\n valueReference\n }\n }\n}\n",
|
|
9
|
-
"
|
|
10
|
-
"
|
|
9
|
+
"ClientSearchSuggestionsQuery": "query ClientSearchSuggestionsQuery($term: String!, $selectedFacets: [IStoreSelectedFacet!]) {\n search(first: 5, term: $term, selectedFacets: $selectedFacets) {\n suggestions {\n terms {\n value\n }\n products {\n id: productID\n slug\n sku\n brand {\n brandName: name\n name\n }\n name\n gtin\n isVariantOf {\n productGroupID\n name\n }\n image {\n url\n alternateName\n }\n offers {\n lowPrice\n offers {\n availability\n price\n listPrice\n quantity\n seller {\n identifier\n }\n }\n }\n }\n }\n products {\n pageInfo {\n totalCount\n }\n }\n metadata {\n isTermMisspelled\n logicalOperator\n }\n }\n}\n",
|
|
10
|
+
"ClientTopSearchSuggestionsQuery": "query ClientTopSearchSuggestionsQuery($term: String!, $selectedFacets: [IStoreSelectedFacet!]) {\n search(first: 5, term: $term, selectedFacets: $selectedFacets) {\n suggestions {\n terms {\n value\n }\n }\n }\n}\n",
|
|
11
11
|
"ValidateSession": "mutation ValidateSession($session: IStoreSession!, $search: String!) {\n validateSession(session: $session, search: $search) {\n locale\n channel\n country\n addressType\n postalCode\n deliveryMode {\n deliveryChannel\n deliveryMethod\n deliveryWindow {\n startDate\n endDate\n }\n }\n geoCoordinates {\n latitude\n longitude\n }\n currency {\n code\n symbol\n }\n person {\n id\n email\n givenName\n familyName\n }\n }\n}\n",
|
|
12
|
-
"
|
|
12
|
+
"ClientShippingSimulationQuery": "query ClientShippingSimulationQuery($postalCode: String!, $country: String!, $items: [IShippingItem!]!) {\n shipping(items: $items, postalCode: $postalCode, country: $country) {\n address {\n city\n neighborhood\n state\n }\n logisticsInfo {\n slas {\n carrier\n price\n availableDeliveryWindows {\n startDateUtc\n endDateUtc\n price\n listPrice\n }\n shippingEstimate\n localizedEstimates\n }\n }\n }\n}\n"
|
|
13
13
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faststore/core",
|
|
3
|
-
"version": "2.2.0-alpha.
|
|
3
|
+
"version": "2.2.0-alpha.9",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"browserslist": "supports es6-module and not dead",
|
|
6
6
|
"exports": {
|
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
"@envelop/parser-cache": "^2.2.0",
|
|
40
40
|
"@envelop/validation-cache": "^2.2.0",
|
|
41
41
|
"@faststore/api": "^2.2.0-alpha.6",
|
|
42
|
-
"@faststore/components": "^2.2.0-alpha.
|
|
42
|
+
"@faststore/components": "^2.2.0-alpha.9",
|
|
43
43
|
"@faststore/graphql-utils": "^2.2.0-alpha.1",
|
|
44
44
|
"@faststore/sdk": "^2.2.0-alpha.1",
|
|
45
|
-
"@faststore/ui": "^2.2.0-alpha.
|
|
45
|
+
"@faststore/ui": "^2.2.0-alpha.9",
|
|
46
46
|
"@graphql-codegen/cli": "^3.3.1",
|
|
47
47
|
"@graphql-codegen/typescript": "^3.0.4",
|
|
48
48
|
"@graphql-codegen/typescript-operations": "^3.0.4",
|
|
@@ -129,5 +129,5 @@
|
|
|
129
129
|
"msw": {
|
|
130
130
|
"workerDirectory": "public"
|
|
131
131
|
},
|
|
132
|
-
"gitHead": "
|
|
132
|
+
"gitHead": "79b9eb42f6f8fb7fdead1141f384c7b826357fa3"
|
|
133
133
|
}
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
ProductGrid as UIProductGrid,
|
|
3
3
|
ProductGridItem as UIProductGridItem,
|
|
4
4
|
} from '@faststore/ui'
|
|
5
|
-
import type {
|
|
5
|
+
import type { ClientManyProductsQueryQuery } from '@generated/graphql'
|
|
6
6
|
import ProductGridSkeleton from 'src/components/skeletons/ProductGridSkeleton'
|
|
7
7
|
|
|
8
8
|
import { ProductCardProps } from '../ProductCard'
|
|
@@ -14,7 +14,7 @@ interface Props {
|
|
|
14
14
|
/**
|
|
15
15
|
* Products listed on the grid.
|
|
16
16
|
*/
|
|
17
|
-
products:
|
|
17
|
+
products: ClientManyProductsQueryQuery['search']['products']['edges']
|
|
18
18
|
page: number
|
|
19
19
|
/**
|
|
20
20
|
* Quantity of products listed.
|
|
@@ -30,6 +30,8 @@ const SearchDropdown = lazy(
|
|
|
30
30
|
() => import('src/components/search/SearchDropdown')
|
|
31
31
|
)
|
|
32
32
|
|
|
33
|
+
const MAX_SUGGESTIONS = 5
|
|
34
|
+
|
|
33
35
|
export type SearchInputProps = {
|
|
34
36
|
onSearchClick?: () => void
|
|
35
37
|
buttonTestId?: string
|
|
@@ -84,7 +86,16 @@ const SearchInput = forwardRef<SearchInputRef, SearchInputProps>(
|
|
|
84
86
|
|
|
85
87
|
useOnClickOutside(searchRef, () => setSearchDropdownVisible(false))
|
|
86
88
|
|
|
87
|
-
const {
|
|
89
|
+
const { data, error } = useSuggestions(searchQueryDeferred)
|
|
90
|
+
const terms = (data?.search.suggestions.terms ?? []).slice(
|
|
91
|
+
0,
|
|
92
|
+
MAX_SUGGESTIONS
|
|
93
|
+
)
|
|
94
|
+
const products = (data?.search.suggestions.products ?? []).slice(
|
|
95
|
+
0,
|
|
96
|
+
MAX_SUGGESTIONS
|
|
97
|
+
)
|
|
98
|
+
const isLoading = !error && !data
|
|
88
99
|
|
|
89
100
|
return (
|
|
90
101
|
<UISearchInput
|
|
@@ -10,6 +10,8 @@ import type { StoreSuggestionTerm } from '@generated/graphql'
|
|
|
10
10
|
import { formatSearchPath } from 'src/sdk/search/formatSearchPath'
|
|
11
11
|
import useTopSearch from 'src/sdk/search/useTopSearch'
|
|
12
12
|
|
|
13
|
+
const MAX_TOP_SEARCH_TERMS = 5
|
|
14
|
+
|
|
13
15
|
export interface SearchTopProps extends HTMLAttributes<HTMLDivElement> {
|
|
14
16
|
/**
|
|
15
17
|
* List of top searched items
|
|
@@ -25,7 +27,11 @@ function SearchTop({ topTerms, sort, ...otherProps }: SearchTopProps) {
|
|
|
25
27
|
const {
|
|
26
28
|
values: { onSearchSelection },
|
|
27
29
|
} = useSearch()
|
|
28
|
-
const {
|
|
30
|
+
const { data } = useTopSearch()
|
|
31
|
+
const terms = (data?.search.suggestions.terms ?? topTerms).slice(
|
|
32
|
+
0,
|
|
33
|
+
MAX_TOP_SEARCH_TERMS
|
|
34
|
+
)
|
|
29
35
|
|
|
30
36
|
if (terms.length === 0) {
|
|
31
37
|
return null
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useEffect, useRef } from 'react'
|
|
2
2
|
import { useInView } from 'react-intersection-observer'
|
|
3
3
|
|
|
4
|
-
import type {
|
|
4
|
+
import type { ClientManyProductsQueryQueryVariables } from '@generated/graphql'
|
|
5
5
|
import ProductCard from 'src/components/product/ProductCard'
|
|
6
6
|
import ProductTilesSkeleton from 'src/components/skeletons/ProductTilesSkeleton'
|
|
7
7
|
import Tiles, { Tile } from 'src/components/ui/Tiles'
|
|
@@ -12,7 +12,8 @@ import Section from '../Section'
|
|
|
12
12
|
|
|
13
13
|
import styles from './section.module.scss'
|
|
14
14
|
|
|
15
|
-
interface ProductTilesProps
|
|
15
|
+
interface ProductTilesProps
|
|
16
|
+
extends Partial<ClientManyProductsQueryQueryVariables> {
|
|
16
17
|
title: string
|
|
17
18
|
}
|
|
18
19
|
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { ShippingSimulationProps as UIShippingSimulationProps } from '@faststore/ui'
|
|
2
2
|
|
|
3
|
-
import { getShippingSimulation } from 'src/sdk/shipping'
|
|
4
|
-
import { ShippingSla } from '@generated/graphql'
|
|
5
3
|
import { useSession } from 'src/sdk/session'
|
|
6
|
-
import { IShippingItem } from '@faststore/api'
|
|
7
4
|
import { useShippingSimulation } from 'src/sdk/shipping/useShippingSimulation'
|
|
8
5
|
|
|
9
6
|
import { ShippingSimulation as ShippingSimulationWrapper } from 'src/components/sections/ProductDetails/Overrides'
|
|
@@ -26,27 +23,6 @@ interface ShippingSimulationProps
|
|
|
26
23
|
}
|
|
27
24
|
}
|
|
28
25
|
|
|
29
|
-
const fetchShippingSimulation = async (
|
|
30
|
-
shippingItem: IShippingItem,
|
|
31
|
-
country: string,
|
|
32
|
-
postalCode: string
|
|
33
|
-
): Promise<[string, ShippingSla[]]> => {
|
|
34
|
-
const shipping = await getShippingSimulation({
|
|
35
|
-
country,
|
|
36
|
-
postalCode: postalCode,
|
|
37
|
-
items: [shippingItem],
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
const location =
|
|
41
|
-
[shipping?.address?.neighborhood, shipping?.address?.city]
|
|
42
|
-
.filter(Boolean)
|
|
43
|
-
.join(' / ') ?? ''
|
|
44
|
-
|
|
45
|
-
const options = shipping?.logisticsInfo?.[0]?.slas ?? []
|
|
46
|
-
|
|
47
|
-
return [location, options as ShippingSla[]]
|
|
48
|
-
}
|
|
49
|
-
|
|
50
26
|
export default function ShippingSimulation({
|
|
51
27
|
productShippingInfo,
|
|
52
28
|
formatter,
|
|
@@ -55,24 +31,25 @@ export default function ShippingSimulation({
|
|
|
55
31
|
idkPostalCodeLinkProps,
|
|
56
32
|
...otherProps
|
|
57
33
|
}: ShippingSimulationProps) {
|
|
58
|
-
const { country, postalCode: sessionPostalCode } = useSession()
|
|
59
|
-
|
|
60
34
|
const {
|
|
61
35
|
input,
|
|
62
36
|
shippingSimulation,
|
|
63
37
|
handleSubmit,
|
|
64
38
|
handleOnInput,
|
|
65
39
|
handleOnClear,
|
|
66
|
-
} = useShippingSimulation(
|
|
67
|
-
productShippingInfo,
|
|
68
|
-
fetchShippingSimulation,
|
|
69
|
-
sessionPostalCode,
|
|
70
|
-
country
|
|
71
|
-
)
|
|
40
|
+
} = useShippingSimulation(productShippingInfo)
|
|
72
41
|
|
|
73
42
|
const { postalCode, displayClearButton, errorMessage } = input
|
|
74
43
|
|
|
75
|
-
const
|
|
44
|
+
const location =
|
|
45
|
+
[
|
|
46
|
+
shippingSimulation?.address?.neighborhood,
|
|
47
|
+
shippingSimulation?.address?.city,
|
|
48
|
+
]
|
|
49
|
+
.filter(Boolean)
|
|
50
|
+
.join(' / ') ?? ''
|
|
51
|
+
|
|
52
|
+
const options = shippingSimulation?.logisticsInfo?.[0]?.slas ?? []
|
|
76
53
|
|
|
77
54
|
return (
|
|
78
55
|
<ShippingSimulationWrapper.Component
|
|
@@ -82,6 +59,7 @@ export default function ShippingSimulation({
|
|
|
82
59
|
onClear={handleOnClear}
|
|
83
60
|
location={location}
|
|
84
61
|
options={options}
|
|
62
|
+
address={shippingSimulation?.address}
|
|
85
63
|
displayClearButton={displayClearButton}
|
|
86
64
|
errorMessage={errorMessage}
|
|
87
65
|
postalCode={postalCode}
|
package/src/pages/[slug]/p.tsx
CHANGED
|
@@ -7,9 +7,8 @@ import type { ComponentType } from 'react'
|
|
|
7
7
|
import deepmerge from 'deepmerge'
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
ServerProductPageQueryQueryVariables,
|
|
10
|
+
ServerProductQueryQuery,
|
|
11
|
+
ServerProductQueryQueryVariables,
|
|
13
12
|
} from '@generated/graphql'
|
|
14
13
|
import RenderSections from 'src/components/cms/RenderSections'
|
|
15
14
|
import BannerNewsletter from 'src/components/sections/BannerNewsletter/BannerNewsletter'
|
|
@@ -44,7 +43,7 @@ const COMPONENTS: Record<string, ComponentType<any>> = {
|
|
|
44
43
|
}
|
|
45
44
|
|
|
46
45
|
type Props = PDPContentType & {
|
|
47
|
-
data:
|
|
46
|
+
data: ServerProductQueryQuery
|
|
48
47
|
globalSections: GlobalSectionsData
|
|
49
48
|
meta: {
|
|
50
49
|
title: string
|
|
@@ -134,8 +133,8 @@ function Page({ data: server, sections, globalSections, offers, meta }: Props) {
|
|
|
134
133
|
}
|
|
135
134
|
|
|
136
135
|
const query = gql`
|
|
137
|
-
query
|
|
138
|
-
...
|
|
136
|
+
query ServerProductQuery($locator: [IStoreSelectedFacet!]!) {
|
|
137
|
+
...ServerProduct
|
|
139
138
|
product(locator: $locator) {
|
|
140
139
|
id: productID
|
|
141
140
|
|
|
@@ -200,7 +199,7 @@ export const getStaticProps: GetStaticProps<
|
|
|
200
199
|
> = async ({ params, previewData }) => {
|
|
201
200
|
const slug = params?.slug ?? ''
|
|
202
201
|
const [searchResult, cmsPage, globalSections] = await Promise.all([
|
|
203
|
-
execute<
|
|
202
|
+
execute<ServerProductQueryQueryVariables, ServerProductQueryQuery>({
|
|
204
203
|
variables: { locator: [{ key: 'slug', value: slug }] },
|
|
205
204
|
operationName: query,
|
|
206
205
|
}),
|
|
@@ -1,27 +1,31 @@
|
|
|
1
1
|
import {
|
|
2
2
|
ClientProductGalleryQueryQuery,
|
|
3
3
|
ClientProductQueryQuery,
|
|
4
|
-
|
|
4
|
+
ClientManyProductsQueryQuery,
|
|
5
5
|
ServerCollectionPageQueryQuery,
|
|
6
|
-
|
|
6
|
+
ServerProductQueryQuery,
|
|
7
7
|
} from '@generated/graphql'
|
|
8
8
|
import type { PropsWithChildren } from 'react'
|
|
9
9
|
import { createContext, useContext, useMemo } from 'react'
|
|
10
10
|
import { SearchPageContextType } from 'src/pages/s'
|
|
11
11
|
|
|
12
12
|
export interface PDPContext {
|
|
13
|
-
data?:
|
|
13
|
+
data?: ServerProductQueryQuery &
|
|
14
14
|
ClientProductQueryQuery['product'] & { isValidating?: boolean }
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export interface PLPContext {
|
|
18
18
|
data?: ServerCollectionPageQueryQuery &
|
|
19
|
-
ClientProductGalleryQueryQuery & {
|
|
19
|
+
ClientProductGalleryQueryQuery & {
|
|
20
|
+
pages: ClientManyProductsQueryQuery[]
|
|
21
|
+
}
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
export interface SearchPageContext {
|
|
23
25
|
data?: SearchPageContextType &
|
|
24
|
-
ClientProductGalleryQueryQuery & {
|
|
26
|
+
ClientProductGalleryQueryQuery & {
|
|
27
|
+
pages: ClientManyProductsQueryQuery[]
|
|
28
|
+
}
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
export const isPDP = (x: any): x is PDPContext =>
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useMemo } from 'react'
|
|
2
|
-
import {
|
|
2
|
+
import { ClientManyProductsQueryQueryVariables } from '@generated/graphql'
|
|
3
3
|
import { useSession } from '../session'
|
|
4
4
|
import { ITEMS_PER_SECTION } from 'src/constants'
|
|
5
5
|
|
|
@@ -12,7 +12,7 @@ export const useLocalizedVariables = ({
|
|
|
12
12
|
sort,
|
|
13
13
|
term,
|
|
14
14
|
selectedFacets,
|
|
15
|
-
}: Partial<
|
|
15
|
+
}: Partial<ClientManyProductsQueryQueryVariables>) => {
|
|
16
16
|
const { channel, locale } = useSession()
|
|
17
17
|
|
|
18
18
|
return useMemo(() => {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { gql } from '@faststore/graphql-utils'
|
|
2
2
|
import { useSearch } from '@faststore/sdk'
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
ClientManyProductsQueryQuery,
|
|
5
|
+
ClientManyProductsQueryQueryVariables,
|
|
6
6
|
} from '@generated/graphql'
|
|
7
7
|
import {
|
|
8
8
|
useEffect,
|
|
@@ -31,14 +31,14 @@ export const useGalleryPage = (page: number) => {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
export const query = gql`
|
|
34
|
-
query
|
|
34
|
+
query ClientManyProductsQuery(
|
|
35
35
|
$first: Int!
|
|
36
36
|
$after: String
|
|
37
37
|
$sort: StoreSort!
|
|
38
38
|
$term: String!
|
|
39
39
|
$selectedFacets: [IStoreSelectedFacet!]!
|
|
40
40
|
) {
|
|
41
|
-
...
|
|
41
|
+
...ClientManyProducts
|
|
42
42
|
search(
|
|
43
43
|
first: $first
|
|
44
44
|
after: $after
|
|
@@ -66,9 +66,9 @@ const getKey = (object: any) => JSON.stringify(object)
|
|
|
66
66
|
* Use this hook for managed pages state and creating useGalleryPage hook that will be used for fetching a list of products per pages in PLP or Search
|
|
67
67
|
*/
|
|
68
68
|
export const useCreateUseGalleryPage = () => {
|
|
69
|
-
const [pages, setPages] = useState<
|
|
69
|
+
const [pages, setPages] = useState<ClientManyProductsQueryQuery[]>([])
|
|
70
70
|
// We create pagesRef as a mirror of the pages state so we don't have to add pages as a dependency of the useGalleryPage hook
|
|
71
|
-
const pagesRef = useRef<
|
|
71
|
+
const pagesRef = useRef<ClientManyProductsQueryQuery[]>([])
|
|
72
72
|
const pagesCache = useRef<string[]>([])
|
|
73
73
|
|
|
74
74
|
const useGalleryPage = useCallback(function useGalleryPage(page: number) {
|
|
@@ -89,8 +89,8 @@ export const useCreateUseGalleryPage = () => {
|
|
|
89
89
|
pagesCache.current[page] === getKey(localizedVariables)
|
|
90
90
|
|
|
91
91
|
const { data } = useQuery<
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
ClientManyProductsQueryQuery,
|
|
93
|
+
ClientManyProductsQueryQueryVariables
|
|
94
94
|
>(query, localizedVariables, {
|
|
95
95
|
fallbackData: null,
|
|
96
96
|
suspense: true,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { gql } from '@faststore/graphql-utils'
|
|
2
2
|
import { useSearch } from '@faststore/sdk'
|
|
3
|
-
import {
|
|
3
|
+
import { ClientManyProductsQueryQueryVariables } from '@generated/graphql'
|
|
4
4
|
import { useEffect, useCallback } from 'react'
|
|
5
5
|
import type { QueryOptions } from '../graphql/useQuery'
|
|
6
6
|
import { useSWRConfig } from 'swr'
|
|
@@ -8,14 +8,14 @@ import { prefetchQuery } from '../graphql/prefetchQuery'
|
|
|
8
8
|
import { useLocalizedVariables } from './useLocalizedVariables'
|
|
9
9
|
|
|
10
10
|
export const query = gql`
|
|
11
|
-
query
|
|
11
|
+
query ClientManyProductsQuery(
|
|
12
12
|
$first: Int!
|
|
13
13
|
$after: String
|
|
14
14
|
$sort: StoreSort!
|
|
15
15
|
$term: String!
|
|
16
16
|
$selectedFacets: [IStoreSelectedFacet!]!
|
|
17
17
|
) {
|
|
18
|
-
...
|
|
18
|
+
...ClientManyProducts
|
|
19
19
|
search(
|
|
20
20
|
first: $first
|
|
21
21
|
after: $after
|
|
@@ -38,7 +38,7 @@ export const query = gql`
|
|
|
38
38
|
`
|
|
39
39
|
|
|
40
40
|
export const useProductsQueryPrefetch = (
|
|
41
|
-
variables:
|
|
41
|
+
variables: ClientManyProductsQueryQueryVariables,
|
|
42
42
|
options?: QueryOptions
|
|
43
43
|
) => {
|
|
44
44
|
const localizedVariables = useLocalizedVariables(variables)
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { gql } from '@faststore/graphql-utils'
|
|
2
2
|
|
|
3
3
|
import type {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
ClientManyProductsQueryQuery,
|
|
5
|
+
ClientManyProductsQueryQueryVariables,
|
|
6
6
|
} from '@generated/graphql'
|
|
7
7
|
|
|
8
8
|
import type { QueryOptions } from '../graphql/useQuery'
|
|
@@ -10,14 +10,14 @@ import { useQuery } from '../graphql/useQuery'
|
|
|
10
10
|
import { useLocalizedVariables } from './useLocalizedVariables'
|
|
11
11
|
|
|
12
12
|
export const query = gql`
|
|
13
|
-
query
|
|
13
|
+
query ClientManyProductsQuery(
|
|
14
14
|
$first: Int!
|
|
15
15
|
$after: String
|
|
16
16
|
$sort: StoreSort!
|
|
17
17
|
$term: String!
|
|
18
18
|
$selectedFacets: [IStoreSelectedFacet!]!
|
|
19
19
|
) {
|
|
20
|
-
...
|
|
20
|
+
...ClientManyProducts
|
|
21
21
|
search(
|
|
22
22
|
first: $first
|
|
23
23
|
after: $after
|
|
@@ -43,14 +43,14 @@ export const query = gql`
|
|
|
43
43
|
* Use this hook for fetching a list of products, like shelves and tiles
|
|
44
44
|
*/
|
|
45
45
|
export const useProductsQuery = (
|
|
46
|
-
variables: Partial<
|
|
46
|
+
variables: Partial<ClientManyProductsQueryQueryVariables>,
|
|
47
47
|
options?: QueryOptions
|
|
48
48
|
) => {
|
|
49
49
|
const localizedVariables = useLocalizedVariables(variables)
|
|
50
50
|
|
|
51
51
|
const { data } = useQuery<
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
ClientManyProductsQueryQuery,
|
|
53
|
+
ClientManyProductsQueryQueryVariables
|
|
54
54
|
>(query, localizedVariables, {
|
|
55
55
|
fallbackData: null,
|
|
56
56
|
suspense: true,
|
|
@@ -5,20 +5,19 @@ import { gql } from '@faststore/graphql-utils'
|
|
|
5
5
|
import { useQuery } from 'src/sdk/graphql/useQuery'
|
|
6
6
|
|
|
7
7
|
import type {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
ClientSearchSuggestionsQueryQuery as Query,
|
|
9
|
+
ClientSearchSuggestionsQueryQueryVariables as Variables,
|
|
10
10
|
} from '@generated/graphql'
|
|
11
11
|
import type { IntelligentSearchQueryEvent } from '../analytics/types'
|
|
12
12
|
|
|
13
13
|
import { useSession } from '../session'
|
|
14
14
|
|
|
15
|
-
const MAX_SUGGESTIONS = 5
|
|
16
|
-
|
|
17
15
|
const query = gql`
|
|
18
|
-
query
|
|
16
|
+
query ClientSearchSuggestionsQuery(
|
|
19
17
|
$term: String!
|
|
20
18
|
$selectedFacets: [IStoreSelectedFacet!]
|
|
21
19
|
) {
|
|
20
|
+
...ClientSearchSuggestions
|
|
22
21
|
search(first: 5, term: $term, selectedFacets: $selectedFacets) {
|
|
23
22
|
suggestions {
|
|
24
23
|
terms {
|
|
@@ -40,7 +39,7 @@ const query = gql`
|
|
|
40
39
|
}
|
|
41
40
|
`
|
|
42
41
|
|
|
43
|
-
function useSuggestions(term: string
|
|
42
|
+
function useSuggestions(term: string) {
|
|
44
43
|
const { channel, locale } = useSession()
|
|
45
44
|
|
|
46
45
|
const variables = useMemo(
|
|
@@ -74,9 +73,8 @@ function useSuggestions(term: string, limit: number = MAX_SUGGESTIONS) {
|
|
|
74
73
|
})
|
|
75
74
|
|
|
76
75
|
return {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
isLoading: !error && !data,
|
|
76
|
+
data,
|
|
77
|
+
error,
|
|
80
78
|
}
|
|
81
79
|
}
|
|
82
80
|
|
|
@@ -2,20 +2,18 @@ import { gql } from '@faststore/graphql-utils'
|
|
|
2
2
|
|
|
3
3
|
import { useQuery } from 'src/sdk/graphql/useQuery'
|
|
4
4
|
import type {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
SearchSuggestionsQueryQueryVariables as Variables,
|
|
5
|
+
ClientSearchSuggestionsQueryQuery as Query,
|
|
6
|
+
ClientSearchSuggestionsQueryQueryVariables as Variables,
|
|
8
7
|
} from '@generated/graphql'
|
|
9
8
|
|
|
10
9
|
import { useSession } from '../session'
|
|
11
10
|
|
|
12
|
-
const MAX_TOP_SEARCH_TERMS = 5
|
|
13
|
-
|
|
14
11
|
const query = gql`
|
|
15
|
-
query
|
|
12
|
+
query ClientTopSearchSuggestionsQuery(
|
|
16
13
|
$term: String!
|
|
17
14
|
$selectedFacets: [IStoreSelectedFacet!]
|
|
18
15
|
) {
|
|
16
|
+
...ClientTopSearchSuggestions
|
|
19
17
|
search(first: 5, term: $term, selectedFacets: $selectedFacets) {
|
|
20
18
|
suggestions {
|
|
21
19
|
terms {
|
|
@@ -26,10 +24,7 @@ const query = gql`
|
|
|
26
24
|
}
|
|
27
25
|
`
|
|
28
26
|
|
|
29
|
-
function useTopSearch(
|
|
30
|
-
initialTerms: StoreSuggestionTerm[] = [],
|
|
31
|
-
limit: number = MAX_TOP_SEARCH_TERMS
|
|
32
|
-
) {
|
|
27
|
+
function useTopSearch() {
|
|
33
28
|
const { channel, locale } = useSession()
|
|
34
29
|
|
|
35
30
|
const { data, error } = useQuery<Query, Variables>(query, {
|
|
@@ -41,8 +36,8 @@ function useTopSearch(
|
|
|
41
36
|
})
|
|
42
37
|
|
|
43
38
|
return {
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
data,
|
|
40
|
+
error,
|
|
46
41
|
}
|
|
47
42
|
}
|
|
48
43
|
|
|
@@ -2,18 +2,19 @@ import type { IShippingItem } from '@faststore/api'
|
|
|
2
2
|
import { gql } from '@faststore/graphql-utils'
|
|
3
3
|
|
|
4
4
|
import type {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
ClientShippingSimulationQueryQuery as Query,
|
|
6
|
+
ClientShippingSimulationQueryQueryVariables as Variables,
|
|
7
7
|
} from '@generated/graphql'
|
|
8
8
|
|
|
9
9
|
import { request } from '../graphql/request'
|
|
10
10
|
|
|
11
11
|
const query = gql`
|
|
12
|
-
query
|
|
12
|
+
query ClientShippingSimulationQuery(
|
|
13
13
|
$postalCode: String!
|
|
14
14
|
$country: String!
|
|
15
15
|
$items: [IShippingItem!]!
|
|
16
16
|
) {
|
|
17
|
+
...ClientShippingSimulation
|
|
17
18
|
shipping(items: $items, postalCode: $postalCode, country: $country) {
|
|
18
19
|
logisticsInfo {
|
|
19
20
|
slas {
|
|
@@ -32,6 +33,7 @@ const query = gql`
|
|
|
32
33
|
address {
|
|
33
34
|
city
|
|
34
35
|
neighborhood
|
|
36
|
+
state
|
|
35
37
|
}
|
|
36
38
|
}
|
|
37
39
|
}
|
|
@@ -54,7 +56,7 @@ export const getShippingSimulation = async ({
|
|
|
54
56
|
country,
|
|
55
57
|
})
|
|
56
58
|
|
|
57
|
-
return data
|
|
59
|
+
return data
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
export default getShippingSimulation
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { ChangeEvent } from 'react'
|
|
2
2
|
import { useCallback, useEffect, useReducer } from 'react'
|
|
3
|
-
import {
|
|
3
|
+
import { ClientShippingSimulationQueryQuery } from '@generated/graphql'
|
|
4
|
+
import getShippingSimulation from '.'
|
|
5
|
+
import { useSession } from '../session'
|
|
4
6
|
|
|
5
7
|
export interface ProductShippingInfo {
|
|
6
8
|
id: string
|
|
@@ -14,14 +16,9 @@ type InputProps = {
|
|
|
14
16
|
errorMessage?: string
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
type ShippingSimulationProps = {
|
|
18
|
-
location?: string
|
|
19
|
-
options?: UIShippingSimulationProps['options']
|
|
20
|
-
}
|
|
21
|
-
|
|
22
19
|
type State = {
|
|
23
20
|
input: InputProps
|
|
24
|
-
shippingSimulation
|
|
21
|
+
shippingSimulation?: ClientShippingSimulationQueryQuery['shipping']
|
|
25
22
|
}
|
|
26
23
|
|
|
27
24
|
type Action =
|
|
@@ -41,17 +38,15 @@ type Action =
|
|
|
41
38
|
type: 'clear'
|
|
42
39
|
}
|
|
43
40
|
|
|
44
|
-
const createEmptySimulation = () =>
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
},
|
|
54
|
-
})
|
|
41
|
+
const createEmptySimulation = () =>
|
|
42
|
+
({
|
|
43
|
+
input: {
|
|
44
|
+
postalCode: '',
|
|
45
|
+
displayClearButton: false,
|
|
46
|
+
errorMessage: '',
|
|
47
|
+
},
|
|
48
|
+
shippingSimulation: undefined,
|
|
49
|
+
} as State)
|
|
55
50
|
|
|
56
51
|
const reducer = (state: State, action: Action) => {
|
|
57
52
|
const { type } = action
|
|
@@ -104,24 +99,14 @@ const reducer = (state: State, action: Action) => {
|
|
|
104
99
|
}
|
|
105
100
|
}
|
|
106
101
|
|
|
107
|
-
export
|
|
108
|
-
shippingItem: ProductShippingInfo,
|
|
109
|
-
country: string,
|
|
110
|
-
postalCode: string
|
|
111
|
-
) => Promise<[string, UIShippingSimulationProps['options']]>
|
|
112
|
-
|
|
113
|
-
export const useShippingSimulation = (
|
|
114
|
-
shippingItem: ProductShippingInfo,
|
|
115
|
-
fetchShippingSimulationFn: FetchShippingSimulation,
|
|
116
|
-
sessionPostalCode: string,
|
|
117
|
-
country: string
|
|
118
|
-
) => {
|
|
102
|
+
export const useShippingSimulation = (shippingItem: ProductShippingInfo) => {
|
|
119
103
|
const [{ input, shippingSimulation }, dispatch] = useReducer(
|
|
120
104
|
reducer,
|
|
121
105
|
null,
|
|
122
106
|
createEmptySimulation
|
|
123
107
|
)
|
|
124
108
|
|
|
109
|
+
const { country, postalCode: sessionPostalCode } = useSession()
|
|
125
110
|
const { postalCode: shippingPostalCode } = input
|
|
126
111
|
|
|
127
112
|
useEffect(() => {
|
|
@@ -131,11 +116,12 @@ export const useShippingSimulation = (
|
|
|
131
116
|
|
|
132
117
|
// Use sessionPostalCode if there is no shippingPostalCode
|
|
133
118
|
async function fetchShipping() {
|
|
134
|
-
const
|
|
135
|
-
shippingItem,
|
|
119
|
+
const data = await getShippingSimulation({
|
|
136
120
|
country,
|
|
137
|
-
sessionPostalCode ?? ''
|
|
138
|
-
|
|
121
|
+
postalCode: sessionPostalCode ?? '',
|
|
122
|
+
items: [shippingItem],
|
|
123
|
+
})
|
|
124
|
+
const shippingSimulation = data.shipping
|
|
139
125
|
|
|
140
126
|
dispatch({
|
|
141
127
|
type: 'update',
|
|
@@ -145,30 +131,22 @@ export const useShippingSimulation = (
|
|
|
145
131
|
displayClearButton: true,
|
|
146
132
|
errorMessage: '',
|
|
147
133
|
},
|
|
148
|
-
shippingSimulation
|
|
149
|
-
location,
|
|
150
|
-
options,
|
|
151
|
-
},
|
|
134
|
+
shippingSimulation,
|
|
152
135
|
},
|
|
153
136
|
})
|
|
154
137
|
}
|
|
155
138
|
|
|
156
139
|
fetchShipping()
|
|
157
|
-
}, [
|
|
158
|
-
country,
|
|
159
|
-
fetchShippingSimulationFn,
|
|
160
|
-
sessionPostalCode,
|
|
161
|
-
shippingItem,
|
|
162
|
-
shippingPostalCode,
|
|
163
|
-
])
|
|
140
|
+
}, [country, sessionPostalCode, shippingItem, shippingPostalCode])
|
|
164
141
|
|
|
165
142
|
const handleSubmit = useCallback(async () => {
|
|
166
143
|
try {
|
|
167
|
-
const
|
|
168
|
-
shippingItem,
|
|
144
|
+
const data = await getShippingSimulation({
|
|
169
145
|
country,
|
|
170
|
-
shippingPostalCode ?? ''
|
|
171
|
-
|
|
146
|
+
postalCode: shippingPostalCode ?? '',
|
|
147
|
+
items: [shippingItem],
|
|
148
|
+
})
|
|
149
|
+
const shippingSimulation = data.shipping
|
|
172
150
|
|
|
173
151
|
dispatch({
|
|
174
152
|
type: 'update',
|
|
@@ -177,10 +155,7 @@ export const useShippingSimulation = (
|
|
|
177
155
|
displayClearButton: true,
|
|
178
156
|
errorMessage: '',
|
|
179
157
|
},
|
|
180
|
-
shippingSimulation
|
|
181
|
-
location,
|
|
182
|
-
options,
|
|
183
|
-
},
|
|
158
|
+
shippingSimulation,
|
|
184
159
|
},
|
|
185
160
|
})
|
|
186
161
|
} catch (error) {
|
|
@@ -192,7 +167,7 @@ export const useShippingSimulation = (
|
|
|
192
167
|
},
|
|
193
168
|
})
|
|
194
169
|
}
|
|
195
|
-
}, [country,
|
|
170
|
+
}, [country, shippingItem, shippingPostalCode])
|
|
196
171
|
|
|
197
172
|
const handleOnInput = useCallback((e: ChangeEvent<HTMLInputElement>) => {
|
|
198
173
|
const currentValue = e.currentTarget.value
|