Skip to content

Commit 37d55fa

Browse files
committed
PWA-3613: RI-Wishlist pagination fix
1 parent 9c1da8d commit 37d55fa

File tree

5 files changed

+80
-39
lines changed

5 files changed

+80
-39
lines changed

packages/peregrine/lib/hooks/useCustomerWishlistSkus/customerWishlist.gql.ce.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const GET_WISHLIST_ITEMS = gql`
77
customer {
88
wishlists {
99
id
10-
items_v2(currentPage: $currentPage, pageSize: 10) {
10+
items_v2(currentPage: $currentPage, pageSize: 20) {
1111
items {
1212
id
1313
# eslint-disable-next-line @graphql-eslint/require-id-when-available

packages/peregrine/lib/hooks/useCustomerWishlistSkus/useCustomerWishlistSkus.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useState } from 'react';
21
import { useQuery } from '@apollo/client';
32
import { useUserContext } from '../../context/user';
43
import mergeOperations from '../../util/shallowMerge';
@@ -16,19 +15,16 @@ export const useCustomerWishlistSkus = (props = {}) => {
1615
const operations = mergeOperations(defaultOperations, props.operations);
1716
const [{ isSignedIn }] = useUserContext();
1817

19-
const [currentPage, setCurrentPage] = useState(1);
20-
2118
const {
2219
client,
2320
data: { customerWishlistProducts }
2421
} = useQuery(operations.getProductsInWishlistsQuery);
2522

2623
useQuery(operations.getWishlistItemsQuery, {
27-
fetchPolicy: 'cache-and-network',
24+
fetchPolicy: 'cache-first',
2825
onCompleted: data => {
2926
const itemsToAdd = new Set();
3027
const wishlists = data.customer.wishlists;
31-
let shouldFetchMore = false;
3228
wishlists.map(wishlist => {
3329
const items = wishlist.items_v2.items;
3430
items.map(item => {
@@ -37,12 +33,6 @@ export const useCustomerWishlistSkus = (props = {}) => {
3733
itemsToAdd.add(sku);
3834
}
3935
});
40-
41-
const pageInfo = wishlist.items_v2.page_info;
42-
43-
if (pageInfo.total_pages > pageInfo.current_page) {
44-
shouldFetchMore = true;
45-
}
4636
});
4737

4838
if (itemsToAdd.size) {
@@ -57,13 +47,10 @@ export const useCustomerWishlistSkus = (props = {}) => {
5747
});
5848
}
5949

60-
if (shouldFetchMore) {
61-
setCurrentPage(current => ++current);
62-
}
6350
},
6451
skip: !isSignedIn,
6552
variables: {
66-
currentPage
53+
currentPage: 1
6754
}
6855
});
6956
};

packages/peregrine/lib/talons/WishlistPage/__tests__/__snapshots__/useWishlist.spec.js.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ Object {
3636
Object {
3737
"name": "item1",
3838
},
39-
Object {
40-
"name": "item2",
41-
},
4239
],
4340
}
4441
`;

