Skip to content

Commit

Permalink
chore: Refactors to update selectedFacets
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofanny committed Jan 24, 2024
1 parent de47497 commit d2e91f6
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions packages/core/src/sdk/product/useProductGalleryQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sendAnalyticsEvent } from '@faststore/sdk'

import { useSearch, setFacet } from '@faststore/sdk'
import { useSearch } from '@faststore/sdk'
import { gql } from '@faststore/graphql-utils'

import { useQuery } from 'src/sdk/graphql/useQuery'
Expand Down Expand Up @@ -72,40 +72,30 @@ export const useProductGalleryQuery = ({
selectedFacets,
})

const findAndSetFacetValue = (
const findFacetValue = (
facets: Facet[],
key: string,
newValue: string
) => {
const existingFacetValue = facets?.find(
(facet: Facet) => facet.key === key
)?.value

if (!existingFacetValue) {
setState({
...state,
selectedFacets: setFacet(
state.selectedFacets,
{ key, value: newValue },
true
),
})
}
searchParam: string
): string | null => {
const facet = facets.find(({ key }) => key === searchParam)
return facet?.value ?? null
}

return useQuery<Query, Variables>(query, localizedVariables, {
onSuccess: (data) => {
if (data && term) {
findAndSetFacetValue(
selectedFacets,
'fuzzy',
data.search.metadata?.fuzzy
)
findAndSetFacetValue(
selectedFacets,
'operator',
data.search.metadata?.logicalOperator
)
const fuzzyFacetValue = findFacetValue(selectedFacets, 'fuzzy')
const operatorFacetValue = findFacetValue(selectedFacets, 'operator')

if (!fuzzyFacetValue && !operatorFacetValue) {
setState({
...state,
selectedFacets: [
...selectedFacets,
{ key: 'fuzzy', value: data.search.metadata?.fuzzy },
{ key: 'operator', value: data.search.metadata?.logicalOperator },
],
})
}

sendAnalyticsEvent<IntelligentSearchQueryEvent>({
name: 'intelligent_search_query',
Expand Down

0 comments on commit d2e91f6

Please sign in to comment.