@graphcommerce/magento-product 8.1.0-canary.2 → 8.1.0-canary.20

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.
Files changed (43) hide show
  1. package/CHANGELOG.md +131 -1
  2. package/components/AddProductsToCart/AddProductsToCartButton.tsx +1 -0
  3. package/components/AddProductsToCart/AddProductsToCartForm.tsx +7 -2
  4. package/components/AddProductsToCart/AddProductsToCartSnackbar.tsx +13 -14
  5. package/components/AddProductsToCart/findAddedItems.ts +81 -0
  6. package/components/AddProductsToCart/index.ts +3 -0
  7. package/components/AddProductsToCart/useAddProductsToCartAction.ts +6 -3
  8. package/components/AddProductsToCart/useFormAddProductsToCart.ts +1 -2
  9. package/components/JsonLdProduct/JsonLdProductOffer.graphql +2 -3
  10. package/components/JsonLdProduct/ProductPageJsonLd.tsx +9 -4
  11. package/components/JsonLdProduct/index.ts +1 -0
  12. package/components/ProductAddToCart/ProductAddToCart.tsx +6 -4
  13. package/components/ProductCustomizable/CustomizableAreaOption.tsx +41 -7
  14. package/components/ProductCustomizable/CustomizableDateOption.tsx +60 -7
  15. package/components/ProductCustomizable/CustomizableDropDownOption.tsx +63 -15
  16. package/components/ProductCustomizable/CustomizableFieldOption.tsx +40 -4
  17. package/components/ProductCustomizable/index.ts +1 -0
  18. package/components/ProductCustomizable/productCustomizableSelectors.ts +59 -0
  19. package/components/ProductFiltersPro/ProductFiltersPro.tsx +25 -10
  20. package/components/ProductFiltersPro/ProductFiltersProAllFiltersChip.tsx +6 -2
  21. package/components/ProductFiltersPro/ProductFiltersProAllFiltersSidebar.tsx +6 -2
  22. package/components/ProductFiltersPro/ProductFiltersProSortChip.tsx +9 -28
  23. package/components/ProductFiltersPro/ProductFiltersProSortDirectionArrow.tsx +15 -0
  24. package/components/ProductFiltersPro/ProductFiltersProSortSection.tsx +7 -32
  25. package/components/ProductFiltersPro/useProductFiltersProSort.tsx +74 -0
  26. package/components/ProductListItems/CategoryDefault.graphql +5 -0
  27. package/components/ProductListItems/ProductListItemsBase.tsx +1 -1
  28. package/components/ProductListItems/filterTypes.tsx +1 -1
  29. package/components/ProductListItems/filteredProductList.tsx +1 -1
  30. package/components/ProductListItems/productListApplyCategoryDefaults.ts +28 -0
  31. package/components/ProductPageBreadcrumb/ProductPageBreadcrumb.tsx +5 -3
  32. package/components/ProductPageMeta/ProductPageMeta.graphql +1 -1
  33. package/components/ProductPagePrice/ProductPagePrice.graphql +3 -0
  34. package/components/ProductPagePrice/ProductPagePrice.tsx +11 -4
  35. package/components/ProductPagePrice/useCustomizableOptionPrice.ts +85 -0
  36. package/components/ProductShortDescription/ProductShortDescription.tsx +2 -0
  37. package/components/ProductStaticPaths/getProductStaticPaths.ts +2 -3
  38. package/components/ProductStaticPaths/getSitemapPaths.ts +3 -0
  39. package/components/index.ts +2 -0
  40. package/hooks/useProductListLink.ts +10 -5
  41. package/hooks/useProductListLinkReplace.ts +3 -0
  42. package/package.json +13 -13
  43. package/tsconfig.json +1 -1
@@ -1,33 +1,81 @@
1
- import { SelectElement } from '@graphcommerce/ecommerce-ui'
1
+ import { SelectElement, useController } from '@graphcommerce/ecommerce-ui'
2
2
  import { SectionHeader, filterNonNullableKeys } from '@graphcommerce/next-ui'