packages/peregrine/lib/talons/WishlistPage/useWishlist.js

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useState } from 'react';
1+
import { useCallback, useEffect, useMemo, useState } from 'react';
22
import { useLazyQuery } from '@apollo/client';
33
import mergeOperations from '../../util/shallowMerge';
44
import defaultOperations from './wishlist.gql';
@@ -21,15 +21,16 @@ export const useWishlist = (props = {}) => {
2121
const [fetchWishlistItems, queryResult] = useLazyQuery(
2222
operations.getCustomerWishlistItems,
2323
{
24-
fetchPolicy: 'cache-and-network',
24+
fetchPolicy: 'cache-first',
2525
nextFetchPolicy: 'cache-first',
2626
variables: {
2727
id,
28-
currentPage: 1
28+
currentPage: 1,
29+
pageSize: 20
2930
}
3031
}
3132
);
32-
const { data, error, loading, fetchMore } = queryResult;
33+
const { data, error, loading, fetchMore, called } = queryResult;
3334

3435
const handleContentToggle = () => {
3536
setIsOpen(currentValue => !currentValue);
@@ -38,27 +39,83 @@ export const useWishlist = (props = {}) => {
3839
const handleLoadMore = useCallback(async () => {
3940
setIsFetchingMore(true);
4041
const currentPage = page + 1;
41-
await fetchMore({
42-
variables: {
43-
id,
44-
currentPage
45-
}
46-
});
4742

48-
setPage(currentPage);
49-
setIsFetchingMore(false);
43+
try {
44+
await fetchMore({
45+
variables: {
46+
id,
47+
currentPage,
48+
pageSize: 20
49+
},
50+
updateQuery: (prevResult, { fetchMoreResult }) => {
51+
if (!fetchMoreResult) {
52+
return prevResult;
53+
}
54+
55+
const prevWishlist = prevResult.customer.wishlist_v2;
56+
const newWishlist = fetchMoreResult.customer.wishlist_v2;
57+
58+
if (prevWishlist.id !== newWishlist.id) {
59+
return prevResult;
60+
}
61+
62+
const prevItems = prevWishlist.items_v2.items || [];
63+
const newItems = newWishlist.items_v2.items || [];
64+
65+
const existingIds = new Set(prevItems.map(item => item.id));
66+
const uniqueNewItems = newItems.filter(
67+
item => !existingIds.has(item.id)
68+
);
69+
70+
return {
71+
...prevResult,
72+
customer: {
73+
...prevResult.customer,
74+
wishlist_v2: {
75+
...prevWishlist,
76+
items_v2: {
77+
...prevWishlist.items_v2,
78+
items: [...prevItems, ...uniqueNewItems]
79+
}
80+
}
81+
}
82+
};
83+
}
84+
});
85+
86+
setPage(currentPage);
87+
} catch (error) {
88+
console.error('Error loading more wishlist items:', error);
89+
} finally {
90+
setIsFetchingMore(false);
91+
}
5092
}, [id, fetchMore, page]);
5193

5294
useEffect(() => {
53-
if (itemsCount >= 1 && isOpen === true && !data) {
95+
if (itemsCount >= 1 && isOpen === true && !called) {
5496
fetchWishlistItems();
5597
}
56-
}, [itemsCount, isOpen, fetchWishlistItems, data]);
98+
}, [itemsCount, isOpen, called]);
99+
100+
const items = useMemo(() => {
101+
if (!data || !data.customer || !data.customer.wishlist_v2) {
102+
return [];
103+
}
104+
105+
const allItems = data.customer.wishlist_v2.items_v2?.items || [];
106+
107+
const uniqueItems = [];
108+
const seenIds = new Set();
109+
110+
for (const item of allItems) {
111+
if (!seenIds.has(item.id)) {
112+
seenIds.add(item.id);
113+
uniqueItems.push(item);
114+
}
115+
}
57116

58-
const items =
59-
data && data.customer.wishlist_v2.items_v2.items
60-
? data.customer.wishlist_v2.items_v2.items
61-
: [];
117+
return uniqueItems;
118+
}, [data]);
62119

63120
return {
64121
handleContentToggle,

packages/peregrine/lib/talons/WishlistPage/wishlist.gql.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ export const GET_CUSTOMER_WISHLIST = gql`
1717
`;
1818

1919
export const GET_CUSTOMER_WISHLIST_ITEMS = gql`
20-
query getCustomerWishlist($id: ID!, $currentPage: Int) {
20+
query getCustomerWishlist($id: ID!, $currentPage: Int, $pageSize: Int) {
2121
# eslint-disable-next-line @graphql-eslint/require-id-when-available
2222
customer {
2323
wishlist_v2(id: $id) {
2424
id
25-
items_v2(currentPage: $currentPage) {
25+
items_v2(currentPage: $currentPage, pageSize: $pageSize) {
2626
items {
2727
id
2828
...WishlistItemFragment

0 commit comments

Comments
 (0)