Skip to content

Commit

Permalink
Fix: load more offers in search bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagooGomess committed Mar 26, 2022
1 parent b564f32 commit c12ecd1
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import OfferItem from "../Offer/OfferItem";
import useSearchResultsWidgetStyles from "./searchResultsWidgetStyles";
import LoadingOfferItem from "./LoadingOfferItem";
import Offer from "../Offer/Offer";
import { SearchResultsConstants } from "./SearchResultsUtils";

const useAdvancedSearchButtonStyles = makeStyles((theme) => ({
root: {
Expand Down Expand Up @@ -55,6 +56,7 @@ const OfferItemsContainer = ({
offers,
moreOffersLoading,
loadMoreOffers,
searchQueryToken,
}) => {
const classes = useSearchResultsWidgetStyles();

Expand All @@ -73,10 +75,10 @@ const OfferItemsContainer = ({

if (observer.current) observer.current.disconnect();
observer.current = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting) loadMoreOffers();
if (entries[0].isIntersecting) loadMoreOffers(searchQueryToken, SearchResultsConstants.FETCH_NEW_OFFERS_LIMIT);
});
if (lastOfferNode) observer.current.observe(lastOfferNode);
}, [initialOffersLoading, lastOfferNode, loadMoreOffers, moreOffersLoading]);
}, [initialOffersLoading, lastOfferNode, loadMoreOffers, moreOffersLoading, searchQueryToken]);

const handleOfferSelection = (...args) => {
toggleShowSearchFilters(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { SearchResultsControllerContext } from "./SearchResultsWidget";

const OffersList = ({
noOffers, classes, selectedOfferIdx, offersLoading, setSelectedOfferIdx,
showSearchFilters, toggleShowSearchFilters, offers, moreOffersLoading, loadMoreOffers,
showSearchFilters, toggleShowSearchFilters, offers, moreOffersLoading, loadMoreOffers, searchQueryToken,
}) => (
<Grid item md={4} id="offer_list" className={classes.fullHeight}>
<Grid container className={classes.fullHeight}>
Expand All @@ -33,6 +33,7 @@ const OffersList = ({
offers={offers}
moreOffersLoading={moreOffersLoading}
loadMoreOffers={loadMoreOffers}
searchQueryToken={searchQueryToken}
/>
}
<Divider
Expand Down Expand Up @@ -129,6 +130,7 @@ const SearchResultsDesktop = () => {
toggleShowSearchFilters,
moreOffersLoading,
loadMoreOffers,
searchQueryToken,
} = useContext(SearchResultsControllerContext);
const classes = useSearchResultsWidgetStyles();

Expand Down Expand Up @@ -161,6 +163,7 @@ const SearchResultsDesktop = () => {
offers={offers}
moreOffersLoading={moreOffersLoading}
loadMoreOffers={loadMoreOffers}
searchQueryToken={searchQueryToken}
/>
{showSearchFilters ?
<Grid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { SearchResultsControllerContext } from "./SearchResultsWidget";

const OffersList = ({
noOffers, classes, offersLoading, showOfferDetails, showSearchFilters,
toggleShowSearchFilters, offers, moreOffersLoading, loadMoreOffers,
toggleShowSearchFilters, offers, moreOffersLoading, loadMoreOffers, searchQueryToken,
}) => (
<Grid container className={classes.fullHeight}>
<Grid xs={12} item className={classes.offerItemsContainer}>
Expand All @@ -33,6 +33,7 @@ const OffersList = ({
offers={offers}
moreOffersLoading={moreOffersLoading}
loadMoreOffers={loadMoreOffers}
searchQueryToken={searchQueryToken}
/>
}
</Grid>
Expand Down Expand Up @@ -126,6 +127,7 @@ const SearchResultsMobile = () => {
toggleShowSearchFilters,
moreOffersLoading,
loadMoreOffers,
searchQueryToken,
} = useContext(SearchResultsControllerContext);

const showOfferDetails = (offerIdx) => {
Expand Down Expand Up @@ -156,6 +158,7 @@ const SearchResultsMobile = () => {
offers={offers}
moreOffersLoading={moreOffersLoading}
loadMoreOffers={loadMoreOffers}
searchQueryToken={searchQueryToken}
/>
{showSearchFilters ?
<SearchArea
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const SearchResultsController = ({
showSearchFilters,
toggleShowSearchFilters,
moreOffersLoading,
searchQueryToken,
loadMoreOffers: () => loadMoreOffers(searchQueryToken, SearchResultsConstants.FETCH_NEW_OFFERS_LIMIT),
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default (filters) => {
}

try {
const { queryToken: updatedQueryToken, results } = await searchOffers({ queryToken, limit, ...filters });
const { updatedQueryToken, results } = await searchOffers({ queryToken, limit, ...filters });
dispatch(setSearchQueryToken((updatedQueryToken)));
dispatch(setSearchOffers(results, !isInitialRequest));

Expand Down Expand Up @@ -71,7 +71,7 @@ export default (filters) => {

return {
search: (...args) => loadOffers(true)(null, ...args),
loadMoreOffers: loadOffers(false),
loadMoreOffers: (queryToken, ...args) => loadOffers(false)(queryToken, ...args),
moreOffersLoading,
moreOffersFetchError,
};
Expand Down
2 changes: 1 addition & 1 deletion src/reducers/searchOffersReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default (state = initialState, action) => {
{
let offers = action.offers;
if (action.accumulate) {
offers = [...state.offers, offers];
offers = [...state.offers, ...offers];
}
return {
...state,
Expand Down
4 changes: 2 additions & 2 deletions src/services/offerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export class NetworkFailureException extends Error {
}

export const searchOffers = buildCancelableRequest(
measureTime(TIMED_ACTIONS.OFFER_SEARCH, async ({ queryToken, ...filters }, { signal }) => {
measureTime(TIMED_ACTIONS.OFFER_SEARCH, async ({ queryToken, limit, ...filters }, { signal }) => {
let isErrorRegistered = false;
try {
const query = queryToken ? `queryToken=${queryToken}` : parseFiltersToURL(filters);
const query = parseFiltersToURL(queryToken ? { queryToken, limit } : { ...filters, limit });
const res = await fetch(`${API_HOSTNAME}/offers?${query}`, {
method: "GET",
credentials: "include",
Expand Down

0 comments on commit c12ecd1

Please sign in to comment.