This commit is contained in:
1 parent
1de8fd9a60
commit
bec76393bb
3 files changed
+25
-4
No files matched your search
@@ -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