@faststore/core 2.2.0-alpha.8 → 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.
@@ -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-bed5a9784a3af4e8.js 2.25 kB
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)
@@ -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
@@ -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 (8.171s)
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 (7ms)
8
- ✓ should return a valid GraphQL schema contain all expected types (10ms)
9
- ✓ should return a valid GraphQL schema contain all expected queries (2ms)
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
12
  ✓ getTypeDefsFromFolder function should return an Array (8ms)
13
13
  Third Party API Extension
14
- ✓ getTypeDefsFromFolder function should return an Array (5ms)
14
+ ✓ getTypeDefsFromFolder function should return an Array (6ms)
15
15
  Final Schema after merging
16
- ✓ should return a valid merged GraphQL schema (32ms)
16
+ ✓ should return a valid merged GraphQL schema (35ms)
17
17
  Envelop
18
- ✓ should exist with its plugins (33ms)
19
- ✓ should handle options and execute (269ms)
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: 8.848s
24
+ Time: 11.351s
25
25
  Ran all test suites.
@@ -1160,6 +1160,18 @@ export type ClientProductGalleryFragment = {
1160
1160
  search: { products: { pageInfo: { totalCount: number } } }
1161
1161
  }
1162
1162
 
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 }> } }
1173
+ }
1174
+
1163
1175
  export type ServerCollectionPageFragment = { collection: { id: string } }
1164
1176
 
1165
1177
  export type ServerProductFragment = { product: { id: string } }
@@ -1451,12 +1463,12 @@ export type ClientProductQueryQuery = {
1451
1463
  }
1452
1464
  }
1453
1465
 
1454
- export type SearchSuggestionsQueryQueryVariables = Exact<{
1466
+ export type ClientSearchSuggestionsQueryQueryVariables = Exact<{
1455
1467
  term: Scalars['String']
1456
1468
  selectedFacets: InputMaybe<Array<IStoreSelectedFacet> | IStoreSelectedFacet>
1457
1469
  }>
1458
1470
 
1459
- export type SearchSuggestionsQueryQuery = {
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 TopSearchSuggestionsQueryQueryVariables = Exact<{
1501
+ export type ClientTopSearchSuggestionsQueryQueryVariables = Exact<{
1490
1502
  term: Scalars['String']
1491
1503
  selectedFacets: InputMaybe<Array<IStoreSelectedFacet> | IStoreSelectedFacet>
1492
1504
  }>
1493
1505
 
1494
- export type TopSearchSuggestionsQueryQuery = {
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 ShippingSimulationQueryQueryVariables = Exact<{
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 ShippingSimulationQueryQuery = {
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: { city: string | null; neighborhood: string | null } | null
1560
+ address: {
1561
+ city: string | null
1562
+ neighborhood: string | null
1563
+ state: string | null
1564
+ } | null
1549
1565
  } | null
1550
1566
  }
@@ -6,8 +6,8 @@
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
- "SearchSuggestionsQuery": "query SearchSuggestionsQuery($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
- "TopSearchSuggestionsQuery": "query TopSearchSuggestionsQuery($term: String!, $selectedFacets: [IStoreSelectedFacet!]) {\n search(first: 5, term: $term, selectedFacets: $selectedFacets) {\n suggestions {\n terms {\n value\n }\n }\n }\n}\n",
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
- "ShippingSimulationQuery": "query ShippingSimulationQuery($postalCode: String!, $country: String!, $items: [IShippingItem!]!) {\n shipping(items: $items, postalCode: $postalCode, country: $country) {\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 address {\n city\n neighborhood\n }\n }\n}\n"
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.8",
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.4",
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.4",
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": "a1d5951123d0b6018c7d6e07e610e35ea8cdd7a1"
132
+ "gitHead": "79b9eb42f6f8fb7fdead1141f384c7b826357fa3"
133
133
  }
@@ -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 { terms, products, isLoading } = useSuggestions(searchQueryDeferred)
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 { terms } = useTopSearch(topTerms)
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,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 { location, options } = shippingSimulation
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}
@@ -0,0 +1,13 @@
1
+ import { gql } from '@faststore/graphql-utils'
2
+
3
+ export const fragment = gql`
4
+ fragment ClientSearchSuggestions on Query {
5
+ search(first: 5, term: $term, selectedFacets: $selectedFacets) {
6
+ suggestions {
7
+ terms {
8
+ value
9
+ }
10
+ }
11
+ }
12
+ }
13
+ `
@@ -0,0 +1,11 @@
1
+ import { gql } from '@faststore/graphql-utils'
2
+
3
+ export const fragment = gql`
4
+ fragment ClientShippingSimulation on Query {
5
+ shipping(items: $items, postalCode: $postalCode, country: $country) {
6
+ address {
7
+ city
8
+ }
9
+ }
10
+ }
11
+ `
@@ -0,0 +1,13 @@
1
+ import { gql } from '@faststore/graphql-utils'
2
+
3
+ export const fragment = gql`
4
+ fragment ClientTopSearchSuggestions on Query {
5
+ search(first: 5, term: $term, selectedFacets: $selectedFacets) {
6
+ suggestions {
7
+ terms {
8
+ value
9
+ }
10
+ }
11
+ }
12
+ }
13
+ `
@@ -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
- SearchSuggestionsQueryQuery as Query,
9
- SearchSuggestionsQueryQueryVariables as Variables,
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 SearchSuggestionsQuery(
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, limit: number = MAX_SUGGESTIONS) {
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
- terms: (data?.search.suggestions.terms ?? []).slice(0, limit),
78
- products: (data?.search.suggestions.products ?? []).slice(0, limit),
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
- StoreSuggestionTerm,
6
- SearchSuggestionsQueryQuery as Query,
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 TopSearchSuggestionsQuery(
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
- terms: (data?.search.suggestions.terms ?? initialTerms).slice(0, limit),
45
- isLoading: !error && !data,
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
- ShippingSimulationQueryQuery as Query,
6
- ShippingSimulationQueryQueryVariables as Variables,
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 ShippingSimulationQuery(
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.shipping
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 { ShippingSimulationProps as UIShippingSimulationProps } from '@faststore/components'
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: ShippingSimulationProps
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
- input: {
46
- postalCode: '',
47
- displayClearButton: false,
48
- errorMessage: '',
49
- },
50
- shippingSimulation: {
51
- location: '',
52
- options: [],
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 type FetchShippingSimulation = (
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 [location, options] = await fetchShippingSimulationFn(
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 [location, options] = await fetchShippingSimulationFn(
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, fetchShippingSimulationFn, shippingItem, shippingPostalCode])
170
+ }, [country, shippingItem, shippingPostalCode])
196
171
 
197
172
  const handleOnInput = useCallback((e: ChangeEvent<HTMLInputElement>) => {
198
173
  const currentValue = e.currentTarget.value