-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseData.js
More file actions
257 lines (233 loc) · 7.88 KB
/
Copy pathuseData.js
File metadata and controls
257 lines (233 loc) · 7.88 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { useState, useEffect, useCallback } from 'react';
const API_URL = 'http://127.0.0.1:8000/api';
function dayBounds(date) {
return {
start: `${date}T00:00:00`,
end: `${date}T23:59:59.999999`,
};
}
export function useData(selectedDate) {
const [tasks, setTasks] = useState([]);
const [energy, setEnergy] = useState([]);
const [food, setFood] = useState([]);
const [summary, setSummary] = useState(null);
const [stats, setStats] = useState(null);
const [activityDays, setActivityDays] = useState([]);
const [coachAnalysis, setCoachAnalysis] = useState(null);
const [board, setBoard] = useState({ entries: [] });
const [milestones, setMilestones] = useState([]);
const [expenses, setExpenses] = useState([]);
const [savedItems, setSavedItems] = useState([]);
const [loading, setLoading] = useState(true);
const fetchTasks = useCallback(async () => {
try {
const response = await fetch(`${API_URL}/tasks`);
const data = await response.json();
setTasks(data.tasks || []);
} catch (error) {
console.error('Error fetching tasks:', error);
}
}, []);
const fetchEnergy = useCallback(async () => {
try {
const { start, end } = dayBounds(selectedDate);
const response = await fetch(`${API_URL}/energy?start_date=${encodeURIComponent(start)}&end_date=${encodeURIComponent(end)}`);
const data = await response.json();
setEnergy(data.energy_levels || []);
} catch (error) {
console.error('Error fetching energy:', error);
}
}, [selectedDate]);
const fetchFood = useCallback(async () => {
try {
const { start, end } = dayBounds(selectedDate);
const response = await fetch(`${API_URL}/food?start_date=${encodeURIComponent(start)}&end_date=${encodeURIComponent(end)}`);
const data = await response.json();
setFood(data.food_logs || []);
} catch (error) {
console.error('Error fetching food:', error);
}
}, [selectedDate]);
const fetchSummary = useCallback(async () => {
try {
const response = await fetch(`${API_URL}/summary/${selectedDate}`);
const data = await response.json();
setSummary(data);
} catch (error) {
console.error('Error fetching summary:', error);
}
}, [selectedDate]);
const fetchStats = useCallback(async () => {
try {
const response = await fetch(`${API_URL}/stats`);
const data = await response.json();
setStats(data);
} catch (error) {
console.error('Error fetching stats:', error);
}
}, []);
const fetchActivityDays = useCallback(async () => {
try {
const response = await fetch(`${API_URL}/activity-days?limit=45`);
const data = await response.json();
setActivityDays(data.days || []);
} catch (error) {
console.error('Error fetching activity days:', error);
}
}, []);
const fetchCoachAnalysis = useCallback(async () => {
try {
const response = await fetch(`${API_URL}/coach/latest`);
const data = await response.json();
setCoachAnalysis(data.analysis || null);
} catch (error) {
console.error('Error fetching coach analysis:', error);
}
}, []);
const fetchBoard = useCallback(async () => {
try {
const response = await fetch(`${API_URL}/board/today`);
const data = await response.json();
setBoard(data || { entries: [] });
} catch (error) {
console.error('Error fetching court board:', error);
}
}, []);
const fetchMilestones = useCallback(async () => {
try {
const { start, end } = dayBounds(selectedDate);
const response = await fetch(`${API_URL}/milestones?start_date=${encodeURIComponent(start)}&end_date=${encodeURIComponent(end)}`);
const data = await response.json();
setMilestones(data.milestones || []);
} catch (error) {
console.error('Error fetching milestones:', error);
}
}, [selectedDate]);
const fetchExpenses = useCallback(async () => {
try {
const { start, end } = dayBounds(selectedDate);
const response = await fetch(`${API_URL}/expenses?start_date=${encodeURIComponent(start)}&end_date=${encodeURIComponent(end)}`);
const data = await response.json();
setExpenses(data.expenses || []);
} catch (error) {
console.error('Error fetching expenses:', error);
}
}, [selectedDate]);
const fetchSavedItems = useCallback(async () => {
try {
const response = await fetch(`${API_URL}/saved?limit=20`);
const data = await response.json();
setSavedItems(data.items || []);
} catch (error) {
console.error('Error fetching saved items:', error);
}
}, []);
const refreshAll = useCallback(async () => {
setLoading(true);
await Promise.all([
fetchTasks(),
fetchEnergy(),
fetchFood(),
fetchSummary(),
fetchStats(),
fetchActivityDays(),
fetchCoachAnalysis(),
fetchBoard(),
fetchMilestones(),
fetchExpenses(),
fetchSavedItems(),
]);
setLoading(false);
}, [fetchTasks, fetchEnergy, fetchFood, fetchSummary, fetchStats, fetchActivityDays, fetchCoachAnalysis, fetchBoard, fetchMilestones, fetchExpenses, fetchSavedItems]);
// Initial data load - run once on mount
useEffect(() => {
refreshAll();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// Refresh when selectedDate changes
useEffect(() => {
refreshAll();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedDate]);
const deleteTask = useCallback(async (taskId) => {
const response = await fetch(`${API_URL}/tasks/${taskId}`, { method: 'DELETE' });
if (!response.ok) {
throw new Error(`Failed to delete task ${taskId}`);
}
await refreshAll();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const createTask = useCallback(async (description) => {
const response = await fetch(`${API_URL}/tasks`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ description }),
});
if (!response.ok) {
throw new Error('Failed to create task');
}
await refreshAll();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const deleteFood = useCallback(async (foodId) => {
const response = await fetch(`${API_URL}/food/${foodId}`, { method: 'DELETE' });
if (!response.ok) {
throw new Error(`Failed to delete food log ${foodId}`);
}
await refreshAll();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const createFood = useCallback(async (data) => {
const response = await fetch(`${API_URL}/food`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error('Failed to create food log');
}
await refreshAll();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const updateFood = useCallback(async (foodId, data) => {
const response = await fetch(`${API_URL}/food/${foodId}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error('Failed to update food log');
}
await refreshAll();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const markBoardOver = useCallback(async (entryId) => {
const response = await fetch(`${API_URL}/board/${entryId}/over`, { method: 'POST' });
if (!response.ok) {
throw new Error(`Failed to mark board entry ${entryId} over`);
}
await refreshAll();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return {
tasks,
energy,
food,
summary,
stats,
activityDays,
coachAnalysis,
board,
milestones,
expenses,
savedItems,
loading,
refreshAll,
createTask,
deleteTask,
deleteFood,
createFood,
updateFood,
markBoardOver,
};
}