diff --git a/app/page.jsx b/app/page.jsx index 4ff252f..7d4801e 100644 --- a/app/page.jsx +++ b/app/page.jsx @@ -18,7 +18,7 @@ const getTickets = async () => { return res.json(); } catch (error) { - console.log("Error loading tickets: ", error); + console.error("Error loading tickets: ", error); return { tickets: [] }; } }; @@ -28,16 +28,43 @@ const Dashboard = () => { const [filteredTickets, setFilteredTickets] = useState([]); const [statusFilter, setStatusFilter] = useState("all"); const [viewBy, setViewBy] = useState("category"); + const [isLoading, setIsLoading] = useState(true); + const timeout = 5000; const [groupVisibility, setGroupVisibility] = useState({}); useEffect(() => { - const fetchTickets = async () => { - const data = await getTickets(); - setTickets(data?.tickets || []); - setFilteredTickets(data?.tickets || []); + let ticketFetchTimeout; + + const fetchTicketsWithTimeout = async () => { + setIsLoading(true); + + try { + const ticketsPromise = getTickets(); + ticketFetchTimeout = setTimeout(() => { + setTickets([]); + setFilteredTickets([]); + setIsLoading(false); + }, timeout); + + const data = await ticketsPromise; + clearTimeout(ticketFetchTimeout); + + setTickets(data.tickets); + setFilteredTickets(data.tickets); + } catch (error) { + console.error("Error fetching tickets:", error); + setTickets([]); + setFilteredTickets([]); + } finally { + setIsLoading(false); + } }; - fetchTickets(); + fetchTicketsWithTimeout(); + + return () => { + if (ticketFetchTimeout) clearTimeout(ticketFetchTimeout); + }; }, []); useEffect(() => { @@ -75,6 +102,17 @@ const Dashboard = () => { })); }; + if (isLoading) { + return ( +
+
+

+ Loading tickets, please wait... +

+
+ ); + } + return (
{tickets.length === 0 ? (