@graphcommerce/magento-product 8.0.6-canary.2 → 8.0.6-canary.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Change Log
2
2
 
3
+ ## 8.0.6-canary.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#2227](https://github.com/graphcommerce-org/graphcommerce/pull/2227) [`d597719`](https://github.com/graphcommerce-org/graphcommerce/commit/d597719baaabbe079660ac063fd021d871831511) - Added option to change sort order (ASC / DESC) for sort options (Name, price, position etc) on catalog and search pages.
8
+ ([@FrankHarland](https://github.com/FrankHarland))
9
+
10
+ ## 8.0.6-canary.3
11
+
3
12
  ## 8.0.6-canary.2
4
13
 
5
14
  ### Patch Changes
@@ -1,14 +1,14 @@
1
1
  import { useForm, UseFormProps, UseFormReturn } from '@graphcommerce/ecommerce-ui'
2
2
  import { useMemoObject } from '@graphcommerce/next-ui'
3
3
  import { useEventCallback } from '@mui/material'
4
+ import { useRouter } from 'next/router'
4
5
  import React, { BaseSyntheticEvent, createContext, useContext, useMemo } from 'react'
5
- import { useProductListLinkReplace } from '../../hooks/useProductListLinkReplace'
6
+ import { productListLinkFromFilter } from '../../hooks/useProductListLink'
6
7
  import { ProductListFiltersFragment } from '../ProductListFilters/ProductListFilters.gql'
7
8
  import {
8
9
  ProductFilterParams,
9
10
  ProductListParams,
10
11
  toFilterParams,
11
- toProductListParams,
12
12
  } from '../ProductListItems/filterTypes'
13
13
 
14
14
  type DataProps = {
@@ -50,11 +50,18 @@ export function ProductFiltersPro(props: FilterFormProviderProps) {
50
50
  const defaultValues = useMemoObject(toFilterParams(params))
51
51
  const form = useForm<ProductFilterParams>({ defaultValues, ...formProps })
52
52
 
53
- const push = useProductListLinkReplace({ scroll: false })
53
+ const router = useRouter()
54
+
54
55
  const submit = useEventCallback(
55
- form.handleSubmit(async (formValues) =>
56
- push({ ...toProductListParams(formValues), currentPage: 1 }),
57
- ),
56
+ form.handleSubmit(async (formValues) => {
57
+ const queryUrl = router.query.url ?? []
58
+ const comingFromURLWithoutFilters = !queryUrl.includes('q')
59
+ const path = productListLinkFromFilter({ ...formValues, currentPage: 1 })
60
+
61
+ if (router.asPath === path) return false
62
+ if (comingFromURLWithoutFilters) return router.push(path, path)
63
+ return router.replace(path, path)
64
+ }),
58
65
  )
59
66
 
60
67
  const filterFormContext: FilterFormContextProps = useMemo(
@@ -29,7 +29,7 @@ const defaultRenderer = {
29
29
  }
30
30
 
31
31
  export function ProductFiltersProAllFiltersChip(props: ProductFiltersProAllFiltersChipProps) {
32
- const { sort_fields, total_count, renderer, ...rest } = props
32
+ const { sort_fields, total_count, renderer, category, ...rest } = props
33
33
 
34
34
  const { submit, params, aggregations, appliedAggregations } = useProductFiltersPro()
35
35
  const { sort } = params
@@ -59,7 +59,11 @@ export function ProductFiltersProAllFiltersChip(props: ProductFiltersProAllFilte
59
59
  >
60
60
  {() => (
61
61
  <>
62
- <ProductFiltersProSortSection sort_fields={sort_fields} total_count={total_count} />
62
+ <ProductFiltersProSortSection
63
+ sort_fields={sort_fields}
64
+ total_count={total_count}
65
+ category={category}
66
+ />
63
67
  <ProductFiltersProLimitSection />
64
68
  <ProductFiltersProAggregations renderer={{ ...defaultRenderer, ...renderer }} />
65
69
  </>
@@ -20,11 +20,15 @@ const defaultRenderer = {
20
20
  }
21
21
 
22
22
  export function ProductFiltersProAllFiltersSidebar(props: ProductFiltersProAllFiltersSidebarProps) {
23
- const { sort_fields, total_count, renderer, sx = [] } = props
23
+ const { sort_fields, total_count, renderer, sx = [], category } = props
24
24
 
25
25
  return (
26
26
  <Box sx={[{ display: { xs: 'none', md: 'grid' } }, ...(Array.isArray(sx) ? sx : [sx])]}>
27
- <ProductFiltersProSortSection sort_fields={sort_fields} total_count={total_count} />
27
+ <ProductFiltersProSortSection
28
+ sort_fields={sort_fields}
29
+ total_count={total_count}
30
+ category={category}
31
+ />
28
32
  <ProductFiltersProLimitSection />
29
33
  <ProductFiltersProAggregations renderer={{ ...defaultRenderer, ...renderer }} />
30
34
  </Box>
@@ -1,53 +1,34 @@
1
- import { useWatch } from '@graphcommerce/ecommerce-ui'
2
- import { useQuery } from '@graphcommerce/graphql'
3
- import { StoreConfigDocument } from '@graphcommerce/magento-store'
4
1
  import {
5
2
  ActionCard,
6
3
  ActionCardListForm,
7
4
  ChipOverlayOrPopper,
8
5
  ChipOverlayOrPopperProps,
9
- filterNonNullableKeys,
10
6
  } from '@graphcommerce/next-ui'
11
7
  import { Trans } from '@lingui/react'
12
- import { useMemo } from 'react'
13
- import { ProductListSortFragment } from '../ProductListSort/ProductListSort.gql'
14
8
  import { useProductFiltersPro } from './ProductFiltersPro'
9
+ import { UseProductFiltersProSortProps, useProductFiltersProSort } from './useProductFiltersProSort'
15
10
 
16
- export type ProductListActionSortProps = ProductListSortFragment &
11
+ export type ProductListActionSortProps = UseProductFiltersProSortProps &
17
12
  Omit<
18
13
  ChipOverlayOrPopperProps,
19
14
  'label' | 'selected' | 'selectedLabel' | 'onApply' | 'onReset' | 'onClose' | 'children'
20
15
  >
21
16
 
22
17
  export function ProductFiltersProSortChip(props: ProductListActionSortProps) {
23
- const { sort_fields, chipProps, ...rest } = props
24
- const { params, form, submit } = useProductFiltersPro()
25
- const { control } = form
26
- const activeSort = useWatch({ control, name: 'sort' })
27
-
28
- const { data: storeConfigQuery } = useQuery(StoreConfigDocument)
29
- const defaultSort = storeConfigQuery?.storeConfig?.catalog_default_sort_by
30
-
31
- const options = useMemo(
32
- () =>
33
- filterNonNullableKeys(sort_fields?.options, ['value', 'label']).map((option) => ({
34
- ...option,
35
- value: option.value === defaultSort ? null : option.value,
36
- title: option.label,
37
- })),
38
- [defaultSort, sort_fields?.options],
39
- )
18
+ const { sort_fields, chipProps, category, ...rest } = props
19
+ const { submit, form } = useProductFiltersPro()
20
+ const { options, showReset, selected, selectedLabel } = useProductFiltersProSort(props)
40
21
 
41
22
  return (
42
23
  <ChipOverlayOrPopper
43
24
  {...rest}
44
25
  overlayProps={{ sizeSm: 'minimal', sizeMd: 'minimal', ...rest.overlayProps }}
45
26
  label={<Trans id='Sort By' />}
46
- selected={Boolean(params.sort)}
47
- selectedLabel={options.find((option) => option.value === params.sort)?.label}
27
+ selected={selected}
28
+ selectedLabel={selectedLabel}
48
29
  onApply={submit}
49
30
  onReset={
50
- activeSort
31
+ showReset
51
32
  ? () => {
52
33
  form.setValue('sort', null)
53
34
  form.setValue('dir', null)
@@ -60,7 +41,7 @@ export function ProductFiltersProSortChip(props: ProductListActionSortProps) {
60
41
  >
61
42
  {() => (
62
43
  <ActionCardListForm
63
- control={control}
44
+ control={form.control}
64
45
  name='sort'
65
46
  layout='list'
66
47
  variant='default'
@@ -0,0 +1,17 @@
1
+ import { SortEnum } from '@graphcommerce/graphql-mesh'
2
+ import { IconSvg } from '@graphcommerce/next-ui'
3
+ import * as IconArrowDown from '@graphcommerce/next-ui/icons/arrow-down.svg'
4
+ import * as IconArrowUp from '@graphcommerce/next-ui/icons/arrow-up.svg'
5
+
6
+ type Props = {
7
+ sortDirection: SortEnum | null
8
+ }
9
+
10
+ export function ProductFiltersProSortDirectionArrow({ sortDirection }: Props) {
11
+ return (
12
+ <IconSvg
13
+ src={sortDirection === 'ASC' || sortDirection === null ? IconArrowUp : IconArrowDown}
14
+ sx={{ display: 'flex' }}
15
+ />
16
+ )
17
+ }
@@ -1,46 +1,21 @@
1
- import { useWatch } from '@graphcommerce/ecommerce-ui'
2
- import { useQuery } from '@graphcommerce/graphql'
3
- import { StoreConfigDocument } from '@graphcommerce/magento-store'
4
- import {
5
- ActionCard,
6
- ActionCardAccordion,
7
- ActionCardListForm,
8
- Button,
9
- filterNonNullableKeys,
10
- } from '@graphcommerce/next-ui'
1
+ import { ActionCard, ActionCardAccordion, ActionCardListForm, Button } from '@graphcommerce/next-ui'
11
2
  import { Trans } from '@lingui/react'
12
- import { useMemo } from 'react'
13
- import { ProductListSortFragment } from '../ProductListSort/ProductListSort.gql'
14
3
  import { useProductFiltersPro } from './ProductFiltersPro'
4
+ import { UseProductFiltersProSortProps, useProductFiltersProSort } from './useProductFiltersProSort'
15
5
 
16
- export type ProductFiltersProSortSectionProps = ProductListSortFragment
6
+ export type ProductFiltersProSortSectionProps = UseProductFiltersProSortProps
17
7
 
18
8
  export function ProductFiltersProSortSection(props: ProductFiltersProSortSectionProps) {
19
- const { sort_fields } = props
20
9
  const { form } = useProductFiltersPro()
21
- const { control } = form
22
- const activeSort = useWatch({ control, name: 'sort' })
23
-
24
- const { data: storeConfigQuery } = useQuery(StoreConfigDocument)
25
- const defaultSort = storeConfigQuery?.storeConfig?.catalog_default_sort_by
26
-
27
- const options = useMemo(
28
- () =>
29
- filterNonNullableKeys(sort_fields?.options, ['value', 'label']).map((option) => ({
30
- ...option,
31
- value: option.value === defaultSort ? null : option.value,
32
- title: option.label,
33
- })),
34
- [defaultSort, sort_fields?.options],
35
- )
10
+ const { options, showReset, selected } = useProductFiltersProSort(props)
36
11
 
37
12
  return (
38
13
  <ActionCardAccordion
39
- defaultExpanded={!!activeSort}
14
+ defaultExpanded={selected}
40
15
  summary={<Trans id='Sort By' />}
41
16
  details={
42
17
  <ActionCardListForm
43
- control={control}
18
+ control={form.control}
44
19
  name='sort'
45
20
  layout='list'
46
21
  variant='default'
@@ -50,7 +25,7 @@ export function ProductFiltersProSortSection(props: ProductFiltersProSortSection
50
25
  />
51
26
  }
52
27
  right={
53
- activeSort ? (
28
+ showReset ? (
54
29
  <Button
55
30
  color='primary'
56
31
  onClick={(e) => {
@@ -0,0 +1,74 @@
1
+ import { useWatch } from '@graphcommerce/ecommerce-ui'
2
+ import { useQuery } from '@graphcommerce/graphql'
3
+ import { StoreConfigDocument } from '@graphcommerce/magento-store'
4
+ import { filterNonNullableKeys } from '@graphcommerce/next-ui'
5
+ import { i18n } from '@lingui/core'
6
+ import { useMemo } from 'react'
7
+ import { CategoryDefaultFragment } from '../ProductListItems/CategoryDefault.gql'
8
+ import { ProductFilterParams } from '../ProductListItems/filterTypes'
9
+ import { ProductListSortFragment } from '../ProductListSort'
10
+ import { useProductFiltersPro } from './ProductFiltersPro'
11
+ import type { ProductListActionSortProps } from './ProductFiltersProSortChip'
12
+ import { ProductFiltersProSortDirectionArrow } from './ProductFiltersProSortDirectionArrow'
13
+
14
+ const exclude = ['relevance', 'position']
15
+
16
+ export type UseProductFiltersProSortProps = ProductListSortFragment & {
17
+ category?: CategoryDefaultFragment
18
+ }
19
+
20
+ export function useProductFiltersProSort(props: ProductListActionSortProps) {
21
+ const { sort_fields, category } = props
22
+
23
+ const { params, form } = useProductFiltersPro()
24
+ const { control, setValue } = form
25
+
26
+ const sortFields = useMemo(
27
+ () =>
28
+ filterNonNullableKeys(sort_fields?.options).map((o) =>
29
+ !category?.uid && o.value === 'position'
30
+ ? { value: 'relevance', label: i18n._('Relevance') }
31
+ : o,
32
+ ),
33
+ [category?.uid, sort_fields?.options],
34
+ )
35
+ const availableSortBy = category?.available_sort_by ?? sortFields.map((o) => o.value)
36
+
37
+ const conf = useQuery(StoreConfigDocument).data?.storeConfig
38
+ const defaultSortBy = (
39
+ category ? category.default_sort_by ?? conf?.catalog_default_sort_by ?? 'position' : 'relevance'
40
+ ) as ProductFilterParams['sort']
41
+
42
+ const formSort = useWatch({ control, name: 'sort' })
43
+ const formDirection = useWatch({ control, name: 'dir' })
44
+ const showReset = Boolean(formSort !== defaultSortBy || formDirection === 'DESC')
45
+ const selected = Boolean(params.sort && (params.sort !== defaultSortBy || params.dir === 'DESC'))
46
+
47
+ const options = useMemo(
48
+ () =>
49
+ sortFields
50
+ .filter((o) => availableSortBy.includes(o.value))
51
+ .map((option) => {
52
+ const value = option.value === defaultSortBy ? null : option.value
53
+ const showSort = formSort === value && !exclude.includes(option.value)
54
+
55
+ return {
56
+ ...option,
57
+ value,
58
+ title: option.label,
59
+ ...(showSort && {
60
+ onClick: () => setValue('dir', formDirection === 'DESC' ? null : 'DESC'),
61
+ price: <ProductFiltersProSortDirectionArrow sortDirection={formDirection} />,
62
+ }),
63
+ }
64
+ }),
65
+ [sortFields, availableSortBy, defaultSortBy, formSort, formDirection, setValue],
66
+ )
67
+
68
+ return {
69
+ options,
70
+ selected,
71
+ showReset,
72
+ selectedLabel: options.find((option) => option.value === params.sort)?.label,
73
+ }
74
+ }
@@ -0,0 +1,5 @@
1
+ fragment CategoryDefault on CategoryInterface {
2
+ uid
3
+ default_sort_by
4
+ available_sort_by
5
+ }
@@ -31,7 +31,7 @@ export type ProductFilterParams = {
31
31
 
32
32
  export function toFilterParams(params: ProductListParams): ProductFilterParams {
33
33
  const [sortKey] = Object.keys(params.sort) as [keyof ProductAttributeSortInput]
34
- const dir = params.sort[sortKey] as SortEnum | undefined
34
+ const dir = params.sort[sortKey]?.toUpperCase() as SortEnum | undefined
35
35
 
36
36
  return {
37
37
  ...params,
@@ -35,7 +35,7 @@ export function parseParams(
35
35
  }
36
36
  if (param === 'dir') {
37
37
  const [sortBy] = Object.keys(categoryVariables.sort)
38
- if (sortBy) categoryVariables.sort[sortBy] = value as SortEnum
38
+ if (sortBy) categoryVariables.sort[sortBy] = value?.toUpperCase() as SortEnum
39
39
  return undefined
40
40
  }
41
41
 
@@ -0,0 +1,28 @@
1
+ import { StoreConfigQuery } from '@graphcommerce/magento-store'
2
+ import { CategoryDefaultFragment } from './CategoryDefault.gql'
3
+ import { ProductListParams } from './filterTypes'
4
+ import { cloneDeep } from '@graphcommerce/graphql'
5
+
6
+ export async function productListApplyCategoryDefaults(
7
+ params: ProductListParams | undefined,
8
+ conf: StoreConfigQuery,
9
+ category: Promise<CategoryDefaultFragment | null | undefined>,
10
+ ) {
11
+ if (!params) return params
12
+
13
+ const newParams = cloneDeep(params)
14
+ if (!newParams.pageSize) newParams.pageSize = conf.storeConfig?.grid_per_page ?? 12
15
+
16
+ if (Object.keys(params.sort).length === 0) {
17
+ const categorySort = (await category)?.default_sort_by as keyof ProductListParams['sort']
18
+ const defaultSort = conf.storeConfig?.catalog_default_sort_by as keyof ProductListParams['sort']
19
+ if (categorySort) newParams.sort = { [categorySort]: 'ASC' }
20
+ else if (defaultSort) newParams.sort = { [defaultSort]: 'ASC' }
21
+ }
22
+
23
+ if (!newParams.filters.category_uid?.in?.[0]) {
24
+ newParams.filters.category_uid = { eq: (await category)?.uid }
25
+ }
26
+
27
+ return newParams
28
+ }
@@ -13,6 +13,8 @@ export * from './ProductListItems/filterTypes'
13
13
  export * from './ProductListItems/getFilterTypes'
14
14
  export * from './ProductListItems/ProductListItems.gql'
15
15
  export * from './ProductListItems/ProductListItemsBase'
16
+ export * from './ProductListItems/productListApplyCategoryDefaults'
17
+ export * from './ProductListItems/CategoryDefault.gql'
16
18
  export * from './ProductListItems/ProductListParamsProvider'
17
19
  export * from './ProductListItems/renderer'
18
20
  export * from './ProductListLink/ProductListLink'
@@ -2,11 +2,13 @@ import {
2
2
  isFilterTypeEqual,
3
3
  isFilterTypeMatch,
4
4
  isFilterTypeRange,
5
+ ProductFilterParams,
5
6
  ProductListParams,
7
+ toFilterParams,
6
8
  } from '../components/ProductListItems/filterTypes'
7
9
 
8
- export function productListLink(props: ProductListParams): string {
9
- const { url, sort, currentPage, pageSize, filters: incoming } = props
10
+ export function productListLinkFromFilter(props: ProductFilterParams): string {
11
+ const { url, sort, dir, currentPage, pageSize, filters: incoming } = props
10
12
  const isSearch = url.startsWith('search')
11
13
  const filters = isSearch ? incoming : { ...incoming, category_uid: undefined }
12
14
  const uid = incoming?.category_uid?.eq || incoming?.category_uid?.in?.[0]
@@ -19,9 +21,8 @@ export function productListLink(props: ProductListParams): string {
19
21
 
20
22
  // todo(paales): How should the URL look like with multiple sorts?
21
23
  // Something like: /sort/position,price/dir/asc,asc
22
- const [sortBy] = Object.keys(sort)
23
- if (sort && sortBy) query += `/sort/${sortBy}`
24
- if (sort && sortBy && sort[sortBy] && sort[sortBy] === 'DESC') query += `/dir/desc`
24
+ if (sort) query += `/sort/${sort}`
25
+ if (dir) query += `/dir/desc`
25
26
  if (pageSize) query += `/page-size/${pageSize}`
26
27
 
27
28
  // Apply filters
@@ -42,6 +43,10 @@ export function productListLink(props: ProductListParams): string {
42
43
  return query ? `/${url}${paginateSort}/q${query}` : `/${url}${paginateSort}`
43
44
  }
44
45
 
46
+ export function productListLink(props: ProductListParams): string {
47
+ return productListLinkFromFilter(toFilterParams(props))
48
+ }
49
+
45
50
  export function useProductListLink(props: ProductListParams): string {
46
51
  return productListLink({ ...props, url: `${props.url}` })
47
52
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/magento-product",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "8.0.6-canary.2",
5
+ "version": "8.0.6-canary.4",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -18,18 +18,18 @@
18
18
  "typescript": "5.3.3"
19
19
  },
20
20
  "peerDependencies": {
21
- "@graphcommerce/ecommerce-ui": "^8.0.6-canary.2",
22
- "@graphcommerce/eslint-config-pwa": "^8.0.6-canary.2",
23
- "@graphcommerce/framer-next-pages": "^8.0.6-canary.2",
24
- "@graphcommerce/framer-scroller": "^8.0.6-canary.2",
25
- "@graphcommerce/graphql": "^8.0.6-canary.2",
26
- "@graphcommerce/graphql-mesh": "^8.0.6-canary.2",
27
- "@graphcommerce/image": "^8.0.6-canary.2",
28
- "@graphcommerce/magento-cart": "^8.0.6-canary.2",
29
- "@graphcommerce/magento-store": "^8.0.6-canary.2",
30
- "@graphcommerce/next-ui": "^8.0.6-canary.2",
31
- "@graphcommerce/prettier-config-pwa": "^8.0.6-canary.2",
32
- "@graphcommerce/typescript-config-pwa": "^8.0.6-canary.2",
21
+ "@graphcommerce/ecommerce-ui": "^8.0.6-canary.4",
22
+ "@graphcommerce/eslint-config-pwa": "^8.0.6-canary.4",
23
+ "@graphcommerce/framer-next-pages": "^8.0.6-canary.4",
24
+ "@graphcommerce/framer-scroller": "^8.0.6-canary.4",
25
+ "@graphcommerce/graphql": "^8.0.6-canary.4",
26
+ "@graphcommerce/graphql-mesh": "^8.0.6-canary.4",
27
+ "@graphcommerce/image": "^8.0.6-canary.4",
28
+ "@graphcommerce/magento-cart": "^8.0.6-canary.4",
29
+ "@graphcommerce/magento-store": "^8.0.6-canary.4",
30
+ "@graphcommerce/next-ui": "^8.0.6-canary.4",
31
+ "@graphcommerce/prettier-config-pwa": "^8.0.6-canary.4",
32
+ "@graphcommerce/typescript-config-pwa": "^8.0.6-canary.4",
33
33
  "@lingui/core": "^4.2.1",
34
34
  "@lingui/macro": "^4.2.1",
35
35
  "@lingui/react": "^4.2.1",