Skip to content

Commit

Permalink
fix: Circular dependency (#1333)
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonlaurentino authored May 30, 2022
1 parent a717807 commit d38941a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 72 deletions.
9 changes: 4 additions & 5 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,15 @@ export { sendAnalyticsEvent } from './analytics/sendAnalyticsEvent'
export { useAnalyticsEvent } from './analytics/useAnalyticsEvent'

// Faceted Search
export {
parse as parseSearchState,
format as formatSearchState,
} from './search/serializer'
export { parse as parseSearchState } from './search/serializer'

export { default as formatSearchState } from './utils/format'

export { initialize as initSearchState } from './search/useSearchState'
export { Provider as SearchProvider } from './search/Provider'
export { useSearch } from './search/useSearch'
export { usePagination } from './search/usePagination'
export type { State as SearchState } from './search/useSearchState'
export type { State as SearchState } from './types'

// UI
export { Provider as UIProvider, Context as UIContext } from './ui/Provider'
Expand Down
7 changes: 4 additions & 3 deletions packages/sdk/src/search/Provider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { createContext, useMemo } from 'react'
import type { FC } from 'react'
import React, { createContext, useMemo } from 'react'

import type { State as SearchState } from '../types'
import type { UseSearchInfiniteState } from './useInfiniteSearchState'
import { useSearchInfiniteState } from './useInfiniteSearchState'
import type { UseSearchState } from './useSearchState'
import { useSearchState } from './useSearchState'
import type { UseSearchInfiniteState } from './useInfiniteSearchState'
import type { State as SearchState, UseSearchState } from './useSearchState'

export interface SearchContext extends UseSearchInfiniteState, UseSearchState {
itemsPerPage: number
Expand Down
29 changes: 2 additions & 27 deletions packages/sdk/src/search/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
import type { SearchSort, State } from '../types'
import { initialize, reducer } from './useSearchState'
import type { State as SearchState, SearchSort } from './useSearchState'

export const format = (params: SearchState): URL => {
const url = new URL(params.base, 'http://localhost')
const { page, selectedFacets, sort, term } = params

if (term !== null) {
url.searchParams.set('q', term)
}

const facets = new Set<string>()

for (const facet of selectedFacets) {
url.searchParams.append(facet.key, facet.value)
facets.add(facet.key)
}

if (selectedFacets.length > 0) {
url.searchParams.set('facets', Array.from(facets).join(','))
}

url.searchParams.set('sort', sort)
url.searchParams.set('page', page.toString())

return url
}

export const parse = ({ pathname, searchParams }: URL): SearchState => {
export const parse = ({ pathname, searchParams }: URL): State => {
let state = initialize({
base: pathname,
term: searchParams.get('q') ?? null,
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/search/usePagination.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useMemo } from 'react'

import { format } from './serializer'
import type { State as SearchState } from '../types'
import format from '../utils/format'
import { useSearch } from './useSearch'
import type { State as SearchState } from './useSearchState'

const getLink = (state: SearchState) => {
const { pathname, search } = format(state)
Expand Down
37 changes: 2 additions & 35 deletions packages/sdk/src/search/useSearchState.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,8 @@
import { useMemo } from 'react'

import { format } from './serializer'
import type { Facet, SearchSort, State } from '../types'
import { SDKError } from '../utils/error'

export type SearchSort =
| 'price_desc'
| 'price_asc'
| 'orders_desc'
| 'name_desc'
| 'name_asc'
| 'release_desc'
| 'discount_desc'
| 'score_desc'

export interface Facet {
key: string
value: string
}

export interface State {
/** @description search sorting criteria */
sort: SearchSort
/**
* @description selected facets
* */
selectedFacets: Facet[]
/** @description full text term */
term: string | null
/**
* @description the base path url for the search context
* */
base: string
/**
* @description current pagination cursor
*/
page: number
}
import format from '../utils/format'

const sortKeys = new Set<SearchSort>([
'price_desc',
Expand Down
33 changes: 33 additions & 0 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export type SearchSort =
| 'price_desc'
| 'price_asc'
| 'orders_desc'
| 'name_desc'
| 'name_asc'
| 'release_desc'
| 'discount_desc'
| 'score_desc'

export interface Facet {
key: string
value: string
}

export interface State {
/** @description search sorting criteria */
sort: SearchSort
/**
* @description selected facets
* */
selectedFacets: Facet[]
/** @description full text term */
term: string | null
/**
* @description the base path url for the search context
* */
base: string
/**
* @description current pagination cursor
*/
page: number
}
28 changes: 28 additions & 0 deletions packages/sdk/src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { State } from '../types'

const format = (params: State): URL => {
const url = new URL(params.base, 'http://localhost')
const { page, selectedFacets, sort, term } = params

if (term !== null) {
url.searchParams.set('q', term)
}

const facets = new Set<string>()

for (const facet of selectedFacets) {
url.searchParams.append(facet.key, facet.value)
facets.add(facet.key)
}

if (selectedFacets.length > 0) {
url.searchParams.set('facets', Array.from(facets).join(','))
}

url.searchParams.set('sort', sort)
url.searchParams.set('page', page.toString())

return url
}

export default format

1 comment on commit d38941a

@vercel
Copy link

@vercel vercel bot commented on d38941a May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.