From e3b208fba50d0c51dc3e1d708c8683f707d487ce Mon Sep 17 00:00:00 2001 From: Vincent Date: Sat, 6 Dec 2025 00:23:37 -0500 Subject: [PATCH 1/2] updating this to work --- .../src/containers/Admin/Users/AllUsers.tsx | 222 +++++++++++- .../Admin/Users/UserProfile/UserProfile.tsx | 330 +++++++++++++++++- 2 files changed, 523 insertions(+), 29 deletions(-) diff --git a/client/src/containers/Admin/Users/AllUsers.tsx b/client/src/containers/Admin/Users/AllUsers.tsx index 7ea76d5..d00ab9c 100644 --- a/client/src/containers/Admin/Users/AllUsers.tsx +++ b/client/src/containers/Admin/Users/AllUsers.tsx @@ -68,8 +68,199 @@ // export default AllUsers +// import React, { useEffect, useState } from "react"; +// import { Link, useNavigate } from "react-router-dom"; +// import axios from "axios"; +// import "./AllUsers.css"; + +// interface User { +// _id: string; +// email: string; +// password?: string; +// description?: string; +// archived?: boolean; +// admin?: boolean; +// developer?: boolean; +// } + +// const ITEMS_PER_PAGE = 10; + +// const AllUsers: React.FC = () => { +// const [users, setUsers] = useState([]); +// const [search, setSearch] = useState(""); +// const [archivedFilter, setArchivedFilter] = useState("all"); +// const [page, setPage] = useState(1); +// const navigate = useNavigate(); + +// // Fetch users +// const fetchUsers = async () => { +// try { +// const { data } = await axios.get("/api/user/view"); +// setUsers(data); +// } catch (error) { +// console.error("Error fetching users:", error); +// } +// }; + +// useEffect(() => { +// fetchUsers(); +// }, []); + +// // Filtered users +// const filteredUsers = users.filter((user) => { +// const matchesSearch = +// user.email?.toLowerCase().includes(search.toLowerCase()) || +// user.description?.toLowerCase().includes(search.toLowerCase()); +// const matchesArchived = +// archivedFilter === "all" || +// (archivedFilter === "archived" && user.archived) || +// (archivedFilter === "active" && !user.archived); +// return matchesSearch && matchesArchived; +// }); + +// // Pagination +// const startIndex = (page - 1) * ITEMS_PER_PAGE; +// const pageUsers = filteredUsers.slice( +// startIndex, +// startIndex + ITEMS_PER_PAGE +// ); +// const totalPages = Math.ceil(filteredUsers.length / ITEMS_PER_PAGE); + +// // Archive toggle +// const toggleArchive = async (id: string, current: boolean) => { +// try { +// await axios.put(`/api/user/archive/${id}`, { archived: !current }); +// setUsers((prev) => +// prev.map((u) => (u._id === id ? { ...u, archived: !current } : u)) +// ); +// } catch (error) { +// console.error("Error updating archive:", error); +// } +// }; + +// return ( +//
+//
+//

All Users

