-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoalFormView.tsx
166 lines (162 loc) · 4.76 KB
/
GoalFormView.tsx
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
import {
Grid,
Typography,
TextField,
SelectChangeEvent,
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
FormControl,
FormControlLabel,
Radio,
RadioGroup,
Alert,
AlertTitle,
} from "@mui/material";
import { SearchBarView } from "../Search/SearchBarView";
import { Exercise, ExerciseType } from "../../Model/workouts/workoutsSlice";
import FullscreenCircularProgress from "../Application/FullscreenCircularProgress";
export function GoalFormView({
open,
setOpen,
goalType,
onExerciseChange,
onEndGoalChange,
metric,
handleSubmit,
isAddButtonDisabled,
selectedType,
setType,
types,
search,
searchLoading,
searchResults,
setName,
name,
onDistanceChange,
}: {
open: boolean;
setOpen: (open: boolean) => void;
goalType: string | undefined;
onExerciseChange: (exercise: Exercise) => void;
onEndGoalChange: (endGoal: number) => void;
metric: string;
handleSubmit: () => void;
isAddButtonDisabled: boolean;
selectedType: ExerciseType | "all";
setType: (event: SelectChangeEvent) => void;
types: ExerciseType[];
search: () => void;
searchLoading: boolean;
searchResults: Exercise[];
setName: (name: string) => void;
name: string;
onDistanceChange: (distance: number) => void;
}) {
function handleEndGoalChange(evt: React.ChangeEvent<HTMLInputElement>) {
onEndGoalChange(parseFloat(evt.target.value));
}
function handleExerciseChange(evt: React.ChangeEvent<HTMLInputElement>) {
onExerciseChange(JSON.parse(evt.target.value) as Exercise);
}
function handleDistanceChange(evt: React.ChangeEvent<HTMLInputElement>) {
onDistanceChange(parseFloat(evt.target.value));
}
return (
<Dialog open={open} onClose={() => setOpen(false)}>
<DialogTitle>Add New Goal</DialogTitle>
<DialogContent>
<Grid container spacing={2}>
<Grid item xs={12}>
<Typography variant="h6"> Select exercise: </Typography>
</Grid>
<Grid item xs={12}>
<SearchBarView
selectedType={selectedType}
setType={setType}
types={types}
search={search}
setName={setName}
name={name}
></SearchBarView>
</Grid>
<Grid item xs={12}>
{searchLoading ? (
<FullscreenCircularProgress />
) : searchResults.length == 0 ? (
<Grid item xs={12}>
<Typography variant="h6" align="center">
No exercises found
</Typography>
</Grid>
) : (
<Grid item xs={12}>
<FormControl>
<RadioGroup onChange={handleExerciseChange}>
{searchResults.map((result: Exercise, index: number) => {
return (
<FormControlLabel
key={index}
value={JSON.stringify(result)}
control={<Radio />}
label={result.name}
/>
);
})}
</RadioGroup>
</FormControl>
</Grid>
)}
</Grid>
{goalType === "cardio" && (
<>
<Grid item xs={6}>
<Typography variant="h6"> Distance: </Typography>
</Grid>
<Grid item xs={6}>
<TextField
label={"km"}
type="number"
id="outlined-basic"
variant="outlined"
required
onChange={handleDistanceChange}
/>
</Grid>
</>
)}
{searchResults.length !== 0 && (
<>
<Grid item xs={6}>
<Typography variant="h6"> Goal: </Typography>
</Grid>
<Grid item xs={6}>
<TextField
label={metric}
type="number"
id="outlined-basic"
variant="outlined"
required
onChange={handleEndGoalChange}
/>
</Grid>
</>
)}
</Grid>
</DialogContent>
<Alert severity="info">
<AlertTitle>Determining your starting point</AlertTitle>
The next time you log this exercise will set the starting point. After which, each subsequent workout will be
used to calculate your progress.
</Alert>
<DialogActions>
<Button onClick={() => setOpen(false)}>Cancel</Button>
<Button onClick={() => handleSubmit()} disabled={isAddButtonDisabled}>
Add goal
</Button>
</DialogActions>
</Dialog>
);
}