@graphcommerce/magento-wishlist 1.2.0 → 1.2.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,19 @@
1
1
  # @graphcommerce/magento-wishlist
2
2
 
3
+ ## 1.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1485](https://github.com/graphcommerce-org/graphcommerce/pull/1485) [`d6262de71`](https://github.com/graphcommerce-org/graphcommerce/commit/d6262de71d2254a2b0b492e1a60f9e141767470e) Thanks [@paales](https://github.com/paales)! - move to useCustomerSession instead of using the tokenquery directly and fix ssr issues
8
+
9
+ - Updated dependencies [[`d6262de71`](https://github.com/graphcommerce-org/graphcommerce/commit/d6262de71d2254a2b0b492e1a60f9e141767470e), [`c8c246b8a`](https://github.com/graphcommerce-org/graphcommerce/commit/c8c246b8aaab0621b68a2fca2a1c529a56fad962), [`e3005fe63`](https://github.com/graphcommerce-org/graphcommerce/commit/e3005fe6306093d47b08c6756c21c8175649e30b)]:
10
+ - @graphcommerce/magento-customer@4.4.0
11
+ - @graphcommerce/magento-cart@4.3.2
12
+ - @graphcommerce/next-ui@4.8.2
13
+ - @graphcommerce/magento-product-configurable@4.1.8
14
+ - @graphcommerce/magento-product@4.4.1
15
+ - @graphcommerce/magento-store@4.2.6
16
+
3
17
  ## 1.2.0
4
18
 
5
19
  ### Minor Changes
@@ -1,5 +1,9 @@
1
- import { useQuery, useMutation, useApolloClient } from '@graphcommerce/graphql'
2
- import { CustomerTokenDocument } from '@graphcommerce/magento-customer'
1
+ import { useMutation, useApolloClient } from '@graphcommerce/graphql'
2
+ import {
3
+ useCustomerQuery,
4
+ useCustomerSession,
5
+ useGuestQuery,
6
+ } from '@graphcommerce/magento-customer'
3
7
  import {
4
8
  IconSvg,
5
9
  iconHeart,
@@ -39,19 +43,13 @@ export function ProductWishlistChipBase(props: ProductWishlistChipProps) {
39
43
  const [inWishlist, setInWishlist] = useState(false)
40
44
  const [displayMessageBar, setDisplayMessageBar] = useState(false)
41
45
 
42
- const { data: token } = useQuery(CustomerTokenDocument)
46
+ const { loggedIn } = useCustomerSession()
43
47
  const [addWishlistItem] = useMutation(AddProductToWishlistDocument)
44
48
  const [removeWishlistItem] = useMutation(RemoveProductFromWishlistDocument)
45
- const isLoggedIn = token?.customerToken && token?.customerToken.valid
46
49
 
47
- const { data: GetCustomerWishlistData, loading } = useQuery(GetIsInWishlistsDocument, {
48
- skip: !isLoggedIn,
49
- })
50
+ const { data: GetCustomerWishlistData, loading } = useCustomerQuery(GetIsInWishlistsDocument)
50
51
 
51
- const { data: guestWishlistData } = useQuery(GuestWishlistDocument, {
52
- ssr: false,
53
- skip: isLoggedIn === true,
54
- })
52
+ const { data: guestWishlistData } = useGuestQuery(GuestWishlistDocument)
55
53
 
56
54
  const { cache } = useApolloClient()
57
55
 
@@ -80,7 +78,7 @@ export function ProductWishlistChipBase(props: ProductWishlistChipProps) {
80
78
 
81
79
  useEffect(() => {
82
80
  // Do not display wishlist UI to guests when configured as customer only
83
- if (hideForGuest && !isLoggedIn) {
81
+ if (hideForGuest && !loggedIn) {
84
82
  return
85
83
  }
86
84
 
@@ -89,17 +87,17 @@ export function ProductWishlistChipBase(props: ProductWishlistChipProps) {
89
87
  }
90
88
 
91
89
  // Mark as active when product is available in either customer or guest wishlist
92
- if (isLoggedIn && !loading) {
90
+ if (loggedIn && !loading) {
93
91
  const inWishlistTest =
94
92
  GetCustomerWishlistData?.customer?.wishlists[0]?.items_v2?.items.map(
95
93
  (item) => item?.product?.sku,
96
94
  ) || []
97
95
  setInWishlist(inWishlistTest.includes(sku))
98
- } else if (!isLoggedIn) {
96
+ } else if (!loggedIn) {
99
97
  const inWishlistTest = guestWishlistData?.guestWishlist?.items.map((item) => item?.sku) || []
100
98
  setInWishlist(inWishlistTest.includes(sku))
101
99
  }
102
- }, [isLoggedIn, sku, loading, GetCustomerWishlistData, guestWishlistData])
100
+ }, [loggedIn, sku, loading, GetCustomerWishlistData, guestWishlistData])
103
101
 
104
102
  const preventAnimationBubble: React.MouseEventHandler<HTMLButtonElement> = (e) => {
105
103
  e.preventDefault()
@@ -113,7 +111,7 @@ export function ProductWishlistChipBase(props: ProductWishlistChipProps) {
113
111
  return
114
112
  }
115
113
 
116
- if (isLoggedIn) {
114
+ if (loggedIn) {
117
115
  if (inWishlist && !ignoreProductWishlistStatus) {
118
116
  const wishlistItemsInSession =
119
117
  GetCustomerWishlistData?.customer?.wishlists[0]?.items_v2?.items || []
@@ -218,7 +216,7 @@ export function ProductWishlistChipBase(props: ProductWishlistChipProps) {
218
216
  </Box>
219
217
  )
220
218
 
221
- return !hideForGuest || isLoggedIn ? output : null
219
+ return !hideForGuest || loggedIn ? output : null
222
220
  }
223
221
 
224
222
  ProductWishlistChipBase.defaultProps = {
@@ -1,10 +1,13 @@
1
- import { useQuery } from '@graphcommerce/graphql'
2
- import { CustomerTokenDocument } from '@graphcommerce/magento-customer'
1
+ import {
2
+ useCustomerQuery,
3
+ useCustomerSession,
4
+ useGuestQuery,
5
+ } from '@graphcommerce/magento-customer'
3
6
  import { iconHeart, DesktopHeaderBadge, IconSvg, extendableComponent } from '@graphcommerce/next-ui'
4
7
  import { i18n } from '@lingui/core'
5
8
  import { Fab, FabProps as FabPropsType, NoSsr, SxProps, Theme } from '@mui/material'
6
9
  import PageLink from 'next/link'
7
- import React, { useEffect } from 'react'
10
+ import React from 'react'
8
11
  import { useWishlistEnabled } from '../../hooks'
9
12
  import { GetIsInWishlistsDocument } from '../../queries/GetIsInWishlists.gql'
10
13
  import { GuestWishlistDocument } from '../../queries/GuestWishlist.gql'
@@ -13,6 +16,7 @@ type WishlistFabContentProps = {
13
16
  icon?: React.ReactNode
14
17
  FabProps?: Omit<FabPropsType, 'children'>
15
18
  sx?: SxProps<Theme>
19
+ activeWishlist: boolean
16
20
  }
17
21
 
18
22
  const name = 'WishlistFab'
@@ -22,31 +26,7 @@ const { classes } = extendableComponent(name, parts)
22
26
  const hideForGuest = process.env.NEXT_PUBLIC_WISHLIST_HIDE_FOR_GUEST === '1'
23
27
 
24
28
  function WishlistFabContent(props: WishlistFabContentProps) {
25
- const { icon, FabProps, sx } = props
26
-
27
- const { data: token } = useQuery(CustomerTokenDocument)
28
- const isLoggedIn = token?.customerToken && token?.customerToken.valid
29
-
30
- const { data: GetCustomerWishlistData, loading } = useQuery(GetIsInWishlistsDocument, {
31
- skip: !isLoggedIn,
32
- })
33
-
34
- const { data: guestWishlistData, loading: loadingGuestWishlistData } = useQuery(
35
- GuestWishlistDocument,
36
- {
37
- ssr: false,
38
- skip: isLoggedIn === true,
39
- },
40
- )
41
-
42
- let activeWishlist = false
43
- if (isLoggedIn) {
44
- const wishlistItemCount = GetCustomerWishlistData?.customer?.wishlists[0]?.items_count || 0
45
- activeWishlist = wishlistItemCount > 0
46
- } else {
47
- const wishlist = guestWishlistData?.guestWishlist?.items || []
48
- activeWishlist = wishlist.length > 0
49
- }
29
+ const { icon, FabProps, sx, activeWishlist } = props
50
30
 
51
31
  const wishlistIcon = icon ?? <IconSvg src={iconHeart} size='large' />
52
32
 
@@ -73,19 +53,30 @@ function WishlistFabContent(props: WishlistFabContentProps) {
73
53
  )
74
54
  }
75
55
 
76
- export function WishlistFab(props: WishlistFabContentProps) {
56
+ export type WishlistFabProps = Omit<WishlistFabContentProps, 'activeWishlist'>
57
+
58
+ export function WishlistFab(props: WishlistFabProps) {
77
59
  const isWishlistEnabled = useWishlistEnabled()
78
60
 
79
- const { data: token } = useQuery(CustomerTokenDocument)
80
- const isLoggedIn = token?.customerToken && token?.customerToken.valid
61
+ const { loggedIn } = useCustomerSession()
62
+
63
+ const { data: GetCustomerWishlistData } = useCustomerQuery(GetIsInWishlistsDocument)
64
+ const { data: guestWishlistData } = useGuestQuery(GuestWishlistDocument)
65
+
66
+ let activeWishlist = false
67
+ if (loggedIn) {
68
+ const wishlistItemCount = GetCustomerWishlistData?.customer?.wishlists[0]?.items_count || 0
69
+ activeWishlist = wishlistItemCount > 0
70
+ } else {
71
+ const wishlist = guestWishlistData?.guestWishlist?.items || []
72
+ activeWishlist = wishlist.length > 0
73
+ }
74
+
75
+ if (!isWishlistEnabled || hideForGuest) return null
81
76
 
82
77
  return (
83
- <>
84
- {isWishlistEnabled && (!hideForGuest || isLoggedIn) && (
85
- <NoSsr>
86
- <WishlistFabContent {...props} />
87
- </NoSsr>
88
- )}
89
- </>
78
+ <NoSsr fallback={<WishlistFabContent {...props} activeWishlist={false} />}>
79
+ <WishlistFabContent {...props} activeWishlist={activeWishlist} />
80
+ </NoSsr>
90
81
  )
91
82
  }
@@ -1,6 +1,6 @@
1
- import { useQuery, useMutation, useApolloClient } from '@graphcommerce/graphql'
1
+ import { useMutation, useApolloClient } from '@graphcommerce/graphql'
2
2
  import { Image } from '@graphcommerce/image'
3
- import { CustomerTokenDocument } from '@graphcommerce/magento-customer'
3
+ import { useCustomerQuery, useCustomerSession } from '@graphcommerce/magento-customer'
4
4
  import { useProductLink } from '@graphcommerce/magento-product'
5
5
  import { Money } from '@graphcommerce/magento-store'
6
6
  import { responsiveVal, extendableComponent, iconEllypsis, IconSvg } from '@graphcommerce/next-ui'
@@ -57,12 +57,9 @@ export function WishlistItemBase(props: WishlistItemBaseProps) {
57
57
  const productLink = useProductLink({ url_key, __typename: productType })
58
58
  const { cache } = useApolloClient()
59
59
 
60
- const { data: token } = useQuery(CustomerTokenDocument)
61
- const isLoggedIn = token?.customerToken && token?.customerToken.valid
60
+ const { loggedIn } = useCustomerSession()
62
61
 
63
- const { data: GetCustomerWishlistData } = useQuery(GetIsInWishlistsDocument, {
64
- skip: !isLoggedIn,
65
- })
62
+ const { data: GetCustomerWishlistData } = useCustomerQuery(GetIsInWishlistsDocument)
66
63
 
67
64
  const [removeWishlistItem] = useMutation(RemoveProductFromWishlistDocument)
68
65
 
@@ -75,7 +72,7 @@ export function WishlistItemBase(props: WishlistItemBaseProps) {
75
72
 
76
73
  const handleClose = (event) => {
77
74
  if (event.target.id === 'remove') {
78
- if (isLoggedIn) {
75
+ if (loggedIn) {
79
76
  let itemIdToDelete = wishlistItemId
80
77
 
81
78
  /** When no internal ID is provided, fetch it by sku */
@@ -1,5 +1,8 @@
1
- import { useQuery } from '@graphcommerce/graphql'
2
- import { CustomerTokenDocument } from '@graphcommerce/magento-customer'
1
+ import {
2
+ useCustomerQuery,
3
+ useCustomerSession,
4
+ useGuestQuery,
5
+ } from '@graphcommerce/magento-customer'
3
6
  import { MenuFabSecondaryItem, iconHeart, IconSvg } from '@graphcommerce/next-ui'
4
7
  import { Badge, NoSsr, SxProps, Theme } from '@mui/material'
5
8
  import React from 'react'
@@ -7,38 +10,17 @@ import { useWishlistEnabled } from '../../hooks'
7
10
  import { GetIsInWishlistsDocument } from '../../queries/GetIsInWishlists.gql'
8
11
  import { GuestWishlistDocument } from '../../queries/GuestWishlist.gql'
9
12
 
10
- type WishlistMenuFabItemProps = {
13
+ type WishlistMenuFabItemContentProps = {
11
14
  icon?: React.ReactNode
12
15
  children: React.ReactNode
13
16
  sx?: SxProps<Theme>
17
+ activeWishlist: boolean
14
18
  }
15
19
 
16
20
  const hideForGuest = process.env.NEXT_PUBLIC_WISHLIST_HIDE_FOR_GUEST === '1'
17
21
 
18
- function WishlistMenuFabItemContent(props: WishlistMenuFabItemProps) {
19
- const { icon, children, sx = [] } = props
20
-
21
- const { data: token } = useQuery(CustomerTokenDocument)
22
- const isLoggedIn = token?.customerToken && token?.customerToken.valid
23
-
24
- const { data: GetCustomerWishlistData } = useQuery(GetIsInWishlistsDocument, {
25
- ssr: false,
26
- skip: !isLoggedIn,
27
- })
28
-
29
- const { data: guestWishlistData } = useQuery(GuestWishlistDocument, {
30
- ssr: false,
31
- skip: isLoggedIn === true,
32
- })
33
-
34
- let activeWishlist
35
- if (isLoggedIn) {
36
- const wishlistItemCount = GetCustomerWishlistData?.customer?.wishlists[0]?.items_count || 0
37
- activeWishlist = wishlistItemCount > 0
38
- } else {
39
- const wishlist = guestWishlistData?.guestWishlist?.items || []
40
- activeWishlist = wishlist.length > 0
41
- }
22
+ function WishlistMenuFabItemContent(props: WishlistMenuFabItemContentProps) {
23
+ const { icon, children, sx = [], activeWishlist } = props
42
24
 
43
25
  return (
44
26
  <MenuFabSecondaryItem
@@ -60,19 +42,29 @@ function WishlistMenuFabItemContent(props: WishlistMenuFabItemProps) {
60
42
  )
61
43
  }
62
44
 
45
+ export type WishlistMenuFabItemProps = Omit<WishlistMenuFabItemContentProps, 'activeWishlist'>
46
+
63
47
  export function WishlistMenuFabItem(props: WishlistMenuFabItemProps) {
64
48
  const isWishlistEnabled = useWishlistEnabled()
49
+ const { loggedIn } = useCustomerSession()
65
50
 
66
- const { data: token } = useQuery(CustomerTokenDocument)
67
- const isLoggedIn = token?.customerToken && token?.customerToken.valid
51
+ const { data: GetCustomerWishlistData } = useCustomerQuery(GetIsInWishlistsDocument)
52
+ const { data: guestWishlistData } = useGuestQuery(GuestWishlistDocument)
53
+
54
+ let activeWishlist: boolean
55
+ if (loggedIn) {
56
+ const wishlistItemCount = GetCustomerWishlistData?.customer?.wishlists[0]?.items_count ?? 0
57
+ activeWishlist = wishlistItemCount > 0
58
+ } else {
59
+ const wishlist = guestWishlistData?.guestWishlist?.items ?? []
60
+ activeWishlist = wishlist.length > 0
61
+ }
68
62
 
63
+ if (hideForGuest) return null
64
+ if (!isWishlistEnabled) return null
69
65
  return (
70
- <>
71
- {isWishlistEnabled && (!hideForGuest || isLoggedIn) && (
72
- <NoSsr fallback={<WishlistMenuFabItemContent {...props} />}>
73
- <WishlistMenuFabItemContent {...props} />
74
- </NoSsr>
75
- )}
76
- </>
66
+ <NoSsr fallback={<WishlistMenuFabItemContent {...props} activeWishlist={false} />}>
67
+ <WishlistMenuFabItemContent {...props} activeWishlist={activeWishlist} />
68
+ </NoSsr>
77
69
  )
78
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphcommerce/magento-wishlist",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "sideEffects": false,
5
5
  "prettier": "@graphcommerce/prettier-config-pwa",
6
6
  "browserslist": [
@@ -22,12 +22,12 @@
22
22
  "@graphcommerce/graphql": "3.1.3",
23
23
  "@graphcommerce/graphql-mesh": "4.1.3",
24
24
  "@graphcommerce/image": "3.1.6",
25
- "@graphcommerce/magento-cart": "4.3.1",
26
- "@graphcommerce/magento-customer": "4.3.2",
27
- "@graphcommerce/magento-product": "4.4.0",
28
- "@graphcommerce/magento-product-configurable": "4.1.7",
29
- "@graphcommerce/magento-store": "4.2.5",
30
- "@graphcommerce/next-ui": "4.8.1"
25
+ "@graphcommerce/magento-cart": "4.3.2",
26
+ "@graphcommerce/magento-customer": "4.4.0",
27
+ "@graphcommerce/magento-product": "4.4.1",
28
+ "@graphcommerce/magento-product-configurable": "4.1.8",
29
+ "@graphcommerce/magento-store": "4.2.6",
30
+ "@graphcommerce/next-ui": "4.8.2"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "@lingui/react": "^3.13.2",