This commit is contained in:
1 parent
1de8fd9a60
commit
bec76393bb
3 files changed
+25
-4
No files matched your search
@@ -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 (
|
||||
<ArchivePage
|
||||
@@ -48,6 +51,7 @@ export default async function Shop({ searchParams }: { searchParams: Promise<{ [
|
||||
initialSearch={search}
|
||||
initialPetType={petType}
|
||||
initialSymptoms={symptoms}
|
||||
initialProducts={initialProducts}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -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<string[]>(initialSymptoms ? initialSymptoms.split(',') : []);
|
||||
const [searchQuery, setSearchQuery] = useState(initialSearch);
|
||||
const [isUpdating, setIsUpdating] = useState(false);
|
||||
const [filteredProducts, setFilteredProducts] = useState<Product[]>([]);
|
||||
const [filteredProducts, setFilteredProducts] = useState<Product[]>(() => orderProductsByAvailability(initialProducts));
|
||||
const [sortBy, setSortBy] = useState<string>("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]);
|
||||
|
||||
@@ -144,6 +144,14 @@ const mockProducts = [
|
||||
});
|
||||
|
||||
describe("ArchivePage Component", () => {
|
||||
it("renders server-provided initial products without waiting for a client fetch", () => {
|
||||
render(<ArchivePage initialProducts={mockProducts as any} />);
|
||||
|
||||
expect(screen.getAllByText("گلوکوزامین مفاصل").length).toBeGreaterThan(0);
|
||||
expect(productService.getProducts).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
|
||||
Reference in new issue
Block a user