From 10ab730f8c6f8bd5a8dddc193b649321146017d4 Mon Sep 17 00:00:00 2001 From: Atul Gunjal Date: Thu, 24 Apr 2025 10:33:36 +0530 Subject: [PATCH] minor --- src/app/components/TokenTable.jsx | 428 +++++++++++++++++------------- 1 file changed, 245 insertions(+), 183 deletions(-) diff --git a/src/app/components/TokenTable.jsx b/src/app/components/TokenTable.jsx index 85d5c91..73aa564 100644 --- a/src/app/components/TokenTable.jsx +++ b/src/app/components/TokenTable.jsx @@ -5,191 +5,253 @@ import { DATABASE_ID, COLLECTION_ID } from "../lib/api"; import { useTheme } from "../context/ThemeContext"; export default function TokenTable({ statusFilter }) { - // ... (keep all your existing state and functions) + const { darkMode } = useTheme(); + const [entries, setEntries] = useState([]); + const [filteredEntries, setFilteredEntries] = useState([]); + const [searchQuery, setSearchQuery] = useState(""); + const [currentPage, setCurrentPage] = useState(1); + const entriesPerPage = 20; + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + const getEntries = async () => { + try { + const today = new Date().toISOString().split('T')[0]; + const response = await databases.listDocuments( + DATABASE_ID, + COLLECTION_ID, + [ + Query.equal('date', today), + Query.orderAsc('tokenNumber') + ], + 100 + ); + return response.documents; + } catch (error) { + console.error("Fetch error:", error); + throw error; + } + }; + + const updateStatus = async (entryId, newStatus) => { + try { + setLoading(true); + await databases.updateDocument( + DATABASE_ID, + COLLECTION_ID, + entryId, + { status: newStatus } + ); + const data = await getEntries(); + setEntries(data); + applyFilters(data, searchQuery); + } catch (error) { + console.error("Update error:", error); + setError(error.message); + } finally { + setLoading(false); + } + }; + + const applyFilters = (data, search) => { + let filtered = data.filter((entry) => { + const matchesSearch = + entry.patientName.toLowerCase().includes(search.toLowerCase()) || + entry.tokenNumber.toString().includes(search); + + const matchesFilter = + statusFilter === "all" || + entry.status === statusFilter || + (statusFilter === "booked" && entry.status === "booked"); + + return matchesSearch && matchesFilter; + }); + setFilteredEntries(filtered); + }; + + useEffect(() => { + const loadData = async () => { + try { + const data = await getEntries(); + setEntries(data); + applyFilters(data, searchQuery); + } catch (err) { + setError(err.message); + } finally { + setLoading(false); + } + }; + loadData(); + }, []); + + useEffect(() => { + applyFilters(entries, searchQuery); + setCurrentPage(1); + }, [searchQuery, statusFilter, entries]); + + const indexOfLastEntry = currentPage * entriesPerPage; + const indexOfFirstEntry = indexOfLastEntry - entriesPerPage; + const currentEntries = filteredEntries.slice(indexOfFirstEntry, indexOfLastEntry); + const totalPages = Math.ceil(filteredEntries.length / entriesPerPage); + + + +// Handle theme loading state +if (darkMode === undefined) { return ( -
- setSearchQuery(e.target.value)} - /> - - {filteredEntries.length === 0 ? ( -
- No entries found. -
- ) : ( - <> - {/* Mobile View (Cards) */} -
- {currentEntries.map((entry) => ( -
-
-
-

Token: {entry.tokenNumber}

-

{entry.patientName}

-
- - {entry.status === "booked" ? "In-Queue" : entry.status} - -
- - {entry.status === "booked" && ( -
- - -
- )} -
- ))} -
- - {/* Desktop View (Table) */} -
- - - - - - - - - - - {currentEntries.map((entry) => ( - - - - - - - ))} - -
TokenNameStatusActions
{entry.tokenNumber}{entry.patientName} - - {entry.status === "booked" ? "In-Queue" : entry.status} - - - {entry.status === "booked" && ( - <> - - - - )} -
-
- - {/* Pagination (Works for both views) */} -
- - - Page {currentPage} of {totalPages} - - -
- - )} +
+
+
+

+ Loading... +

+
); +} +if (loading) return ( +
+
+

+ Loading... +

+
+); + +if (error) return ( +
+ Error: {error} +
+); + +return ( +
+ setSearchQuery(e.target.value)} + /> + + {filteredEntries.length === 0 ? ( +
+ No entries found. +
+ ) : ( + <> +
+ + + + + + + + + + + {currentEntries.map((entry) => ( + + + + + + + ))} + +
TokenNameStatusActions
{entry.tokenNumber}{entry.patientName} + + {entry.status === "booked" ? "In-Queue" : entry.status} + + + {entry.status === "booked" && ( + <> + + + + )} +
+
+ +
+ + + Page {currentPage} of {totalPages} + + +
+ + )} +
+); } \ No newline at end of file