diff --git a/frontend/application/app/shop/page.tsx b/frontend/application/app/shop/page.tsx index 9d7b1264..ac11b984 100644 --- a/frontend/application/app/shop/page.tsx +++ b/frontend/application/app/shop/page.tsx @@ -1,6 +1,8 @@ import type { Metadata } from "next"; import ArchivePage from "../../components/ArchivePage"; import { getPageMetadata } from "../../lib/seo"; +import { productService } from "../../lib/services/productService"; +import type { PetType } from "../../lib/data/products"; export async function generateMetadata({ searchParams, @@ -41,6 +43,7 @@ export default async function Shop({ searchParams }: { searchParams: Promise<{ [ const symptomsParam = typeof resolvedParams.symptoms === 'string' ? resolvedParams.symptoms : ""; const symptomSingle = typeof resolvedParams.symptom === 'string' ? resolvedParams.symptom : ""; const symptoms = symptomsParam || (symptomSingle ? symptomSingle : ""); + const { data: initialProducts } = await productService.getProducts({ category, petType: petType as PetType | "all", query: search, symptom: symptomSingle, limit: 999 }); return ( ); } diff --git a/frontend/application/components/ArchivePage.tsx b/frontend/application/components/ArchivePage.tsx index 374af28e..92a4ba77 100644 --- a/frontend/application/components/ArchivePage.tsx +++ b/frontend/application/components/ArchivePage.tsx @@ -449,12 +449,14 @@ export default function ArchivePage({ initialCategory = "all", initialSearch = "", initialPetType = "all", - initialSymptoms = "" + initialSymptoms = "", + initialProducts = [] }: { initialCategory?: string, initialSearch?: string, initialPetType?: string, - initialSymptoms?: string + initialSymptoms?: string, + initialProducts?: Product[] }) { const router = useRouter(); const getText = useSettingsStore(state => state.getText); @@ -463,7 +465,7 @@ export default function ArchivePage({ const [activeSymptoms, setActiveSymptoms] = useState(initialSymptoms ? initialSymptoms.split(',') : []); const [searchQuery, setSearchQuery] = useState(initialSearch); const [isUpdating, setIsUpdating] = useState(false); - const [filteredProducts, setFilteredProducts] = useState([]); + const [filteredProducts, setFilteredProducts] = useState(() => orderProductsByAvailability(initialProducts)); const [sortBy, setSortBy] = useState("createdAt"); const [sortOrder, setSortOrder] = useState<"asc" | "desc">("desc"); const [viewMode, setViewMode] = useState<"list" | "grid">("list"); @@ -560,6 +562,9 @@ export default function ArchivePage({ } }, [selectedCategory, selectedPet, searchQuery, activeSymptoms, router]); + const hasInitialProducts = Boolean(initialProducts && initialProducts.length > 0); + const didSkipInitialFetch = React.useRef(false); + const fetchProducts = useCallback(async () => { setIsUpdating(true); try { @@ -588,8 +593,12 @@ export default function ArchivePage({ }, [selectedCategory, selectedPet, searchQuery, sortBy, sortOrder, activeSymptoms]); useEffect(() => { + if (hasInitialProducts && !didSkipInitialFetch.current) { + didSkipInitialFetch.current = true; + return; + } Promise.resolve().then(() => fetchProducts()); - }, [fetchProducts]); + }, [fetchProducts, hasInitialProducts]); const toggleSymptom = (s: string) => { setActiveSymptoms(prev => prev.includes(s) ? prev.filter(item => item !== s) : [...prev, s]); diff --git a/frontend/application/components/__tests__/ArchivePage.test.tsx b/frontend/application/components/__tests__/ArchivePage.test.tsx index bd3e2da2..41755351 100644 --- a/frontend/application/components/__tests__/ArchivePage.test.tsx +++ b/frontend/application/components/__tests__/ArchivePage.test.tsx @@ -144,6 +144,14 @@ const mockProducts = [ }); describe("ArchivePage Component", () => { + it("renders server-provided initial products without waiting for a client fetch", () => { + render(); + + expect(screen.getAllByText("گلوکوزامین مفاصل").length).toBeGreaterThan(0); + expect(productService.getProducts).not.toHaveBeenCalled(); + }); + + beforeEach(() => { vi.clearAllMocks();