3
- import { Box } from '@mui/material'
3
+ import { Box, ListItemText, MenuItem, TextField, Typography } from '@mui/material'
4
4
  import { useFormAddProductsToCart } from '../AddProductsToCart'
5
5
  import { OptionTypeRenderer } from './CustomizableAreaOption'
6
+ import { Money } from '@graphcommerce/magento-store'
6
7
 
7
8
  type CustomizableDropDownOptionProps = React.ComponentProps<
8
9
  OptionTypeRenderer['CustomizableDropDownOption']
9
10
  >
10
11
 
11
12
  export function CustomizableDropDownOption(props: CustomizableDropDownOptionProps) {
12
- const { uid, required, index, title, dropdownValue } = props
13
- const { control } = useFormAddProductsToCart()
13
+ const { uid, required, index, title, dropdownValue, productPrice, currency } = props
14
+ const { control, getValues } = useFormAddProductsToCart()
15
+
16
+ const {
17
+ field: { onChange, value, ref, ...field },
18
+ fieldState: { invalid, error },
19
+ } = useController({
20
+ name: `cartItems.${index}.customizable_options.${uid}`,
21
+ rules: {
22
+ required: Boolean(required),
23
+ },
24
+ control,
25
+ defaultValue: '',
26
+ })
14
27
 
15
28
  return (
16
29
  <Box>
17
30
  <SectionHeader labelLeft={title} sx={{ mt: 0 }} />
18
- <SelectElement
19
- sx={{ width: '100%' }}
31
+
32
+ <TextField
33
+ sx={{
34
+ width: '100%',
35
+ '& .MuiSelect-select': {
36
+ display: 'flex',
37
+ justifyContent: 'space-between',
38
+ alignItems: 'center',
39
+ },
40
+ }}
20
41
  color='primary'
21
- control={control}
22
- name={`cartItems.${index}.customizable_options.${uid}`}
23
- label={title}
42
+ value={value ?? ''}
43
+ {...field}
44
+ inputRef={ref}
45
+ onChange={(event) => onChange(event.target.value)}
46
+ select
24
47
  required={Boolean(required)}
25
- defaultValue=''
26
- options={filterNonNullableKeys(dropdownValue, ['title']).map((option) => ({
27
- id: option.uid,
28
- label: option.title,
29
- }))}
30
- />
48
+ error={invalid}
49
+ helperText={error?.message}
50
+ >
51
+ {filterNonNullableKeys(dropdownValue, ['title']).map((option) => (
52
+ <MenuItem key={option.uid} value={option.uid}>
53
+ <Box>{option.title}</Box>
54
+
55
+ {option.price ? (
56
+ <Box
57
+ sx={{
58
+ // display: 'flex',
59
+ typography: 'body1',
60
+ '&.sizeMedium': { typographty: 'subtitle1' },
61
+ '&.sizeLarge': { typography: 'h6' },
62
+ color: option.uid === value ? 'text.primary' : 'text.secondary',
63
+ }}
64
+ >
65
+ <span style={{ fontFamily: 'arial', paddingTop: '1px' }}>+&nbsp;</span>
66
+ <Money
67
+ value={
68
+ option.price_type === 'PERCENT'
69
+ ? productPrice * (option.price / 100)
70
+ : option.price
71
+ }
72
+ currency={currency}
73
+ />
74
+ </Box>
75
+ ) : null}
76
+ </MenuItem>
77
+ ))}
78
+ </TextField>
31
79
  </Box>
32
80
  )
33
81
  }
@@ -1,3 +1,4 @@
1
+ import { Money } from '@graphcommerce/magento-store'
1
2
  import { TextFieldElement } from '@graphcommerce/ecommerce-ui'
2
3
  import { SectionHeader } from '@graphcommerce/next-ui'
3
4
  import { i18n } from '@lingui/core'
@@ -10,10 +11,12 @@ type CustomizableFieldOptionProps = React.ComponentProps<
10
11
  >
11
12
 
