Skip to content

Commit

Permalink
IBX-8195: Content items are displayed multiple times in UDW after add…
Browse files Browse the repository at this point in the history
…ing item to bookmarks (#1265)
  • Loading branch information
GrabowskiM authored Jun 10, 2024
1 parent 57149fd commit fc50f29
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@
display: flex;
align-items: center;
justify-content: center;
margin-top: calculateRem(50px);
margin: calculateRem(50px) 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const FinderBranch = ({ locationData, itemsPerPage }) => {
const [sortOrder] = useContext(SortOrderContext);
const contentTypesMap = useContext(ContentTypesMapContext);
const [markedLocationId] = useContext(MarkedLocationIdContext);
const totalCount = useRef(0);
const branchRef = useRef(null);
const sortingOptions = SORTING_OPTIONS.find((option) => option.sortClause === sorting);
const [loadedLocations, isLoading] = useFindLocationsByParentLocationIdFetch(
Expand All @@ -40,14 +39,14 @@ const FinderBranch = ({ locationData, itemsPerPage }) => {
let resizeStartPositionX = 0;
let branchCurrentWidth = 0;
const loadMore = ({ target }) => {
const areAllItemsLoaded = locationData.subitems.length >= totalCount.current;
const areAllItemsLoaded = locationData.subitems.length >= locationData.totalCount;
const isOffsetReached = target.scrollHeight - target.clientHeight - target.scrollTop < SCROLL_OFFSET;

if (areAllItemsLoaded || !isOffsetReached || isLoading) {
return;
}

setOffset(offset + itemsPerPage);
setOffset(Math.min(offset + itemsPerPage, locationData.totalCount));
};
const expandBranch = () => {
dispatchLoadedLocationsAction({ type: 'UPDATE_LOCATIONS', data: { ...locationData, collapsed: false } });
Expand Down Expand Up @@ -113,11 +112,11 @@ const FinderBranch = ({ locationData, itemsPerPage }) => {
return (
<Fragment>
<div className="c-finder-branch__items-wrapper" onScroll={loadMore} style={{ width }}>
{renderLoadingSpinner()}

{subitems.map(({ location }) => (
<FinderLeaf key={location.id} location={location} />
))}

{renderLoadingSpinner()}
</div>
{renderDragHandler()}
</Fragment>
Expand All @@ -137,14 +136,12 @@ const FinderBranch = ({ locationData, itemsPerPage }) => {

useEffect(() => {
setOffset(0);
totalCount.current = 0;
}, [sortingOptions.sortClause, sortOrder]);

useEffect(() => {
if (loadedLocations.subitems) {
const data = { ...locationData, ...loadedLocations, subitems: [...locationData.subitems, ...loadedLocations.subitems] };

totalCount.current = loadedLocations.totalCount;
dispatchLoadedLocationsAction({ type: 'UPDATE_LOCATIONS', data });
}
}, [loadedLocations, dispatchLoadedLocationsAction, isLoading]);
Expand Down Expand Up @@ -172,6 +169,7 @@ FinderBranch.propTypes = {
parentLocationId: PropTypes.number.isRequired,
subitems: PropTypes.array.isRequired,
location: PropTypes.object.isRequired,
totalCount: PropTypes.number.isRequired,
collapsed: PropTypes.bool,
}).isRequired,
itemsPerPage: PropTypes.number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,15 @@ export const useFindLocationsByParentLocationIdFetch = (locationData, { sortClau

let effectCleaned = false;

if (state.dataFetched) {
if (
!locationData.parentLocationId ||
locationData.collapsed ||
locationData.subitems.length >= locationData.totalCount ||
locationData.subitems.length >= limit + offset
) {
dispatch({ type: 'FETCH_END', data: {} });
if (
!locationData.parentLocationId ||
locationData.collapsed ||
locationData.subitems.length >= locationData.totalCount ||
locationData.subitems.length >= limit + offset
) {
dispatch({ type: 'FETCH_END', data: state.data });

return;
}
return;
}

dispatch({ type: 'FETCH_START' });
Expand Down

0 comments on commit fc50f29

Please sign in to comment.