@graphcommerce/magento-category 4.6.0 → 4.7.1

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,26 @@
1
1
  # Change Log
2
2
 
3
+ ## 4.7.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies []:
8
+ - @graphcommerce/magento-product@4.7.2
9
+
10
+ ## 4.7.0
11
+
12
+ ### Minor Changes
13
+
14
+ - [#1662](https://github.com/graphcommerce-org/graphcommerce/pull/1662) [`de8925aa9`](https://github.com/graphcommerce-org/graphcommerce/commit/de8925aa910b191c62041530c68c697a58a1e52d) Thanks [@paales](https://github.com/paales)! - Allow for a custom Component for magentoMenuToNavigation and allow React.ReactNode for items
15
+
16
+ ### Patch Changes
17
+
18
+ - Updated dependencies [[`0c21c5c23`](https://github.com/graphcommerce-org/graphcommerce/commit/0c21c5c233ebab15f6629c234e3de1cc8c0452e1), [`de8925aa9`](https://github.com/graphcommerce-org/graphcommerce/commit/de8925aa910b191c62041530c68c697a58a1e52d), [`f5eae0afd`](https://github.com/graphcommerce-org/graphcommerce/commit/f5eae0afdbd474b1f81c450425ffadf2d025187a)]:
19
+ - @graphcommerce/next-ui@4.28.0
20
+ - @graphcommerce/framer-scroller@2.1.40
21
+ - @graphcommerce/magento-product@4.7.1
22
+ - @graphcommerce/magento-store@4.3.1
23
+
3
24
  ## 4.6.0
4
25
 
5
26
  ### Minor Changes
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/magento-category",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "4.6.0",
5
+ "version": "4.7.1",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -19,12 +19,12 @@
19
19
  "type-fest": "^2.12.2"
20
20
  },
21
21
  "dependencies": {
22
- "@graphcommerce/framer-scroller": "2.1.39",
22
+ "@graphcommerce/framer-scroller": "2.1.40",
23
23
  "@graphcommerce/graphql": "3.4.8",
24
24
  "@graphcommerce/image": "3.1.9",
25
- "@graphcommerce/magento-product": "4.7.0",
26
- "@graphcommerce/magento-store": "4.3.0",
27
- "@graphcommerce/next-ui": "4.27.0"
25
+ "@graphcommerce/magento-product": "4.7.2",
26
+ "@graphcommerce/magento-store": "4.3.1",
27
+ "@graphcommerce/next-ui": "4.28.0"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "@lingui/react": "^3.13.2",
@@ -1,7 +1,7 @@
1
- fragment NavigationItem on CategoryTree {
1
+ fragment NavigationItem on CategoryTree @injectable {
2
2
  uid
3
3
  include_in_menu
4
4
  name
5
5
  url_path
6
- children_count
6
+ level
7
7
  }
@@ -0,0 +1,86 @@
1
+ import {
2
+ NavigationNode,
3
+ NavigationNodeButton,
4
+ NavigationNodeHref,
5
+ NavigationNodeType,
6
+ nonNullable,
7
+ } from '@graphcommerce/next-ui'
8
+ import { i18n } from '@lingui/core'
9
+ import { MenuQueryFragment } from '../queries/MenuQueryFragment.gql'
10
+ import { NavigationItemFragment } from '../queries/NavigationItem.gql'
11
+
12
+ type Item = NonNullable<NonNullable<NonNullable<MenuQueryFragment['menu']>['items']>[0]>
13
+
14
+ export type MagentoNavigationItemProps = NavigationItemFragment
15
+
16
+ function categoryToNav(
17
+ props: Item | null | undefined,
18
+ Component?: React.ComponentType<MagentoNavigationItemProps>,
19
+ ): NavigationNode | undefined {
20
+ if (!props) return undefined
21
+ const { uid, children, include_in_menu, name, url_path, level } = props
22
+
23
+ if (!uid || include_in_menu !== 1 || !name) return undefined
24
+
25
+ const allName = i18n._(/* i18n */ 'All {name}', { name })
26
+ // If we've got children we make a button that navigates to childitems.
27
+ if (children && children.length > 0) {
28
+ const button: NavigationNodeButton = {
29
+ type: NavigationNodeType.BUTTON,
30
+ href: url_path ? `/${url_path}` : undefined,
31
+ name: Component ? <Component key={uid} {...props} /> : name,
32
+ id: uid,
33
+ childItems: [
34
+ ...(url_path
35
+ ? [
36
+ {
37
+ name: Component ? (
38
+ <Component key={uid} {...props} name={allName} level={(level ?? 0) + 1} />
39
+ ) : (
40
+ allName
41
+ ),
42
+ href: `/${url_path}`,
43
+ id: `${uid}-all`,
44
+ },
45
+ ]
46
+ : []),
47
+ ...children.map((child) => categoryToNav(child, Component)).filter(nonNullable),
48
+ ],
49
+ }
50
+ return button
51
+ }
52
+
53
+ // If we've got no children we make a href.
54
+ return {
55
+ name: Component ? <Component key={uid} {...props} /> : name,
56
+ id: uid,
57
+ href: `/${url_path}`,
58
+ } as NavigationNodeHref
59
+ }
60
+
61
+ /**
62
+ * Converts the Magento GraphQL category tree to a NavigationNode tree, which can be used in the
63
+ * Navigation component.
64
+ *
65
+ * To provide a custom renderer to the navigation items, pass a component as the third argument;
66
+ *
67
+ * ```tsx
68
+ * function MagentoNavigationItem(props: MagentoNavigationItemProps) {
69
+ * const { name, level } = props
70
+ * return (
71
+ * <>
72
+ * {name} {level}
73
+ * </>
74
+ * )
75
+ * }
76
+ * ```
77
+ */
78
+ export function magentoMenuToNavigation(
79
+ menu: MenuQueryFragment['menu'],
80
+ includeRoot: boolean,
81
+ Component?: React.ComponentType<MagentoNavigationItemProps>,
82
+ ) {
83
+ return ((includeRoot ? menu?.items : menu?.items?.[0]?.children) || [])
84
+ .map((child) => categoryToNav(child, Component))
85
+ .filter(nonNullable)
86
+ }
@@ -1,60 +0,0 @@
1
- import {
2
- NavigationNode,
3
- NavigationNodeButton,
4
- NavigationNodeHref,
5
- nonNullable,
6
- } from '@graphcommerce/next-ui'
7
- import { i18n } from '@lingui/core'
8
- import { MenuQueryFragment } from '../queries/MenuQueryFragment.gql'
9
-
10
- export type Item = NonNullable<NonNullable<MenuQueryFragment['menu']>['items']>[0]
11
-
12
- export function categoryToNav(props: Item | null | undefined): NavigationNode | undefined {
13
- if (!props) return undefined
14
- const { uid, children, include_in_menu, name, url_path } = props
15
-
16
- if (!uid || include_in_menu !== 1 || !name) return undefined
17
-
18
- // If we've got children we make a button that navigates to childitems.
19
- if (children && children.length > 0) {
20
- return {
21
- name,
22
- id: uid,
23
- childItems: [
24
- ...(url_path
25
- ? [
26
- {
27
- name: i18n._(/* i18n */ 'All {name}', { name }),
28
- href: `/${url_path}`,
29
- id: `${uid}-all`,
30
- },
31
- ]
32
- : []),
33
- ...children.map(categoryToNav).filter(nonNullable),
34
- ],
35
- } as NavigationNodeButton
36
- }
37
-
38
- // If we've got no children we make a href.
39
- return { name, id: uid, href: `/${url_path}` } as NavigationNodeHref
40
- }
41
-
42
- /**
43
- * Converts the Magento GraphQL category tree to a NavigationNode tree, which can be used in the
44
- * Navigation component.
45
- */
46
- export function magentoMenuToNavigation(menu: MenuQueryFragment['menu'], includeRoot: boolean) {
47
- return ((includeRoot ? menu?.items : menu?.items?.[0]?.children) || [])
48
- .map(categoryToNav)
49
- .filter(nonNullable)
50
- }
51
-
52
- /**
53
- * Converts the Magento GraphQL category tree to a NavigationNode tree, which can be used in the
54
- * Navigation component.
55
- *
56
- * @deprecated Please use magentoMenuToNavigation instead.
57
- */
58
- export function useMagentoMenuToNavigation(menu: MenuQueryFragment['menu'], includeRoot: boolean) {
59
- return magentoMenuToNavigation(menu, includeRoot)
60
- }