Skip to content

Commit 61e2cdb

Browse files
committed
refactored the way Lists and tasks being fetched into contexts, fixed all the bugs in the update task erea, and added the update description field so the user can set or delete the description
1 parent 956275c commit 61e2cdb

File tree

12 files changed

+604
-431
lines changed

12 files changed

+604
-431
lines changed

client/src/App.tsx

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,33 @@ import Register from "./Authentication/Register";
66
import Dashboard from "./Dashboard/Dashboard";
77
import Navbar from "./Navbar/Navbar";
88
import ProtectedRoute from "./Authentication/ProtectedRoute";
9+
import { ListsProvider } from "./Dashboard/context/ListsContext";
910

1011
export default function App() {
1112
return (
1213
<BrowserRouter>
1314
<AuthProvider>
14-
<div className="app-container">
15-
<Navbar />
16-
<Routes>
17-
<Route
18-
index
19-
element={<Navigate to="/dashboard" replace />}
20-
/>
21-
<Route path="/login" element={<Login />} />
22-
<Route path="/register" element={<Register />} />
23-
<Route
24-
path="/dashboard"
25-
element={
26-
<ProtectedRoute>
27-
<Dashboard />
28-
</ProtectedRoute>
29-
}
30-
/>
31-
</Routes>
32-
</div>
15+
<ListsProvider>
16+
<div className="app-container">
17+
<Navbar />
18+
<Routes>
19+
<Route
20+
index
21+
element={<Navigate to="/dashboard" replace />}
22+
/>
23+
<Route path="/login" element={<Login />} />
24+
<Route path="/register" element={<Register />} />
25+
<Route
26+
path="/dashboard"
27+
element={
28+
<ProtectedRoute>
29+
<Dashboard />
30+
</ProtectedRoute>
31+
}
32+
/>
33+
</Routes>
34+
</div>
35+
</ListsProvider>
3336
</AuthProvider>
3437
</BrowserRouter>
3538
);

client/src/Authentication/context/AuthContext.tsx

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ interface AuthContextType {
88
accessToken: string | null;
99
userId: string | null;
1010
user: User | null;
11-
isSidebarOpen: boolean;
12-
toggleSidebar: () => void;
1311
login: (authResponse: AuthResponse) => void;
1412
logout: () => void;
1513
refreshAccessToken: () => void;
@@ -23,10 +21,6 @@ function AuthProvider({ children }: { children: React.ReactNode }) {
2321
const [userId, setUserId] = useState<string | null>(null);
2422
const [user, setUser] = useState<User | null>(null);
2523
const [isLoading, setIsLoading] = useState(true);
26-
const [isSidebarOpen, setIsSidebarOpen] = useState(() => {
27-
const saved = localStorage.getItem("sidebarOpen");
28-
return saved !== null ? JSON.parse(saved) : true;
29-
});
3024

3125
useEffect(() => {
3226
const initializeAuth = async () => {
@@ -90,12 +84,6 @@ function AuthProvider({ children }: { children: React.ReactNode }) {
9084
}
9185
};
9286

93-
const toggleSidebar = () => {
94-
const newState = !isSidebarOpen;
95-
setIsSidebarOpen(newState);
96-
localStorage.setItem("sidebarOpen", JSON.stringify(newState));
97-
};
98-
9987
if (isLoading) {
10088
return <div>Loading...</div>;
10189
}
@@ -106,11 +94,9 @@ function AuthProvider({ children }: { children: React.ReactNode }) {
10694
accessToken,
10795
userId,
10896
user,
109-
isSidebarOpen,
11097
login,
11198
logout,
11299
refreshAccessToken,
113-
toggleSidebar,
114100
}}
115101
>
116102
{children}
@@ -126,4 +112,4 @@ const UseAuth = () => {
126112
return context;
127113
};
128114

129-
export { AuthContext, AuthProvider, UseAuth };
115+
export { AuthProvider, UseAuth };

client/src/Dashboard/Dashboard.tsx

Lines changed: 9 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,10 @@
1-
import { useState, useEffect } from "react";
2-
import { List } from "../API/interfaces";
3-
import { tasksApi } from "../API/Calls/TasksCalls";
4-
import { listsApi } from "../API/Calls/ListsCalls";
5-
import { UseAuth } from "../Authentication/context/AuthContext";
6-
import MainContent from "./MainContent";
7-
import Sidebar from "./Sidebar";
1+
import { UseLists } from "./context/ListsContext";
2+
import { TaskProvider } from "./context/TaskContext";
3+
import MainContent from "./main/MainContent";
4+
import Sidebar from "./sidebar/Sidebar";
85

96
export default function Dashboard() {
10-
const [lists, setLists] = useState<List[]>([]);
11-
const [selectedList, setSelectedList] = useState<List | null>(null);
12-
const [isLoading, setIsLoading] = useState(true);
13-
const { isSidebarOpen, toggleSidebar } = UseAuth();
14-
15-
useEffect(() => {
16-
const fetchLists = async () => {
17-
try {
18-
setIsLoading(true);
19-
const response = await listsApi.getAllLists();
20-
const sortedLists = [...response].sort((a, b) => {
21-
const nameA = a.name.toLowerCase();
22-
const nameB = b.name.toLowerCase();
23-
const numA = parseInt(nameA) || 0;
24-
const numB = parseInt(nameB) || 0;
25-
if (numA && numB) {
26-
return numA - numB;
27-
}
28-
return nameA.localeCompare(nameB);
29-
});
30-
setLists(sortedLists);
31-
if (sortedLists.length > 0) {
32-
setSelectedList(sortedLists[0]);
33-
}
34-
} catch (err: Error | unknown) {
35-
console.error("Fetch error:", err);
36-
} finally {
37-
setIsLoading(false);
38-
}
39-
};
40-
fetchLists();
41-
}, []);
42-
43-
useEffect(() => {
44-
setSelectedList(selectedList);
45-
}, [selectedList]);
46-
47-
useEffect(() => {
48-
const fetchTasks = async () => {
49-
if (selectedList) {
50-
try {
51-
const tasks = await tasksApi.getTasksByListId(
52-
selectedList.id
53-
);
54-
setSelectedList({
55-
...selectedList,
56-
tasks,
57-
});
58-
} catch (err) {
59-
console.error("Failed to fetch tasks:", err);
60-
}
61-
}
62-
};
63-
fetchTasks();
64-
}, [selectedList?.id]);
65-
66-
const handleListUpdate = (updatedList: List) => {
67-
setLists(
68-
lists.map((list) =>
69-
list.id === updatedList.id ? updatedList : list
70-
)
71-
);
72-
setSelectedList(updatedList);
73-
};
7+
const { isLoading, isSidebarOpen, toggleSidebar } = UseLists();
748

759
if (isLoading)
7610
return (
@@ -83,12 +17,7 @@ export default function Dashboard() {
8317
<div className="container-fluid h-90 bg-dark text-light">
8418
<div className="row bg-dark">
8519
{isSidebarOpen ? (
86-
<Sidebar
87-
lists={lists}
88-
setLists={setLists}
89-
selectedList={selectedList}
90-
setSelectedList={setSelectedList}
91-
/>
20+
<Sidebar />
9221
) : (
9322
<div
9423
className="d-flex flex-column bg-dark text-light t-smooth"
@@ -105,11 +34,9 @@ export default function Dashboard() {
10534
</div>
10635
)}
10736

108-
<MainContent
109-
selectedList={selectedList}
110-
setSelectedList={setSelectedList}
111-
onListUpdate={handleListUpdate}
112-
/>
37+
<TaskProvider>
38+
<MainContent />
39+
</TaskProvider>
11340
</div>
11441
</div>
11542
);

0 commit comments

Comments
 (0)