fix(ux-25): add persistent error state with retry and recovery to shop archive

This commit is contained in:
devops committed 2026-10-17 18:05:35 +00:00
1 parent 956ef3b4d3
commit 92065cc7f2
2 files changed
+58 -2

No files matched your search

@@ -466,6 +466,7 @@ export default function ArchivePage({
const [activeSymptoms, setActiveSymptoms] = useState<string[]>(initialSymptoms ? initialSymptoms.split(',') : []);
const [searchQuery, setSearchQuery] = useState(initialSearch);
const [isUpdating, setIsUpdating] = useState(false);
const [fetchError, setFetchError] = useState<string | null>(null);
const [filteredProducts, setFilteredProducts] = useState<Product[]>(() => orderProductsByAvailability(initialProducts));
const [sortBy, setSortBy] = useState<string>("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({
)}
<AnimatePresence mode="wait">
{isUpdating ? (
{fetchError ? (
<div key="fetch-error" data-testid="shop-fetch-error" className="bg-white rounded-[2.5rem] p-10 sm:p-14 text-center border border-rose-200/80 shadow-sm flex flex-col items-center">
<div className="w-16 h-16 bg-rose-50 rounded-2xl flex items-center justify-center text-rose-500 mb-4 border border-rose-100">
<AlertCircle className="w-8 h-8" />
</div>
<h3 className="text-xl font-black text-medical-gray-900 mb-2">خطا در دریافت اطلاعات محصولات</h3>
<p className="text-medical-gray-500 max-w-md mb-6 font-medium text-xs sm:text-sm leading-relaxed">{fetchError}</p>
<button
type="button"
onClick={() => fetchProducts()}
className="inline-flex items-center gap-2 bg-canina-blue text-white px-6 py-2.5 rounded-xl font-black text-xs sm:text-sm hover:bg-canina-dark transition-all shadow-md shadow-canina-blue/20 cursor-pointer active:scale-95"
>
<Activity className="w-4 h-4" />
<span>تلاش مجدد</span>
</button>
</div>
) : isUpdating ? (
viewMode === "list" ? (
<div key="skeleton-list" className="flex flex-col gap-3 lg:hidden">
{Array.from({ length: 8 }).map((_, i) => (
@@ -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(<ArchivePage />);
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();
});
});
});