+//
+ +// {/* Search + Filter */} +//
+// setSearch(e.target.value)} +// /> +// +//
+ +// {/* Scrollable table */} +//
+// +// +// +// +// +// +// +// +// +// +// +// +// +// {pageUsers.length > 0 ? ( +// pageUsers.map((user) => ( +// +// +// +// +// +// +// +// +// +// )) +// ) : ( +// +// +// +// )} +// +//
Email (click to edit)PasswordDescriptionArchivedAdminDeveloperActions
+// +// {user.email} +// +// {user.password || "—"}{user.description || "—"} +// +// {user.archived ? "Archived" : "Active"} +// +// +// +// {user.admin ? "Yes" : "No"} +// +// +// +// {user.developer ? "Yes" : "No"} +// +// +// +//   +// +// Edit +// +//
+// No users found +//
+//
+ +// {/* Pagination */} +// {totalPages > 1 && ( +//
+// +// +// Page {page} of {totalPages} +// +// +//
+// )} + +// +// Back Home +// +//
+// ); +// }; + +// export default AllUsers; + import React, { useEffect, useState } from "react"; -import { Link, useNavigate } from "react-router-dom"; +import { Link } from "react-router-dom"; import axios from "axios"; import "./AllUsers.css"; @@ -90,7 +281,6 @@ const AllUsers: React.FC = () => { const [search, setSearch] = useState(""); const [archivedFilter, setArchivedFilter] = useState("all"); const [page, setPage] = useState(1); - const navigate = useNavigate(); // Fetch users const fetchUsers = async () => { @@ -106,7 +296,7 @@ const AllUsers: React.FC = () => { fetchUsers(); }, []); - // Filtered users + // Filter users const filteredUsers = users.filter((user) => { const matchesSearch = user.email?.toLowerCase().includes(search.toLowerCase()) || @@ -126,7 +316,7 @@ const AllUsers: React.FC = () => { ); const totalPages = Math.ceil(filteredUsers.length / ITEMS_PER_PAGE); - // Archive toggle + // Toggle archive status const toggleArchive = async (id: string, current: boolean) => { try { await axios.put(`/api/user/archive/${id}`, { archived: !current }); @@ -140,9 +330,7 @@ const AllUsers: React.FC = () => { return (
-
-

All Users

-
+

All Users

{/* Search + Filter */}
@@ -177,11 +365,14 @@ const AllUsers: React.FC = () => { - {pageUsers.length > 0 ? ( + {pageUsers.length ? ( pageUsers.map((user) => ( - + {user.email} @@ -189,7 +380,9 @@ const AllUsers: React.FC = () => { {user.description || "—"} {user.archived ? "Archived" : "Active"} @@ -207,7 +400,9 @@ const AllUsers: React.FC = () => { @@ -235,7 +430,10 @@ const AllUsers: React.FC = () => { {/* Pagination */} {totalPages > 1 && (
- diff --git a/client/src/containers/Admin/Users/UserProfile/UserProfile.tsx b/client/src/containers/Admin/Users/UserProfile/UserProfile.tsx index 8c1dc57..aaf3fe6 100644 --- a/client/src/containers/Admin/Users/UserProfile/UserProfile.tsx +++ b/client/src/containers/Admin/Users/UserProfile/UserProfile.tsx @@ -229,6 +229,275 @@ // export default UserProfile; +// import React, { useEffect, useState } from "react"; +// import { useParams, useNavigate, Link } from "react-router-dom"; +// import axios from "axios"; +// import Button from "react-bootstrap/Button"; + +// interface User { +// email: string; +// password?: string; +// description?: string; +// } + +// const UserProfile: React.FC = () => { +// const { id } = useParams<{ id: string }>(); +// const navigate = useNavigate(); +// const [user, setUser] = useState({ email: "", password: "", description: "" }); +// const [loading, setLoading] = useState(true); +// const [error, setError] = useState(null); +// const [success, setSuccess] = useState(false); + +// // Fetch user by ID +// const fetchUser = async () => { +// try { +// const { data } = await axios.get(`/api/user/${id}`); +// setUser(data); +// } catch (err) { +// setError("Failed to fetch user."); +// console.error(err); +// } finally { +// setLoading(false); +// } +// }; + +// useEffect(() => { +// fetchUser(); +// }, [id]); + +// // Handle input changes +// const handleChange = (e: React.ChangeEvent) => { +// const { name, value } = e.target; +// setUser((prev) => ({ ...prev, [name]: value })); +// }; + +// // Handle form submit +// const handleSubmit = async (e: React.FormEvent) => { +// e.preventDefault(); +// try { +// await axios.put(`/api/user/update/${id}`, { +// email: user.email, +// password: user.password, +// description: user.description, +// }); +// setSuccess(true); +// setTimeout(() => navigate("/AllUsers"), 1000); +// } catch (err) { +// setError("Failed to update user."); +// console.error(err); +// } +// }; + +// if (loading) return

Loading user data...

; + +// return ( +//
+//

Update User

+// {error &&

{error}

} +// {success &&

User updated successfully!

} + +//
+//
+// +// +//
+ +//
+// +// +//
+ +//
+// +//