12
13
  export function CustomizableFieldOption(props: CustomizableFieldOptionProps) {
13
- const { uid, required, optionIndex, index, title, fieldValue } = props
14
- const { control, register } = useFormAddProductsToCart()
14
+ const { uid, required, optionIndex, index, title, fieldValue, productPrice, currency } = props
15
+ const { control, register, resetField, getValues } = useFormAddProductsToCart()
15
16
 
16
- const maxLength = fieldValue?.max_characters ?? 0
17
+ if (!fieldValue) return null
18
+
19
+ const maxLength = fieldValue.max_characters ?? 0
17
20
  return (
18
21
  <Box>
19
22
  <SectionHeader labelLeft={title} sx={{ mt: 0 }} />
@@ -29,7 +32,36 @@ export function CustomizableFieldOption(props: CustomizableFieldOptionProps) {
29
32
  control={control}
30
33
  name={`cartItems.${index}.entered_options.${optionIndex}.value`}
31
34
  required={Boolean(required)}
32
- validation={{
35
+ InputProps={{
36
+ endAdornment:
37
+ fieldValue.price === 0
38
+ ? null
39
+ : fieldValue.price && (
40
+ <Box
41
+ sx={{
42
+ display: 'flex',
43
+ typography: 'body1',
44
+ '&.sizeMedium': { typographty: 'subtitle1' },
45
+ '&.sizeLarge': { typography: 'h6' },
46
+ color: getValues(`cartItems.${index}.entered_options.${optionIndex}.value`)
47
+ ? 'text.primary'
48
+ : 'text.secondary',
49
+ }}
50
+ >
51
+ {/* Change fontFamily so the + is properly outlined */}
52
+ <span style={{ fontFamily: 'arial', paddingTop: '1px' }}>+{'\u00A0'}</span>
53
+ <Money
54
+ value={
55
+ fieldValue.price_type === 'PERCENT'
56
+ ? productPrice * (fieldValue.price / 100)
57
+ : fieldValue.price
58
+ }
59
+ currency={currency}
60
+ />
61
+ </Box>
62
+ ),
63
+ }}
64
+ rules={{
33
65
  maxLength: {
34
66
  value: maxLength,
35
67
  message: i18n._(/* i18n*/ 'There is a maximum of ‘{maxLength}’ characters', {
@@ -43,6 +75,10 @@ export function CustomizableFieldOption(props: CustomizableFieldOptionProps) {
43
75
  maxLength,
44
76
  })
45
77
  }
78
+ onChange={(data) => {
79
+ if (!data.currentTarget.value)
80
+ resetField(`cartItems.${index}.entered_options.${optionIndex}.value`)
81
+ }}
46
82
  />
47
83
  </Box>
48
84
  )
@@ -1 +1,2 @@
1
1
  export * from './ProductCustomizable'
2
+ export * from './productCustomizableSelectors'
@@ -0,0 +1,59 @@
1
+ import type { PriceTypeEnum } from '@graphcommerce/graphql-mesh'
2
+ import type { Simplify } from 'type-fest'
3
+ import type { CustomizableAreaOptionFragment } from './CustomizableAreaOption.gql'
4
+ import type { CustomizableCheckboxOptionFragment } from './CustomizableCheckboxOption.gql'
5
+ import type { CustomizableDateOptionFragment } from './CustomizableDateOption.gql'
6
+ import type { CustomizableDropDownOptionFragment } from './CustomizableDropDownOption.gql'
7
+ import type { CustomizableFieldOptionFragment } from './CustomizableFieldOption.gql'
8
+ import type { CustomizableFileOptionFragment } from './CustomizableFileOption.gql'
9
+ import type { CustomizableMultipleOptionFragment } from './CustomizableMultipleOption.gql'
10
+ import type { CustomizableRadioOptionFragment } from './CustomizableRadioOption.gql'
11
+ import type { ProductCustomizable_SimpleProduct_Fragment } from './ProductCustomizable.gql'
12
+
13
+ export type CustomizableProductOptionBase =
14
+ | {
15
+ price?: number | null | undefined
16
+ price_type?: PriceTypeEnum | null | undefined
17
+ uid?: string | null | undefined
18
+ }
19
+ | undefined
20
+ | null
21
+
22
+ export type AnyOption = NonNullable<
23
+ NonNullable<ProductCustomizable_SimpleProduct_Fragment['options']>[number]
24
+ >
25
+
26
+ export type OptionValueSelector = {
27
+ [T in AnyOption as T['__typename']]: (
28
+ option: T,
29
+ ) => CustomizableProductOptionBase | CustomizableProductOptionBase[]
30
+ }
31
+
32
+ type MissingOptionValueSelectors = Omit<
33
+ OptionValueSelector,
34
+ keyof typeof productCustomizableSelectors
35
+ >
36
+ type DefinedOptionValueSelectors = Partial<
37
+ Pick<OptionValueSelector, keyof typeof productCustomizableSelectors>
38
+ >
39
+
40
+ type Selectors = Simplify<
41
+ keyof MissingOptionValueSelectors extends never
42
+ ? (MissingOptionValueSelectors & DefinedOptionValueSelectors) | undefined
43
+ : MissingOptionValueSelectors & DefinedOptionValueSelectors
44
+ >
45
+
46
+ export const productCustomizableSelectors = {
47
+ CustomizableAreaOption: (o: CustomizableAreaOptionFragment) => o.areaValue,
48
+ CustomizableCheckboxOption: (o: CustomizableCheckboxOptionFragment) => o.checkboxValue,
49
+ CustomizableFileOption: (o: CustomizableFileOptionFragment) => o.fileValue,
50
+ CustomizableDateOption: (o: CustomizableDateOptionFragment) => o.dateValue,
51
+ CustomizableDropDownOption: (o: CustomizableDropDownOptionFragment) => o.dropdownValue,
52
+ CustomizableFieldOption: (o: CustomizableFieldOptionFragment) => o.fieldValue,
53
+ CustomizableMultipleOption: (o: CustomizableMultipleOptionFragment) => o.multipleValue,
54
+ CustomizableRadioOption: (o: CustomizableRadioOptionFragment) => o.radioValue,
55
+ }
56
+
57
+ export type SelectorsProp = keyof MissingOptionValueSelectors extends never
58
+ ? { selectors?: Selectors }
59
+ : { selectors: Selectors }
@@ -1,14 +1,15 @@
1
1
  import { useForm, UseFormProps, UseFormReturn } from '@graphcommerce/ecommerce-ui'
2
- import { useMemoObject } from '@graphcommerce/next-ui'
3
- import { useEventCallback } from '@mui/material'
4
- import React, { BaseSyntheticEvent, createContext, useContext, useMemo } from 'react'
5
- import { useProductListLinkReplace } from '../../hooks/useProductListLinkReplace'
2
+ import { useMatchMediaMotionValue, useMemoObject } from '@graphcommerce/next-ui'
3
+ import { useEventCallback, useTheme } from '@mui/material'
4
+ import { m, useTransform } from 'framer-motion'
5
+ import { useRouter } from 'next/router'
6
+ import React, { BaseSyntheticEvent, createContext, useContext, useMemo, useRef } from 'react'
7
+ import { productListLinkFromFilter } from '../../hooks/useProductListLink'
6
8
  import { ProductListFiltersFragment } from '../ProductListFilters/ProductListFilters.gql'
7
9
  import {
8
10
  ProductFilterParams,
9
11
  ProductListParams,
10
12
  toFilterParams,
11
- toProductListParams,
12
13
  } from '../ProductListItems/filterTypes'
13
14
 
14
15
  type DataProps = {
@@ -44,17 +45,31 @@ export type FilterFormProviderProps = Omit<
44
45
  params: ProductListParams
45
46
  } & DataProps
46
47
 
48
+ const isSidebar = import.meta.graphCommerce.productFiltersLayout === 'SIDEBAR'
49
+
47
50
  export function ProductFiltersPro(props: FilterFormProviderProps) {
48
51
  const { children, params, aggregations, appliedAggregations, filterTypes, ...formProps } = props
49
52
 
50
53
  const defaultValues = useMemoObject(toFilterParams(params))
51
54
  const form = useForm<ProductFilterParams>({ defaultValues, ...formProps })
55
+ const ref = useRef<HTMLFormElement>(null)
56
+
57
+ const router = useRouter()
58
+ const theme = useTheme()
59
+ const isDesktop = useMatchMediaMotionValue('up', 'md')
60
+ const scrollMarginTop = useTransform(() => (isDesktop.get() ? 0 : theme.appShell.headerHeightSm))
61
+ const scroll = useTransform(() => !isSidebar || isDesktop.get())
52
62
 
53
- const push = useProductListLinkReplace({ scroll: false })
54
63
  const submit = useEventCallback(
55
- form.handleSubmit(async (formValues) =>
56
- push({ ...toProductListParams(formValues), currentPage: 1 }),
57
- ),
64
+ form.handleSubmit(async (formValues) => {
65
+ const path = productListLinkFromFilter({ ...formValues, currentPage: 1 })
66
+ if (router.asPath === path) return false
67
+
68
+ const opts = { scroll: scroll.get() }
69
+ return (router.query.url ?? []).includes('q')
70
+ ? router.replace(path, path, opts)
71
+ : router.push(path, path, opts)
72
+ }),
58
73
  )
59
74
 
60
75
  const filterFormContext: FilterFormContextProps = useMemo(
@@ -71,7 +86,7 @@ export function ProductFiltersPro(props: FilterFormProviderProps) {
71
86
 
72
87
  return (
73
88
  <FilterFormContext.Provider value={filterFormContext}>
74
- <form noValidate onSubmit={submit} id='products' />
89
+ <m.form ref={ref} noValidate onSubmit={submit} id='products' style={{ scrollMarginTop }} />
75
90
  {children}
76
91
  </FilterFormContext.Provider>
77
92
  )
@@ -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,15 @@
1
+ import { SortEnum } from '@graphcommerce/graphql-mesh'
2
+ import { IconSvg, iconArrowDown, iconArrowUp } from '@graphcommerce/next-ui'
3
+
4
+ type Props = {
5
+ sortDirection: SortEnum | null
6
+ }
7
+
8
+ export function ProductFiltersProSortDirectionArrow({ sortDirection }: Props) {
9
+ return (
10
+ <IconSvg
11
+ src={sortDirection === 'ASC' || sortDirection === null ? iconArrowUp : iconArrowDown}
12
+ sx={{ display: 'flex' }}
13
+ />
14
+ )
15
+ }
@@ -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
+ }
@@ -1,7 +1,7 @@
1
1
  import { LazyHydrate, RenderType, extendableComponent, responsiveVal } from '@graphcommerce/next-ui'
2
2
  import { Box, BoxProps } from '@mui/material'
3
3
  import { ProductListItemFragment } from '../../Api/ProductListItem.gql'
4
- import { AddProductsToCartForm } from '../../index'
4
+ import { AddProductsToCartForm } from '../AddProductsToCart'
5
5
  import { ProductListItemProps } from '../ProductListItem/ProductListItem'
6
6
  import { ProductListItemRenderer } from './renderer'
7
7
 
@@ -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
+ }
@@ -32,9 +32,11 @@ export function ProductPageBreadcrumb(props: ProductPageBreadcrumbsProps) {
32
32
  {breadcrumb.category_name}
33
33
  </Link>
34
34
  ))}
35
- <Link href={`/${category?.url_path}`} underline='hover' color='inherit'>
36
- {category?.name}
37
- </Link>
35
+ {category && (
36
+ <Link href={`/${category?.url_path}`} underline='hover' color='inherit'>
37
+ {category?.name}
38
+ </Link>
39
+ )}
38
40
  <Typography color='text.primary'>{name}</Typography>
39
41
  </Breadcrumbs>
40
42
  )
@@ -1,4 +1,4 @@
1
- fragment ProductPageMeta on ProductInterface {
1
+ fragment ProductPageMeta on ProductInterface @injectable {
2
2
  ...ProductLink
3
3
  sku
4
4
  name
@@ -1,4 +1,5 @@
1
1
  fragment ProductPagePrice on ProductInterface {
2
+ __typename
2
3
  url_key
3
4
  price_range {
4
5
  minimum_price {
@@ -30,4 +31,6 @@ fragment ProductPagePrice on ProductInterface {
30
31
  }
31
32
  quantity
32
33
  }
34
+
35
+ ...ProductCustomizable
33
36
  }