Skip to content

Commit

Permalink
Update: offer search offset logic to queryToken
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagooGomess committed Mar 24, 2022
1 parent 5ba7b6c commit 6cc22a6
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 30 deletions.
6 changes: 6 additions & 0 deletions src/actions/searchOffersActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const OfferSearchTypes = Object.freeze({
SET_JOB_FIELDS: "SET_JOB_FIELDS",
SET_JOB_TECHS: "SET_JOB_TECHS",
SET_OFFERS_SEARCH_RESULT: "SET_OFFERS_SEARCH_RESULT",
SET_SEARCH_QUERY_TOKEN: "SET_SEARCH_QUERY_TOKEN",
SET_OFFERS_LOADING: "SET_OFFERS_LOADING",
SET_SEARCH_OFFERS_ERROR: "SET_SEARCH_OFFERS_ERROR",
SET_JOB_DURATION_TOGGLE: "SET_JOB_DURATION_TOGGLE",
Expand All @@ -26,6 +27,11 @@ export const setSearchOffers = (offers) => ({
offers,
});

export const setSearchQueryToken = (queryToken) => ({
type: OfferSearchTypes.SET_SEARCH_QUERY_TOKEN,
queryToken,
});

export const setOffersFetchError = (error) => ({
type: OfferSearchTypes.SET_SEARCH_OFFERS_ERROR,
error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import OfferItem from "../Offer/OfferItem";
import useSearchResultsWidgetStyles from "./searchResultsWidgetStyles";
import LoadingOfferItem from "./LoadingOfferItem";
import { addSnackbar } from "../../../../actions/notificationActions";
import { SearchResultsConstants } from "./SearchResultsUtils";
import Offer from "../Offer/Offer";

const useAdvancedSearchButtonStyles = makeStyles((theme) => ({
Expand Down Expand Up @@ -53,7 +52,6 @@ const OfferItemsContainer = ({
showSearchFilters,
toggleShowSearchFilters,
offers,
setOfferOffset,
setShouldFetchMoreOffers,
hasMoreOffers,
infiniteScrollLoading,
Expand Down Expand Up @@ -86,7 +84,6 @@ const OfferItemsContainer = ({
if (observer.current) observer.current.disconnect();
observer.current = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting && hasMoreOffers) {
setOfferOffset((previousOffset) => previousOffset + SearchResultsConstants.fetchNewOffersLimit);
setShouldFetchMoreOffers(true);
} else {
setShouldFetchMoreOffers(false);
Expand All @@ -95,7 +92,7 @@ const OfferItemsContainer = ({
if (lastOfferNode) observer.current.observe(lastOfferNode);
}, [
addSnackbar, hasMoreOffers, infiniteScrollError, infiniteScrollLoading,
lastOfferNode, loading, setOfferOffset, setShouldFetchMoreOffers,
lastOfferNode, loading, setShouldFetchMoreOffers,
]);

const handleOfferSelection = (...args) => {
Expand Down Expand Up @@ -150,7 +147,6 @@ OfferItemsContainer.propTypes = {
showSearchFilters: PropTypes.bool,
toggleShowSearchFilters: PropTypes.func.isRequired,
offers: PropTypes.arrayOf(PropTypes.instanceOf(Offer)),
setOfferOffset: PropTypes.func,
setShouldFetchMoreOffers: PropTypes.func,
hasMoreOffers: PropTypes.bool,
infiniteScrollLoading: PropTypes.bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { SearchResultsControllerContext } from "./SearchResultsWidget";

const OffersList = ({
noOffers, classes, selectedOfferIdx, offersLoading, setSelectedOfferIdx,
showSearchFilters, toggleShowSearchFilters, offers, setOfferOffset,
setShouldFetchMoreOffers, hasMoreOffers, infiniteScrollLoading, infiniteScrollError,
showSearchFilters, toggleShowSearchFilters, offers, setShouldFetchMoreOffers,
hasMoreOffers, infiniteScrollLoading, infiniteScrollError,
}) => (
<Grid item md={4} id="offer_list" className={classes.fullHeight}>
<Grid container className={classes.fullHeight}>
Expand All @@ -32,7 +32,6 @@ const OffersList = ({
showSearchFilters={showSearchFilters}
toggleShowSearchFilters={toggleShowSearchFilters}
offers={offers}
setOfferOffset={setOfferOffset}
setShouldFetchMoreOffers={setShouldFetchMoreOffers}
hasMoreOffers={hasMoreOffers}
infiniteScrollLoading={infiniteScrollLoading}
Expand Down Expand Up @@ -64,7 +63,6 @@ OffersList.propTypes = {
showSearchFilters: PropTypes.bool.isRequired,
toggleShowSearchFilters: PropTypes.func.isRequired,
offers: PropTypes.arrayOf(PropTypes.instanceOf(Offer)),
setOfferOffset: PropTypes.func,
setShouldFetchMoreOffers: PropTypes.func,
hasMoreOffers: PropTypes.bool,
infiniteScrollLoading: PropTypes.bool,
Expand Down Expand Up @@ -136,7 +134,6 @@ const SearchResultsDesktop = () => {
handleAdminEnableOffer,
showSearchFilters,
toggleShowSearchFilters,
setOfferOffset,
setShouldFetchMoreOffers,
hasMoreOffers,
infiniteScrollLoading,
Expand Down Expand Up @@ -171,7 +168,6 @@ const SearchResultsDesktop = () => {
showSearchFilters={showSearchFilters}
toggleShowSearchFilters={toggleShowSearchFilters}
offers={offers}
setOfferOffset={setOfferOffset}
setShouldFetchMoreOffers={setShouldFetchMoreOffers}
hasMoreOffers={hasMoreOffers}
infiniteScrollLoading={infiniteScrollLoading}
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,
setOfferOffset, setShouldFetchMoreOffers, hasMoreOffers, infiniteScrollLoading, infiniteScrollError,
setShouldFetchMoreOffers, hasMoreOffers, infiniteScrollLoading, infiniteScrollError,
}) => (
<Grid container className={classes.fullHeight}>
<Grid xs={12} item className={classes.offerItemsContainer}>
Expand All @@ -31,7 +31,6 @@ const OffersList = ({
showSearchFilters={showSearchFilters}
toggleShowSearchFilters={toggleShowSearchFilters}
offers={offers}
setOfferOffset={setOfferOffset}
setShouldFetchMoreOffers={setShouldFetchMoreOffers}
hasMoreOffers={hasMoreOffers}
infiniteScrollLoading={infiniteScrollLoading}
Expand All @@ -56,7 +55,6 @@ OffersList.propTypes = {
showSearchFilters: PropTypes.bool.isRequired,
toggleShowSearchFilters: PropTypes.func.isRequired,
offers: PropTypes.arrayOf(PropTypes.instanceOf(Offer)),
setOfferOffset: PropTypes.func,
setShouldFetchMoreOffers: PropTypes.func,
hasMoreOffers: PropTypes.bool,
infiniteScrollLoading: PropTypes.bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const SearchResultsController = ({
}) => {

const [selectedOfferIdx, setSelectedOfferIdx] = useState(null);
const [offerOffset, setOfferOffset] = useState(0);
const [shouldFetchMoreOffers, setShouldFetchMoreOffers] = useState(false);

// Reset the selected offer on every "loading", so that it does not show up after finished loading
Expand All @@ -49,7 +48,7 @@ const SearchResultsController = ({
hasMoreOffers,
loading: infiniteScrollLoading,
error: infiniteScrollError,
} = useLoadMoreOffers({ offerOffset, setOfferOffset, shouldFetchMoreOffers });
} = useLoadMoreOffers({ shouldFetchMoreOffers });

const handleDisableOffer = useCallback(({ offer, adminReason, onSuccess, onError }) => {
disableOfferService(offer._id, adminReason).then(() => {
Expand Down Expand Up @@ -121,7 +120,6 @@ const SearchResultsController = ({
handleAdminEnableOffer,
showSearchFilters,
toggleShowSearchFilters,
setOfferOffset,
setShouldFetchMoreOffers,
hasMoreOffers,
infiniteScrollLoading,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from "react";
import { useSelector, useDispatch } from "react-redux";

import { setSearchOffers } from "../../../../actions/searchOffersActions";
import { setSearchOffers, setSearchQueryToken } from "../../../../actions/searchOffersActions";
import Offer from "../Offer/Offer";
import { parseFiltersToURL } from "../../../../utils";
import config from "../../../../config";
Expand All @@ -10,10 +10,11 @@ import { SearchResultsConstants } from "./SearchResultsUtils";

const { API_HOSTNAME } = config;

export default ({ offerOffset, setOfferOffset, shouldFetchMoreOffers }) => {
export default ({ shouldFetchMoreOffers }) => {

const dispatch = useDispatch();
const offerSearch = useSelector(({ offerSearch }) => ({
queryToken: offerSearch.queryToken,
value: offerSearch.searchValue,
jobType: offerSearch.jobType,
jobMinDuration: offerSearch.jobDuration[0],
Expand All @@ -23,7 +24,6 @@ export default ({ offerOffset, setOfferOffset, shouldFetchMoreOffers }) => {
}));

const filters = {
offset: offerOffset,
limit: SearchResultsConstants.fetchNewOffersLimit,
...offerSearch,
};
Expand All @@ -34,24 +34,20 @@ export default ({ offerOffset, setOfferOffset, shouldFetchMoreOffers }) => {
const [hasMoreOffers, setHasMoreOffers] = useState(true);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const [fetchedOffsets, setFetchedOffsets] = useState([]);

useEffect(() => {
if (initialOffersLoading) {
setOfferOffset(0);
setHasMoreOffers(true);
setError(null);
setLoading(false);
setFetchedOffsets([]);
}
}, [setOfferOffset, initialOffersLoading]);
}, [initialOffersLoading]);

useEffect(() => {

const fetchOffers = async () => {

try {
setFetchedOffsets((offsets) => [...offsets, filters.offset]);
setLoading(true);
const query = parseFiltersToURL(filters);
const res = await fetch(`${API_HOSTNAME}/offers?${query}`, {
Expand All @@ -66,7 +62,9 @@ export default ({ offerOffset, setOfferOffset, shouldFetchMoreOffers }) => {
setLoading(false);
return;
}
const offersData = await res.json();
const data = await res.json();
const offersData = data.results;
const queryToken = data.queryToken;

const offerIds = [...offers.map((offer) => offer._id)];
const newOffers = [...offers];
Expand All @@ -78,6 +76,7 @@ export default ({ offerOffset, setOfferOffset, shouldFetchMoreOffers }) => {

setHasMoreOffers(offersData.length > 0);
dispatch(setSearchOffers(newOffers));
dispatch(setSearchQueryToken(queryToken));
setLoading(false);
setError(null);

Expand All @@ -91,7 +90,7 @@ export default ({ offerOffset, setOfferOffset, shouldFetchMoreOffers }) => {
}
};

if (shouldFetchMoreOffers && !fetchedOffsets.includes(filters.offset) && !initialOffersLoading && !loading) {
if (shouldFetchMoreOffers && !initialOffersLoading && !loading) {
fetchOffers().catch((error) => {
setError({
cause: ErrorTypes.UNEXPECTED,
Expand All @@ -102,7 +101,7 @@ export default ({ offerOffset, setOfferOffset, shouldFetchMoreOffers }) => {

}, [
dispatch, shouldFetchMoreOffers, filters, initialOffersLoading,
fetchedOffsets, loading, offers,
loading, offers,
]);

return { offers, hasMoreOffers, loading, error };
Expand Down
5 changes: 5 additions & 0 deletions src/reducers/searchOffersReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export default (state = initialState, action) => {
...state,
offers: action.offers,
};
case OfferSearchTypes.SET_SEARCH_QUERY_TOKEN:
return {
...state,
queryToken: action.queryToken,
};
case OfferSearchTypes.SET_OFFERS_LOADING:
return {
...state,
Expand Down
13 changes: 11 additions & 2 deletions src/services/offerService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { setLoadingOffers, setSearchOffers, setOffersFetchError, resetOffersFetchError } from "../actions/searchOffersActions";
import {
setLoadingOffers,
setSearchOffers,
setSearchQueryToken,
setOffersFetchError,
resetOffersFetchError,
} from "../actions/searchOffersActions";
import Offer from "../components/HomePage/SearchResultsArea/Offer/Offer";
import config from "../config";
import { parseFiltersToURL, buildCancelableRequest } from "../utils";
Expand Down Expand Up @@ -38,8 +44,11 @@ export const searchOffers = (filters) => buildCancelableRequest(
));
return;
}
const offers = await res.json();
const data = await res.json();
const offers = data.results;
const queryToken = data.queryToken;
dispatch(setSearchOffers(offers.map((offerData) => new Offer(offerData))));
dispatch(setSearchQueryToken(queryToken));
dispatch(setLoadingOffers(false));

sendSearchReport(filters, `/offers?${query}`);
Expand Down

0 comments on commit 6cc22a6

Please sign in to comment.