Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder switch #55

Merged
merged 7 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Login from "./pages/Login";
import PagesWrapper from "./pages/PagesWrapper";
import "./App.css";
import PrivateRoute from "./pages/PrivateRoute";
import SortToggle from "./layout/SortToggle";

function App() {
return (
Expand All @@ -21,7 +20,6 @@ function App() {
<PrivateRoute path="/setting" component={SettingPage} />
<Route path="/signup" component={Signup} />
<Route path="/login" component={Login} />
<Route path="/testingmaterial" component={SortToggle} />
</PagesWrapper>
</Switch>
</BrowserRouter>
Expand Down
14 changes: 11 additions & 3 deletions client/src/layout/Scroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ 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,
items,
error,
loadmore,
hasMore,
setHasMore,
mentionDatas,
setMentionDatas,
}) => {
const { dispatch, error, searchTerm, user } = useContext(UserContext);
const { dispatch, searchTerm, user } = useContext(UserContext);

useEffect(() => {
getMentions(dispatch, searchTerm, user.platforms, 1, sort)
Expand All @@ -29,7 +31,13 @@ const Scroller = ({
loadMore={loadmore}
hasMore={hasMore}
loader={<Spinner />}>
{items}
{!error && mentionDatas.length > 0 ? (
mentionDatas.map((mentionData) => (
<Mention key={mentionData._id} mention={mentionData} />
))
) : (
<div>No results found</div>
)}
</InfiniteScroll>
);
};
Expand Down
2 changes: 1 addition & 1 deletion client/src/layout/SortToggle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useContext } from "react";
import React from "react";
import ToggleButton from "@material-ui/lab/ToggleButton";
import ToggleButtonGroup from "@material-ui/lab/ToggleButtonGroup";

Expand Down
38 changes: 18 additions & 20 deletions client/src/pages/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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";
Expand All @@ -23,6 +22,7 @@ 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);
Expand All @@ -47,12 +47,14 @@ const HomePage = () => {
};

const handleAlignment = (event, newAlignment) => {
setSwitching(true);
getMentions(dispatch, searchTerm, user.platforms, 1, newAlignment)
.then((data) => {
setMentionDatas(data.mentions);
setHasMore(data.nextPage ? true : false);
})
.catch((err) => alert(err.message));
.catch((err) => alert(err.message))
.finally(() => setSwitching(false));
Jovialiste82 marked this conversation as resolved.
Show resolved Hide resolved
setSort(newAlignment);
setCurrentPage(1);
};
Expand All @@ -69,15 +71,6 @@ const HomePage = () => {

if (mentionDatas === null) return <Spinner />;

const items =
!error && mentionDatas.length > 0 ? (
mentionDatas.map((mentionData) => (
<Mention key={mentionData._id} mention={mentionData} />
))
) : (
<div>No results found</div>
);

return (
<>
<AppBarLoggedIn />
Expand All @@ -90,22 +83,27 @@ const HomePage = () => {
<Typography variant="h3" align="left">
My mentions
</Typography>

<SortToggle
align="right"
handleAlignment={handleAlignment}
alignment={sort}
setAlignment={setSort}
/>
</Box>

<Scroller
sort={sort}
items={items}
loadmore={loadMore}
hasMore={hasMore}
setHasMore={setHasMore}
setMentionDatas={setMentionDatas}
/>
{switching ? (
<Spinner />
) : (
<Scroller
sort={sort}
loadmore={loadMore}
hasMore={hasMore}
setHasMore={setHasMore}
mentionDatas={mentionDatas}
setMentionDatas={setMentionDatas}
error={error}
/>
)}
</Grid>
<Grid item xs={2} style={{ backgroundColor: "#FAFBFF" }}></Grid>
</Grid>
Expand Down