From 92065cc7f20f5445b0a1ff245b680c1b69ef54d7 Mon Sep 17 00:00:00 2001 From: devops Date: Sat, 17 Oct 2026 18:05:35 +0000 Subject: [PATCH] fix(ux-25): add persistent error state with retry and recovery to shop archive --- .../application/components/ArchivePage.tsx | 23 +++++++++++- .../components/__tests__/ArchivePage.test.tsx | 37 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/frontend/application/components/ArchivePage.tsx b/frontend/application/components/ArchivePage.tsx index ec0233c5..ba31ea18 100644 --- a/frontend/application/components/ArchivePage.tsx +++ b/frontend/application/components/ArchivePage.tsx @@ -466,6 +466,7 @@ export default function ArchivePage({ const [activeSymptoms, setActiveSymptoms] = useState(initialSymptoms ? initialSymptoms.split(',') : []); const [searchQuery, setSearchQuery] = useState(initialSearch); const [isUpdating, setIsUpdating] = useState(false); + const [fetchError, setFetchError] = useState(null); const [filteredProducts, setFilteredProducts] = useState(() => orderProductsByAvailability(initialProducts)); const [sortBy, setSortBy] = useState("createdAt"); const [sortOrder, setSortOrder] = useState<"asc" | "desc">("desc"); @@ -574,6 +575,7 @@ export default function ArchivePage({ const didSkipInitialFetch = React.useRef(false); const fetchProducts = useCallback(async () => { + setFetchError(null); setIsUpdating(true); try { const res = await productService.getProducts({ @@ -595,7 +597,8 @@ export default function ArchivePage({ setVisibleCount(12); } catch (error) { console.error("Failed to fetch products:", error); - toast.error("خطا در دریافت لیست محصولات از سرور. لطفاً صفحه را رفرش کنید."); + toast.error("خطا در دریافت لیست محصولات از سرور."); + setFetchError("خطا در برقراری ارتباط با سرور یا دریافت اطلاعات محصولات."); } finally { setIsUpdating(false); } @@ -1094,7 +1097,23 @@ export default function ArchivePage({ )} - {isUpdating ? ( + {fetchError ? ( +
+
+ +
+

خطا در دریافت اطلاعات محصولات

+

{fetchError}

+ +
+ ) : isUpdating ? ( viewMode === "list" ? (
{Array.from({ length: 8 }).map((_, i) => ( diff --git a/frontend/application/components/__tests__/ArchivePage.test.tsx b/frontend/application/components/__tests__/ArchivePage.test.tsx index 5243e288..cb779abc 100644 --- a/frontend/application/components/__tests__/ArchivePage.test.tsx +++ b/frontend/application/components/__tests__/ArchivePage.test.tsx @@ -346,4 +346,41 @@ describe("ArchivePage Component", () => { expect(productLinks).toHaveLength(1); }); + + it("renders error state when productService fails and recovers on retry", async () => { + vi.mocked(productService.getProducts).mockRejectedValueOnce(new Error("Network Error")); + + render(); + + await waitFor(() => { + expect(screen.getByTestId("shop-fetch-error")).toBeInTheDocument(); + expect(screen.getByText("خطا در دریافت اطلاعات محصولات")).toBeInTheDocument(); + expect(screen.getByText("خطا در برقراری ارتباط با سرور یا دریافت اطلاعات محصولات.")).toBeInTheDocument(); + }); + + // Mock recovery on retry + vi.mocked(productService.getProducts).mockResolvedValueOnce({ + data: [ + { + id: "prod-recovered", + name: "محصول بازیابی شده", + nameFa: "محصول بازیابی شده", + priceValue: 150000, + stock: 5, + categorySlug: "joints", + images: ["/test.png"], + symptoms: [], + main_ingredients: [], + } as any, + ], + } as any); + + const retryButton = screen.getByRole("button", { name: /تلاش مجدد/i }); + fireEvent.click(retryButton); + + await waitFor(() => { + expect(screen.queryByTestId("shop-fetch-error")).not.toBeInTheDocument(); + expect(screen.getByText("محصول بازیابی شده")).toBeInTheDocument(); + }); + }); });