@magento/peregrine 15.6.2-alpha11 → 15.6.2-alpha12

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.
@@ -7,7 +7,7 @@ export const GET_WISHLIST_ITEMS = gql`
7
7
  customer {
8
8
  wishlists {
9
9
  id
10
- items_v2(currentPage: $currentPage, pageSize: 10) {
10
+ items_v2(currentPage: $currentPage, pageSize: 20) {
11
11
  items {
12
12
  id
13
13
  # eslint-disable-next-line @graphql-eslint/require-id-when-available
@@ -1,4 +1,3 @@
1
- import { useState } from 'react';
2
1
  import { useQuery } from '@apollo/client';
3
2
  import { useUserContext } from '../../context/user';
4
3
  import mergeOperations from '../../util/shallowMerge';
@@ -16,19 +15,16 @@ export const useCustomerWishlistSkus = (props = {}) => {
16
15
  const operations = mergeOperations(defaultOperations, props.operations);
17
16
  const [{ isSignedIn }] = useUserContext();
18
17
 
19
- const [currentPage, setCurrentPage] = useState(1);
20
-
21
18
  const {
22
19
  client,
23
20
  data: { customerWishlistProducts }
24
21
  } = useQuery(operations.getProductsInWishlistsQuery);
25
22
 
26
23
  useQuery(operations.getWishlistItemsQuery, {
27
- fetchPolicy: 'cache-and-network',
24
+ fetchPolicy: 'cache-first',
28
25
  onCompleted: data => {
29
26
  const itemsToAdd = new Set();
30
27
  const wishlists = data.customer.wishlists;
31
- let shouldFetchMore = false;
32
28
  wishlists.map(wishlist => {
33
29
  const items = wishlist.items_v2.items;
34
30
  items.map(item => {
@@ -37,12 +33,6 @@ export const useCustomerWishlistSkus = (props = {}) => {
37
33
  itemsToAdd.add(sku);
38
34
  }
39
35
  });
40
-
41
- const pageInfo = wishlist.items_v2.page_info;
42
-
43
- if (pageInfo.total_pages > pageInfo.current_page) {
44
- shouldFetchMore = true;
45
- }
46
36
  });
47
37
 
48
38
  if (itemsToAdd.size) {
@@ -56,14 +46,10 @@ export const useCustomerWishlistSkus = (props = {}) => {
56
46
  }
57
47
  });
58
48
  }
59
-
60
- if (shouldFetchMore) {
61
- setCurrentPage(current => ++current);
62
- }
63
49
  },
64
50
  skip: !isSignedIn,
65
51
  variables: {
66
- currentPage
52
+ currentPage: 1
67
53
  }
68
54
  });
69
55
  };
@@ -1,4 +1,4 @@
1
- import { useCallback, useEffect, useState } from 'react';
1
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
2
  import { useLazyQuery } from '@apollo/client';
3
3
  import mergeOperations from '../../util/shallowMerge';
4
4
  import defaultOperations from './wishlist.gql';
@@ -17,15 +17,17 @@ export const useWishlist = (props = {}) => {
17
17
  const [page, setPage] = useState(1);
18
18
  const [isOpen, setIsOpen] = useState(!isCollapsed);
19
19
  const [isFetchingMore, setIsFetchingMore] = useState(false);
20
+ const hasFetchedRef = useRef(false);
20
21
 
21
22
  const [fetchWishlistItems, queryResult] = useLazyQuery(
22
23
  operations.getCustomerWishlistItems,
23
24
  {
24
- fetchPolicy: 'cache-and-network',
25
+ fetchPolicy: 'cache-first',
25
26
  nextFetchPolicy: 'cache-first',
26
27
  variables: {
27
28
  id,
28
- currentPage: 1
29
+ currentPage: 1,
30
+ pageSize: 20
29
31
  }
30
32
  }
31
33
  );
@@ -38,27 +40,84 @@ export const useWishlist = (props = {}) => {
38
40
  const handleLoadMore = useCallback(async () => {
39
41
  setIsFetchingMore(true);
40
42
  const currentPage = page + 1;
41
- await fetchMore({
42
- variables: {
43
- id,
44
- currentPage
45
- }
46
- });
47
43
 
48
- setPage(currentPage);
49
- setIsFetchingMore(false);
44
+ try {
45
+ await fetchMore({
46
+ variables: {
47
+ id,
48
+ currentPage,
49
+ pageSize: 20
50
+ },
51
+ updateQuery: (prevResult, { fetchMoreResult }) => {
52
+ if (!fetchMoreResult) {
53
+ return prevResult;
54
+ }
55
+
56
+ const prevWishlist = prevResult.customer.wishlist_v2;
57
+ const newWishlist = fetchMoreResult.customer.wishlist_v2;
58
+
59
+ if (prevWishlist.id !== newWishlist.id) {
60
+ return prevResult;
61
+ }
62
+
63
+ const prevItems = prevWishlist.items_v2.items || [];
64
+ const newItems = newWishlist.items_v2.items || [];
65
+
66
+ const existingIds = new Set(prevItems.map(item => item.id));
67
+ const uniqueNewItems = newItems.filter(
68
+ item => !existingIds.has(item.id)
69
+ );
70
+
71
+ return {
72
+ ...prevResult,
73
+ customer: {
74
+ ...prevResult.customer,
75
+ wishlist_v2: {
76
+ ...prevWishlist,
77
+ items_v2: {
78
+ ...prevWishlist.items_v2,
79
+ items: [...prevItems, ...uniqueNewItems]
80
+ }
81
+ }
82
+ }
83
+ };
84
+ }
85
+ });
86
+
87
+ setPage(currentPage);
88
+ } catch (error) {
89
+ console.error('Error loading more wishlist items:', error);
90
+ } finally {
91
+ setIsFetchingMore(false);
92
+ }
50
93
  }, [id, fetchMore, page]);
51
94
 
52
95
  useEffect(() => {
53
- if (itemsCount >= 1 && isOpen === true && !data) {
96
+ if (itemsCount >= 1 && isOpen === true && !hasFetchedRef.current) {
97
+ hasFetchedRef.current = true;
54
98
  fetchWishlistItems();
55
99
  }
56
- }, [itemsCount, isOpen, fetchWishlistItems, data]);
100
+ }, [itemsCount, isOpen, fetchWishlistItems]);
101
+
102
+ const items = useMemo(() => {
103
+ if (!data || !data.customer || !data.customer.wishlist_v2) {
104
+ return [];
105
+ }
106
+
107
+ const allItems = data.customer.wishlist_v2.items_v2?.items || [];
108
+
109
+ const uniqueItems = [];
110
+ const seenIds = new Set();
111
+
112
+ for (const item of allItems) {
113
+ if (!seenIds.has(item.id)) {
114
+ seenIds.add(item.id);
115
+ uniqueItems.push(item);
116
+ }
117
+ }
57
118
 
58
- const items =
59
- data && data.customer.wishlist_v2.items_v2.items
60
- ? data.customer.wishlist_v2.items_v2.items
61
- : [];
119
+ return uniqueItems;
120
+ }, [data]);
62
121
 
63
122
  return {
64
123
  handleContentToggle,
@@ -17,12 +17,12 @@ export const GET_CUSTOMER_WISHLIST = gql`
17
17
  `;
18
18
 
19
19
  export const GET_CUSTOMER_WISHLIST_ITEMS = gql`
20
- query getCustomerWishlist($id: ID!, $currentPage: Int) {
20
+ query getCustomerWishlist($id: ID!, $currentPage: Int, $pageSize: Int) {
21
21
  # eslint-disable-next-line @graphql-eslint/require-id-when-available
22
22
  customer {
23
23
  wishlist_v2(id: $id) {
24
24
  id
25
- items_v2(currentPage: $currentPage) {
25
+ items_v2(currentPage: $currentPage, pageSize: $pageSize) {
26
26
  items {
27
27
  id
28
28
  ...WishlistItemFragment
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magento/peregrine",
3
- "version": "15.6.2-alpha11",
3
+ "version": "15.6.2-alpha12",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },