-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.lazy.tsx
106 lines (91 loc) · 3.04 KB
/
index.lazy.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
import { createLazyFileRoute } from "@tanstack/react-router";
import { GoalsView } from "../../views/Goals/GoalsView";
import { useDispatch, useSelector } from "react-redux";
import {
setEndGoal,
setGoalType,
setExercise,
addGoalDb,
fetchGoals,
deleteGoalDb,
resetToDefaultState,
} from "../../Model/goals/goalsReducer";
import { useEffect, useState } from "react";
import { AppDispatch,RootState } from "../../store";
import { setSearchName } from "../../Model/addWorkout/addWorkoutSlice";
import { Exercise, ExerciseTypes } from "../../Model/workouts/workoutsSlice";
import { useHandlers } from "../../PresenterUtils/handlers";
export const Route = createLazyFileRoute("/goals/")({
component: GoalsPresenter,
});
export function GoalsPresenter() {
const { handleSetName, handleSearch, handleSetType, handleSetDistance } = useHandlers();
const dispatch = useDispatch<AppDispatch>();
const [open, setOpen] = useState(false);
const [isLoading, setIsLoading] = useState(true);
function updateExercise(exercise: Exercise) {
dispatch(setExercise(exercise.name));
dispatch(setGoalType(exercise.type));
}
function updateEndGoal(endGoal: number) {
dispatch(setEndGoal(endGoal));
}
async function handleAddGoal() {
const response = await dispatch(addGoalDb());
if (response.meta.requestStatus === "rejected") {
console.log("Error adding goal.");
}
dispatch(fetchGoals());
dispatch(resetToDefaultState());
dispatch(setSearchName(""));
setOpen(false);
}
async function deleteGoal(key: string) {
const response = await dispatch(deleteGoalDb(key));
if (response.meta.requestStatus === "rejected") {
console.log("Error deleting goal.");
}
dispatch(fetchGoals());
}
const goals = useSelector((state: RootState) => state.goals);
const [isAddButtonDisabled, setIsButtonDisabled] = useState(true);
useEffect(() => {
if (goals.endGoal && goals.currentExercise) {
setIsButtonDisabled(false);
} else {
setIsButtonDisabled(true);
}
}, [goals.endGoal, goals.currentExercise]);
useEffect(() => {
// Fetch workouts from database
try {
setIsLoading(true);
dispatch(fetchGoals());
setIsLoading(false);
} catch (error) {
console.log("Error fetching goals. Try again later.", "error");
}
}, [dispatch]);
const addWorkoutState = useSelector((state: RootState) => state.addWorkout);
const filteredGoals = goals.goals.filter(goal => goal.key !== undefined);
return(
<GoalsView
open={open}
setOpen={setOpen}
updateExercise={updateExercise}
updateEndGoal={updateEndGoal}
handleAddGoal={handleAddGoal}
isAddButtonDisabled={isAddButtonDisabled}
handleSearch={handleSearch}
handleSetName={handleSetName}
handleSetDistance={handleSetDistance}
handleSetType={handleSetType}
filteredGoals={filteredGoals}
deleteGoal={deleteGoal}
goals={goals}
addWorkoutState={addWorkoutState}
ExerciseTypes={ExerciseTypes}
isLoading={isLoading}
/>
);
}