diff --git a/client/src/hooks/getMentions.js b/client/src/hooks/getMentions.js index 6d118d7..830d5d0 100644 --- a/client/src/hooks/getMentions.js +++ b/client/src/hooks/getMentions.js @@ -1,6 +1,6 @@ import axios from "axios"; -export const getMentions = async (dispatch, keyword, platforms, page = 1) => { +export const getMentions = async (dispatch, keyword, platforms, page, sort) => { const platformsArray = Object.keys(platforms).filter((key) => platforms[key]); const platformsString = platformsArray.join(); @@ -8,7 +8,7 @@ export const getMentions = async (dispatch, keyword, platforms, page = 1) => { try { const url = `/api/mentions?platforms=${platformsString}${ keyword ? "&keyword=" + keyword : "" - }&page=${page}`; + }&page=${page}&sort=${sort}`; const res = await axios.get(url); return res.data; diff --git a/client/src/layout/Scroller.js b/client/src/layout/Scroller.js new file mode 100644 index 0000000..39d0f1e --- /dev/null +++ b/client/src/layout/Scroller.js @@ -0,0 +1,45 @@ +import React, { useContext, useEffect } from "react"; +import InfiniteScroll from "react-infinite-scroller"; +import { UserContext } from "../context/user"; +import { getMentions } from "../hooks/getMentions"; +import Spinner from "./Spinner"; +import Mention from "../layout/Mention"; + +const Scroller = ({ + sort, + error, + loadmore, + hasMore, + setHasMore, + mentionDatas, + setMentionDatas, +}) => { + const { dispatch, searchTerm, user } = useContext(UserContext); + + useEffect(() => { + getMentions(dispatch, searchTerm, user.platforms, 1, sort) + .then((data) => { + setMentionDatas(data.mentions); + setHasMore(data.nextPage ? true : false); + }) + .catch((err) => alert("Cookie expired. Please log in again")); + }, [sort]); + + return ( + }> + {!error && mentionDatas.length > 0 ? ( + mentionDatas.map((mentionData) => ( + + )) + ) : ( +
No results found
+ )} +
+ ); +}; + +export default Scroller; diff --git a/client/src/layout/SortToggle.js b/client/src/layout/SortToggle.js new file mode 100644 index 0000000..437716f --- /dev/null +++ b/client/src/layout/SortToggle.js @@ -0,0 +1,22 @@ +import React from "react"; +import ToggleButton from "@material-ui/lab/ToggleButton"; +import ToggleButtonGroup from "@material-ui/lab/ToggleButtonGroup"; + +const SortToggle = ({ handleAlignment, alignment }) => { + return ( + + + Most Recent + + + Most Popular + + + ); +}; + +export default SortToggle; diff --git a/client/src/pages/HomePage.js b/client/src/pages/HomePage.js index 0fca6fd..7a5f126 100644 --- a/client/src/pages/HomePage.js +++ b/client/src/pages/HomePage.js @@ -2,8 +2,8 @@ import React, { useContext, useEffect, useState } from "react"; import AppBarLoggedIn from "../layout/AppBarLoggedIn"; import { Grid, Typography, Box } from "@material-ui/core"; import { UserContext } from "../context/user"; -import Mention from "../layout/Mention"; import MentionList from "../layout/MentionList"; +import SortToggle from "../layout/SortToggle"; import { makeStyles } from "@material-ui/core/styles"; import Spinner from "../layout/Spinner"; import { getMentions } from "../hooks/getMentions"; @@ -21,7 +21,10 @@ const HomePage = () => { const classes = useStyles(); const [hasMore, setHasMore] = useState(true); const [mentionDatas, setMentionDatas] = useState(null); + const [switching, setSwitching] = useState(false); const [currentPage, setCurrentPage] = useState(1); + const [sort, setSort] = useState("date"); + const { dispatch, error, searchTerm, user } = useContext(UserContext); const loadMore = async () => { @@ -29,7 +32,8 @@ const HomePage = () => { dispatch, searchTerm, user.platforms, - currentPage + 1 + currentPage + 1, + sort ); setHasMore(data.nextPage ? true : false); const newData = [...mentionDatas, data.mentions].flat(); @@ -37,8 +41,21 @@ const HomePage = () => { setCurrentPage(currentPage + 1); }; + const handleAlignment = (event, newAlignment) => { + setSwitching(true); + getMentions(dispatch, searchTerm, user.platforms, 1, newAlignment) + .then((data) => { + setMentionDatas(data.mentions); + setHasMore(data.nextPage ? true : false); + setSort(newAlignment); + setCurrentPage(1); + }) + .catch((err) => alert(err.message)) + .finally(() => setSwitching(false)); + }; + useEffect(() => { - getMentions(dispatch, searchTerm, user.platforms, 1) + getMentions(dispatch, searchTerm, user.platforms, 1, sort) .then((data) => { setMentionDatas(data.mentions); setHasMore(data.nextPage ? true : false); @@ -49,15 +66,6 @@ const HomePage = () => { if (mentionDatas === null) return ; - const items = - !error && mentionDatas.length > 0 ? ( - mentionDatas.map((mentionData) => ( - - )) - ) : ( -
No results found
- ); - return ( <> @@ -70,15 +78,28 @@ const HomePage = () => { My mentions + + + {switching ? ( + + ) : ( + + )} - loadMore(currentPage)} - hasMore={hasMore} - loader={}> - {items} -