Add Filters to Homepage

This commit is contained in:
Ceda EI 2021-04-10 00:53:19 +05:30
parent 20e06b5639
commit b742a1d98b
2 changed files with 64 additions and 36 deletions

View File

@ -1,38 +1,24 @@
.App { * {
text-align: center; box-sizing: border-box;
} }
.App-logo { .base {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex; display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
} }
.App-link { .filters {
color: #61dafb; width: 10%;
position: fixed;
align-self: flex-start;
top: 200px;
right: 10%;
padding: 0%;
} }
@keyframes App-logo-spin { .feeds {
from { margin: 2em 10%;
transform: rotate(0deg); display: flex;
} flex-wrap: wrap;
to { justify-content: space-evenly;
transform: rotate(360deg); width: 60%;
}
} }

View File

@ -7,7 +7,9 @@ import {
CardActions, CardActions,
CardContent, CardContent,
CardMedia, CardMedia,
Checkbox,
Chip, Chip,
FormControlLabel,
IconButton, IconButton,
Link, Link,
Menu, Menu,
@ -22,9 +24,10 @@ import NavBar from "./shared/NavBar";
import config from "../config"; import config from "../config";
// TODO: Replace with API accessed feeds // TODO: Replace with API accessed feeds
import feeds from "./dummyData/feeds.json"; import feeds from "./dummyData/feeds.json";
const feedCategories = Array.from(new Set(feeds.map(i => i.tags).flat()));
function FeedCard({ feed }) { function FeedCard({ feed }) {
const [filled, setFilled] = useState(Math.random() > 0.5); const [filled, setFilled] = useState(Math.random() > 0.7);
let style = {}; let style = {};
if (filled) { if (filled) {
style={ style={
@ -35,8 +38,9 @@ function FeedCard({ feed }) {
<Card <Card
key={feed.id} key={feed.id}
style={{ style={{
maxWidth: "600px", maxWidth: "500px",
margin: "20px auto", width: "100%",
margin: "20px 0",
}} }}
> >
<CardActionArea> <CardActionArea>
@ -76,6 +80,7 @@ FeedCard.propTypes = {
function App() { function App() {
const history = useHistory(); const history = useHistory();
const [anchorEl, setAnchorEl] = useState(null); const [anchorEl, setAnchorEl] = useState(null);
const [categories, setCategories] = useState(feedCategories);
const open = Boolean(anchorEl); const open = Boolean(anchorEl);
function handleMenu(event) { function handleMenu(event) {
@ -91,6 +96,25 @@ function App() {
history.push("/"); history.push("/");
} }
function toggleCategory(category) {
console.log(category);
console.log(categories);
console.log(categories.includes(category));
if (categories.includes(category))
setCategories(categories.filter(i => i !== category));
else
setCategories([...categories, category]);
}
const visibleFeeds = feeds.filter(i => {
for (let tag of i.tags) {
for (let category of categories) {
if (category == tag)
return true;
}
}
return false;
});
return (<> return (<>
<NavBar> <NavBar>
<div> <div>
@ -124,9 +148,27 @@ function App() {
</Menu> </Menu>
</div> </div>
</NavBar> </NavBar>
<div className="base">
<div> <div className="feeds">
{feeds.map(feed => <FeedCard key={feed.id} feed={feed} />)} {visibleFeeds.length === 0 ? <Typography variant="h5" style={{color: "#666"}}component="div"> Nothing here (°°) </Typography>: null}
{visibleFeeds.map(feed => <FeedCard key={feed.id} feed={feed} />)}
</div>
<div className="filters">
<Typography variant="h5" component="p">Filters</Typography>
{feedCategories.map(category => (
<FormControlLabel
key={category}
control={
<Checkbox
checked={categories.includes(category)}
onChange={() => toggleCategory(category)}
name="checkedF"
/>
}
label={category}
/>
))}
</div>
</div> </div>
</>); </>);
} }