fix(CQ-WEB-10): Add loading/error states to pages with mock data
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Convert tasks, calendar, and dashboard pages from synchronous mock data to async loading pattern with useState/useEffect. Each page now shows a loading state via child components while data loads, and displays a PDA-friendly amber-styled message with a retry button if loading fails. This prepares these pages for real API integration by establishing the async data flow pattern. Child components (TaskList, Calendar, dashboard widgets) already handled isLoading props — now the pages actually use them. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
46
apps/web/src/app/(authenticated)/calendar/page.test.tsx
Normal file
46
apps/web/src/app/(authenticated)/calendar/page.test.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { render, screen, waitFor } from "@testing-library/react";
|
||||
import CalendarPage from "./page";
|
||||
|
||||
// Mock the Calendar component
|
||||
vi.mock("@/components/calendar/Calendar", () => ({
|
||||
Calendar: ({
|
||||
events,
|
||||
isLoading,
|
||||
}: {
|
||||
events: unknown[];
|
||||
isLoading: boolean;
|
||||
}): React.JSX.Element => (
|
||||
<div data-testid="calendar">{isLoading ? "Loading" : `${String(events.length)} events`}</div>
|
||||
),
|
||||
}));
|
||||
|
||||
describe("CalendarPage", (): void => {
|
||||
it("should render the page title", (): void => {
|
||||
render(<CalendarPage />);
|
||||
expect(screen.getByRole("heading", { level: 1 })).toHaveTextContent("Calendar");
|
||||
});
|
||||
|
||||
it("should show loading state initially", (): void => {
|
||||
render(<CalendarPage />);
|
||||
expect(screen.getByTestId("calendar")).toHaveTextContent("Loading");
|
||||
});
|
||||
|
||||
it("should render the Calendar with events after loading", async (): Promise<void> => {
|
||||
render(<CalendarPage />);
|
||||
await waitFor((): void => {
|
||||
expect(screen.getByTestId("calendar")).toHaveTextContent("3 events");
|
||||
});
|
||||
});
|
||||
|
||||
it("should have proper layout structure", (): void => {
|
||||
const { container } = render(<CalendarPage />);
|
||||
const main = container.querySelector("main");
|
||||
expect(main).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("should render the subtitle text", (): void => {
|
||||
render(<CalendarPage />);
|
||||
expect(screen.getByText("View your schedule at a glance")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -1,18 +1,39 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import type { ReactElement } from "react";
|
||||
import { Calendar } from "@/components/calendar/Calendar";
|
||||
import { mockEvents } from "@/lib/api/events";
|
||||
import type { Event } from "@mosaic/shared";
|
||||
|
||||
export default function CalendarPage(): ReactElement {
|
||||
// TODO: Replace with real API call when backend is ready
|
||||
// const { data: events, isLoading } = useQuery({
|
||||
// queryKey: ["events"],
|
||||
// queryFn: fetchEvents,
|
||||
// });
|
||||
const [events, setEvents] = useState<Event[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const events = mockEvents;
|
||||
const isLoading = false;
|
||||
useEffect(() => {
|
||||
void loadEvents();
|
||||
}, []);
|
||||
|
||||
async function loadEvents(): Promise<void> {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
// TODO: Replace with real API call when backend is ready
|
||||
// const data = await fetchEvents();
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
setEvents(mockEvents);
|
||||
} catch (err) {
|
||||
setError(
|
||||
err instanceof Error
|
||||
? err.message
|
||||
: "We had trouble loading your calendar. Please try again when you're ready."
|
||||
);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<main className="container mx-auto px-4 py-8">
|
||||
@@ -20,7 +41,20 @@ export default function CalendarPage(): ReactElement {
|
||||
<h1 className="text-3xl font-bold text-gray-900">Calendar</h1>
|
||||
<p className="text-gray-600 mt-2">View your schedule at a glance</p>
|
||||
</div>
|
||||
<Calendar events={events} isLoading={isLoading} />
|
||||
|
||||
{error !== null ? (
|
||||
<div className="rounded-lg border border-amber-200 bg-amber-50 p-6 text-center">
|
||||
<p className="text-amber-800">{error}</p>
|
||||
<button
|
||||
onClick={() => void loadEvents()}
|
||||
className="mt-4 rounded-md bg-amber-600 px-4 py-2 text-sm font-medium text-white hover:bg-amber-700 transition-colors"
|
||||
>
|
||||
Try again
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<Calendar events={events} isLoading={isLoading} />
|
||||
)}
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user