diff --git a/src/Pages/Search.js b/src/Pages/Search.js index 6d88bed07d954a318dc0a56152f8073a8bdb0855..63018200fe7e0b29f9d39bbc1d4799600b17e222 100644 --- a/src/Pages/Search.js +++ b/src/Pages/Search.js @@ -40,7 +40,7 @@ import CollectionCardFunction from "../Components/CollectionCardFunction"; import ContactCard from "../Components/ContactCard"; import CircularProgress from '@material-ui/core/CircularProgress'; import { saveHeaders } from '../Components/HelperFunctions/saveTokens'; - +import getRequest from './HelperFunctions/getAxiosConfig' let order = "review_average"; @@ -77,22 +77,10 @@ export default function Search(props) { ); const [optionResult, setOptionResult] = useState(option); - const collectStuff = (tipoBusca, filtro) => { - if (!loadingMoreData) // this line prevents resetting filter when loading more data - currFilter = filtro; - if (filtro) - setIsFiltering(true); - - /// TODO: FIX this axios call - const axiosConfig = getAxiosConfig(); - axios({ - method: 'get', - url: `${apiUrl}/search?page=${page}&results_per_page=${resultsPerPage}&order=${order}&query=${state.search.query}${currFilter ? currFilter : ""}&search_class=${tipoBusca}`, - headers: axiosConfig.headers - }).then(res => { - if (tipoBusca === "LearningObject") setResultsResource(res.data); - else if (tipoBusca === "Collection") setResultsCollection(res.data); - else if (tipoBusca === "User") setResultsUser(res.data); + function handleSuccessfulGet (data) { + if (tipoBusca === "LearningObject") setResultsResource(data); + else if (tipoBusca === "Collection") setResultsCollection(data); + else if (tipoBusca === "User") setResultsUser(data); setOptionResult(tipoBusca); dispatch({ type: "SAVE_SEARCH", @@ -101,12 +89,22 @@ export default function Search(props) { class: tipoBusca, }, }); - setTotalResults(res.headers["x-total-count"]); + setTotalResults(data.headers["x-total-count"]); setIsLoading(false); setIsFiltering(false); setLoadingMoreData(false); - saveHeaders(res); - }) + } + + const collectStuff = (tipoBusca, filtro) => { + if (!loadingMoreData) // this line prevents resetting filter when loading more data + currFilter = filtro; + if (filtro) + setIsFiltering(true); + + const url = `/search?page=${page}&results_per_page=${resultsPerPage}&order=${order}&query=${state.search.query}${currFilter ? currFilter : ""}&search_class=${tipoBusca}` + + getRequest(url, handleSuccessfulGet, (error) => {console.console.log(error)}) + }; useEffect(() => { @@ -197,7 +195,7 @@ export default function Search(props) { </Grid> </Grid> </HeaderFilters> - + { isloading ? <LoadingSpinner text="Carregando..." /> : optionResult === "Collection" ? ( @@ -228,7 +226,7 @@ export default function Search(props) { <div style={{ display: "flex", flexDirection: "row", justifyContent: "center", }}> <button style={{ height: 36, backgroundColor: "#ff7f00", marginBottom: 50, marginTop: 50, fontSize: 14, - color: "white", borderRadius: 4,border: "none", + color: "white", borderRadius: 4,border: "none", }} onClick={() => { setLoadingMoreData(true);