Skip to content

Commit 77c4c18

Browse files
del15881del22123
andauthored
PWA-3613: RI-Wishlist pagination fix (#4562)
* PWA-3613: RI-Wishlist pagination fix * PWA-3613:Fixed lint and prettier issues --------- Co-authored-by: Bharathidasan Elangovan <del22123@adobe.com>
1 parent 9c1da8d commit 77c4c18

File tree

5 files changed

+81
-39
lines changed

5 files changed

+81
-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 & 16 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) {
@@ -56,14 +46,10 @@ export const useCustomerWishlistSkus = (props = {}) => {
5646
}
5747
});
5848
}
59-
60-
if (shouldFetchMore) {
61-
setCurrentPage(current => ++current);
62-
}
6349
},
6450
skip: !isSignedIn,
6551
variables: {
66-
currentPage
52+
currentPage: 1
6753
}
6854
});
6955
};

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: 76 additions & 17 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, useRef, useState } from 'react';
22
import { useLazyQuery } from '@apollo/client';
33
import mergeOperations from '../../util/shallowMerge';
44
import defaultOperations from './wishlist.gql';
@@ -17,15 +17,17 @@ export const useWishlist = (props = {}) => {
1717
const [page, setPage] = useState(1);
1818
const [isOpen, setIsOpen] = useState(!isCollapsed);
1919
const [isFetchingMore, setIsFetchingMore] = useState(false);
20+
const hasFetchedRef = useRef(false);
2021

2122
const [fetchWishlistItems, queryResult] = useLazyQuery(
2223
operations.getCustomerWishlistItems,
2324
{
24-
fetchPolicy: 'cache-and-network',
25+
fetchPolicy: 'cache-first',
2526
nextFetchPolicy: 'cache-first',
2627
variables: {
2728
id,
28-
currentPage: 1
29+
currentPage: 1,
30+
pageSize: 20
2931
}
3032
}
3133
);
@@ -38,27 +40,84 @@ export const useWishlist = (props = {}) => {
3840
const handleLoadMore = useCallback(async () => {
3941
setIsFetchingMore(true);
4042
const currentPage = page + 1;
41-
await fetchMore({
42-
variables: {
43-
id,
44-
currentPage
45-
}
46-
});
4743

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+
}
5093
}, [id, fetchMore, page]);
5194

5295
useEffect(() => {
53-
if (itemsCount >= 1 && isOpen === true && !data) {
96+
if (itemsCount >= 1 && isOpen === true && !hasFetchedRef.current) {
97+
hasFetchedRef.current = true;
5498
fetchWishlistItems();
5599
}
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+
}
57118

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]);
62121

63122
return {
64123
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)