Skip to content

Commit

Permalink
Remove: unnecessary variables
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagooGomess committed Feb 14, 2022
1 parent 11ee4c0 commit 7c6dd69
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 56 deletions.
6 changes: 0 additions & 6 deletions src/actions/searchOffersActions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { INITIAL_JOB_TYPE, INITIAL_JOB_DURATION, INITIAL_ERROR } from "../reducers/searchOffersReducer";

export const OfferSearchTypes = Object.freeze({
SET_SUBMIT_NUMBER: "SET_SUBMIT_NUMBER",
SET_SEARCH_VALUE: "SET_SEARCH_VALUE",
SET_JOB_DURATION: "SET_JOB_DURATION",
SET_JOB_TYPE: "SET_JOB_TYPE",
Expand All @@ -17,11 +16,6 @@ export const OfferSearchTypes = Object.freeze({
ADMIN_ENABLE_OFFER: "ADMIN_ENABLE_OFFER",
});

export const setSubmitNumber = (submitNumber) => ({
type: OfferSearchTypes.SET_SUBMIT_NUMBER,
submitNumber,
});

export const setLoadingOffers = (loading) => ({
type: OfferSearchTypes.SET_OFFERS_LOADING,
loading,
Expand Down
23 changes: 12 additions & 11 deletions src/components/HomePage/SearchArea/SearchArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
setFields,
setShowJobDurationSlider,
setTechs,
setSubmitNumber,
} from "../../../actions/searchOffersActions";
import { INITIAL_JOB_TYPE, INITIAL_JOB_DURATION } from "../../../reducers/searchOffersReducer";
import { SearchResultsConstants } from "../SearchResultsArea/SearchResultsWidget/SearchResultsUtils";

import { Paper } from "@material-ui/core";
import SearchBar from "./SearchBar";
Expand All @@ -33,7 +33,6 @@ export const AdvancedSearchController = ({
enableAdvancedSearchDefault, showJobDurationSlider, setShowJobDurationSlider, jobMinDuration,
jobMaxDuration, setJobDuration, jobType, setJobType, fields, setFields, technologies, setTechs,
resetAdvancedSearchFields, onSubmit, searchValue, setSearchValue, searchOffers, onMobileClose,
submitNumber, setSubmitNumber,
}) => {

const advancedSearchProps = useAdvancedSearch({
Expand All @@ -54,12 +53,18 @@ export const AdvancedSearchController = ({

const submitForm = useCallback((e) => {
if (e) e.preventDefault();
searchOffers({ value: searchValue, jobMinDuration, jobMaxDuration, jobType, fields, technologies });
setSubmitNumber(submitNumber + 1);

searchOffers({
value: searchValue,
jobMinDuration,
jobMaxDuration,
jobType,
fields,
technologies,
limit: SearchResultsConstants.initialLimit,
});

if (onSubmit) onSubmit();
}, [fields, jobMaxDuration, jobMinDuration, jobType, onSubmit, searchOffers, searchValue, setSubmitNumber, submitNumber, technologies]);
}, [fields, jobMaxDuration, jobMinDuration, jobType, onSubmit, searchOffers, searchValue, technologies]);

return {
...advancedSearchProps,
Expand All @@ -76,7 +81,7 @@ export const AdvancedSearchController = ({
};
};

export const SearchArea = ({ onSubmit, searchOffers, searchValue, submitNumber, setSubmitNumber,
export const SearchArea = ({ onSubmit, searchOffers, searchValue,
jobMinDuration = INITIAL_JOB_DURATION, jobMaxDuration = INITIAL_JOB_DURATION + 1, jobType = INITIAL_JOB_TYPE,
fields, technologies, showJobDurationSlider, setShowJobDurationSlider, advanced: enableAdvancedSearchDefault = false,
setSearchValue, setJobDuration, setJobType, setFields, setTechs, resetAdvancedSearchFields, onMobileClose }) => {
Expand All @@ -95,7 +100,6 @@ export const SearchArea = ({ onSubmit, searchOffers, searchValue, submitNumber,
enableAdvancedSearchDefault, showJobDurationSlider, setShowJobDurationSlider, jobMinDuration,
jobMaxDuration, setJobDuration, jobType, setJobType, fields, setFields, technologies, setTechs,
resetAdvancedSearchFields, onSubmit, searchValue, setSearchValue, searchOffers, onMobileClose,
submitNumber, setSubmitNumber,
},
AdvancedSearchControllerContext
);
Expand Down Expand Up @@ -144,7 +148,6 @@ SearchArea.propTypes = {
jobMinDuration: PropTypes.number,
jobMaxDuration: PropTypes.number,
jobType: PropTypes.string,
setSubmitNumber: PropTypes.func,
setSearchValue: PropTypes.func.isRequired,
setJobDuration: PropTypes.func.isRequired,
setJobType: PropTypes.func.isRequired,
Expand All @@ -160,7 +163,6 @@ SearchArea.propTypes = {
};

export const mapStateToProps = ({ offerSearch }) => ({
submitNumber: offerSearch.submitNumber,
searchValue: offerSearch.searchValue,
jobType: offerSearch.jobType,
jobMinDuration: offerSearch.jobDuration[0],
Expand All @@ -172,7 +174,6 @@ export const mapStateToProps = ({ offerSearch }) => ({

export const mapDispatchToProps = (dispatch) => ({
searchOffers: (filters) => dispatch(searchOffers(filters)),
setSubmitNumber: (submitNumber) => dispatch((setSubmitNumber(submitNumber))),
setSearchValue: (value) => dispatch(setSearchValue(value)),
setJobDuration: (_, value) => dispatch(setJobDuration(...value)),
setJobType: (e) => dispatch(setJobType(e.target.value)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import useSearchResultsWidgetStyles from "./searchResultsWidgetStyles";
import { Tune } from "@material-ui/icons";
import clsx from "clsx";
import LoadingOfferItem from "./LoadingOfferItem";
import useLoadMoreOffers from "../../../../hooks/useLoadMoreOffers";
import useLoadMoreOffers from "./useLoadMoreOffers";
import { connect } from "react-redux";
import { addSnackbar } from "../../../../actions/notificationActions";
import { SearchResultsConstants } from "./SearchResultsUtils";

const useAdvancedSearchButtonStyles = makeStyles((theme) => ({
root: {
Expand Down Expand Up @@ -71,10 +72,6 @@ const OfferItemsContainer = ({
if (node) setLastOfferNode(node);
}, []);

useEffect(() => {
setOffset(offers?.length);
}, [offers]);

useEffect(() => {

if (loading || infiniteScrollLoading) {
Expand All @@ -93,7 +90,7 @@ const OfferItemsContainer = ({
if (observer.current) observer.current.disconnect();
observer.current = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting && hasMore) {
setOffset((previousOffset) => previousOffset + 5);
setOffset((previousOffset) => previousOffset + SearchResultsConstants.fetchNewOffersLimit);
setFetchMoreOffers(true);
} else {
setFetchMoreOffers(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const SearchResultsConstants = {
initialLimit: 5,
fetchNewOffersLimit: 5,
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { useEffect, useState } from "react";
import { useSelector, useDispatch } from "react-redux";

import {
setSearchOffers,
} from "../actions/searchOffersActions";
import Offer from "../components/HomePage/SearchResultsArea/Offer/Offer";
import { parseFiltersToURL } from "../utils";
import config from "../config";
import ErrorTypes from "../utils/ErrorTypes";
import { setSearchOffers } from "../../../../actions/searchOffersActions";
import Offer from "../Offer/Offer";
import { parseFiltersToURL } from "../../../../utils";
import config from "../../../../config";
import ErrorTypes from "../../../../utils/ErrorTypes";
import { SearchResultsConstants } from "./SearchResultsUtils";

const { API_HOSTNAME } = config;

Expand All @@ -25,31 +24,27 @@ export default ({ offset, setOffset, fetchMoreOffers }) => {

const filters = {
offset,
limit: 5,
limit: SearchResultsConstants.fetchNewOffersLimit,
...offerSearch,
};

const oldOffers = useSelector((state) => state.offerSearch.offers);
const offers = useSelector((state) => state.offerSearch.offers);
const initialOffersLoading = useSelector((state) => state.offerSearch.loading);
const submitNumber = useSelector((state) => state.offerSearch.submitNumber);

const [hasMore, setHasMore] = useState(true);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const [offers, setOffers] = useState(oldOffers);
const [fetchedOffsets, setFetchedOffsets] = useState([]);

useEffect(() => {
setOffers(oldOffers);
}, [oldOffers]);

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

useEffect(() => {

Expand All @@ -72,17 +67,16 @@ export default ({ offset, setOffset, fetchMoreOffers }) => {
}
const offersData = await res.json();

const oldOfferIds = [...oldOffers.map((oldOffer) => oldOffer._id)];
const newOffers = [...oldOffers];
const offerIds = [...offers.map((offer) => offer._id)];
const newOffers = [...offers];

offersData.forEach((offerData) => {
if (!oldOfferIds.includes(offerData._id))
if (!offerIds.includes(offerData._id))
newOffers.push(new Offer(offerData));
});

setHasMore(offersData.length > 0);
dispatch(setSearchOffers(newOffers));
setOffers(newOffers);
setLoading(false);
setError(null);

Expand All @@ -106,8 +100,8 @@ export default ({ offset, setOffset, fetchMoreOffers }) => {
}

}, [
dispatch, fetchMoreOffers, filters, oldOffers, initialOffersLoading, offset,
fetchedOffsets, submitNumber, hasMore, loading, offers,
dispatch, fetchMoreOffers, filters, initialOffersLoading,
fetchedOffsets, loading, offers,
]);

return { offers, hasMore, loading, error };
Expand Down
6 changes: 0 additions & 6 deletions src/reducers/searchOffersReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const JOB_MIN_DURATION = 1;
export const JOB_MAX_DURATION = 12;

const initialState = {
submitNumber: 0,
searchValue: "",
jobType: INITIAL_JOB_TYPE,
filterJobDuration: false,
Expand All @@ -23,11 +22,6 @@ const initialState = {
export default (state = initialState, action) => {

switch (action.type) {
case OfferSearchTypes.SET_SUBMIT_NUMBER:
return {
...state,
submitNumber: action.submitNumber,
};
case OfferSearchTypes.SET_OFFERS_SEARCH_RESULT:
return {
...state,
Expand Down

0 comments on commit 7c6dd69

Please sign in to comment.