-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dashboard.js
183 lines (171 loc) · 5.03 KB
/
Dashboard.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import DirectionStepper from "./DirectionStepper";
import { nanoid } from "@reduxjs/toolkit"
import React, { useEffect, useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import { fetchSession } from "../session/sessionSlice";
import UserPlanExcerpt from "../user-plans/UserPlansExcerpt";
import { createMealPlan, getCurrentUsersPlans } from "../user-plans/planFetches";
//MATERIAL UI
import Grid from "@material-ui/core/Grid";
import Paper from "@material-ui/core/Paper";
import Button from "@material-ui/core/Button";
import TextField from "@material-ui/core/TextField";
import Typography from "@material-ui/core/Typography";
import { makeStyles } from "@material-ui/core/styles";
import FormControl from "@material-ui/core/FormControl";
import FormHelperText from "@material-ui/core/FormHelperText";
import LinearProgress from "@material-ui/core/LinearProgress";
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
padding: "10px",
},
main: {
backgroundColor: "secondary",
},
subtitle: {
paddingBottom: "10px",
},
columnOne: {
paddingTop: "20px",
justifyContent: "center",
},
columnTwo: {
paddingTop: "20px",
paddingLeft: "20px",
backgroundColor: "#F4F9FE",
justifyContent: 'space-evenly',
},
formControl: {
alignText:'center',
margin: theme.spacing(1),
},
selectEmpty: {
marginTop: theme.spacing(2),
},
heading: {
justifyContent: "center",
alignText: "center",
alignItems: "center",
display: "flex",
paddingTop: "25px",
paddingBottom: "25px",
},
}));
const Dashboard = () => {
const [value, setValue] = useState("");
const dispatch = useDispatch();
const classes = useStyles();
const planStatus = useSelector((state) => state.userPlans.status);
const sessionStatus = useSelector((state) => state.session.planStatus);
const mealPlans = useSelector((state) => state.userPlans.plans);
const error = useSelector((state) => state.session.error);
const user = useSelector((state) => state.session.user);
useEffect(() => {
if (planStatus === "idle") {
dispatch(getCurrentUsersPlans());
}
}, [planStatus, dispatch]);
useEffect(() => {
if (sessionStatus === "idle") {
dispatch(fetchSession());
}
}, [sessionStatus, dispatch]);
let mealPlanContent;
if (planStatus === "loading") {
mealPlanContent = (
<div className={{ paddingTop: "100px" }}>
<LinearProgress color='primary' />
</div>
);
} if (planStatus === "succeeded" && sessionStatus === "succeeded") {
//Maps content for the session user's meal plans
mealPlanContent = mealPlans.map((plan) => (
<Grid item md={4} xs={12} className={classes.plans}>
<UserPlanExcerpt key={nanoid()} {...plan} />
</Grid>
));
} if (planStatus === "failed") {
mealPlanContent = <div>{error}</div>;
}
const handleChange = (e) => {
setValue(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
dispatch(createMealPlan({ title: value }));
setValue('');
};
const mealPlanForm = (
<form className={classes.formControl} onSubmit={(e) => handleSubmit(e)}>
<FormControl fullWidth variant='outlined'>
<TextField
id='outlined-name'
label='Title'
value={value}
onChange={handleChange}
variant='outlined'
/>
<Button color='secondary' variant='contained' type='submit'>
Submit
</Button>
<FormHelperText>' i.e. "Southwest Fusion Week" '</FormHelperText>
</FormControl>
</form>
);
return (
<Grid className={classes.root}>
<Grid container className={classes.heading}>
<Grid item>
<Typography
align='center'
variant='h2'
color='primary'
component='h6'
>
Dashboard
</Typography>
</Grid>
</Grid>
{/* Main */}
<Paper>
<Grid container className={classes.main}>
<Grid className={classes.columnOne} container md={12}>
{/*column 1 */}
<Grid item className={classes.subtitle}>
<Typography
align='center'
variant='h4'
color='primary'
component='h6'
>
Create New Meal Plan
</Typography>
<DirectionStepper />
<Grid item md={12}>
{mealPlanForm}
</Grid>
</Grid>
</Grid>
<Grid className={classes.columnTwo} container md={12}>
{/*column 2*/}
<Grid item md={12}>
<Typography
className={classes.subtitle}
align='center'
variant='h4'
color='primary'
component='h6'
>
Your Meal Plans
</Typography>
</Grid>
{mealPlanContent}
</Grid>
</Grid>
</Paper>
{/* Main */}
</Grid>
);
};
export default Dashboard;