-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlansIndex.js
90 lines (82 loc) · 2.11 KB
/
PlansIndex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import PlanExcerpt from "./PlanExcerpt";
import React, { useEffect } from "react";
import { nanoid } from "@reduxjs/toolkit";
import { selectAllPlans } from "./plansSlice";
import { fetchPlans } from "../plans/plansSlice";
//MATERIAL UI
import Typography from "@material-ui/core/Typography";
import { makeStyles } from "@material-ui/core/styles";
import { useSelector, useDispatch } from "react-redux";
import LinearProgress from "@material-ui/core/LinearProgress";
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
},
paper: {
height: 140,
width: 100,
},
control: {
padding: theme.spacing(2),
},
loading: {
paddingTop: "100px",
},
heading: {
justifyContent: "center",
alignText: "center",
alignItems: "center",
display: "flex",
paddingTop: "25px",
paddingBottom: "25px",
},
}));
const PlansIndex = () => {
const dispatch = useDispatch();
const classes = useStyles();
const plans = useSelector(selectAllPlans);
const status = useSelector((state) => state.plans.status);
const error = useSelector((state) => state.plans.error);
const filteredPlans = plans.filter(
(plan) => plan.meal_plan_recipes.length !== 0
);
useEffect(() => {
if (status === "idle") {
dispatch(fetchPlans());
}
}, [status, dispatch]);
//Conditionally renders based off of the fetch status
let content;
if (status === "loading") {
content = (
<div className={classes.loading}>
<LinearProgress color='secondary' />
</div>
);
} else if (status === "succeeded") {
content = filteredPlans.map((plan) => (
<PlanExcerpt key={nanoid()} {...plan} />
));
} else if (status === "failed") {
content = <div>{error}</div>;
}
return (
<section className={classes.root}>
<Typography
style={{
textAlign: "center",
margin: "20px",
paddingTop: "20px",
paddingBottom: "10px",
}}
variant='h3'
color='primary'
component='h3'
>
Meal Plans
</Typography>
{content}
</section>
);
};
export default PlansIndex;