From 80d9dc2f454a685efb478fc9033295a005cd0c0b Mon Sep 17 00:00:00 2001 From: devops Date: Sat, 17 Oct 2026 11:18:52 +0000 Subject: [PATCH] fix(wiki): compact mobile cards and add pagination --- .../application/components/IngredientWiki.tsx | 95 +++++++++++++------ .../__tests__/IngredientWiki.test.tsx | 77 ++++++++++++++- 2 files changed, 141 insertions(+), 31 deletions(-) diff --git a/frontend/application/components/IngredientWiki.tsx b/frontend/application/components/IngredientWiki.tsx index 91d9503d..ec4d50d3 100644 --- a/frontend/application/components/IngredientWiki.tsx +++ b/frontend/application/components/IngredientWiki.tsx @@ -51,12 +51,12 @@ function formatIngredientDisplayName(item: { nameFa?: string; scientificName?: s const rawName = item.nameFa || item.name || ''; const cleanFa = rawName.replace(/\s*\([^)]*\)\s*/g, ' ').replace(/\s+/g, ' ').trim(); const sub = (item.scientificName || item.nameEn || '').replace(/\s*\([^)]*\)\s*/g, ' ').replace(/\s+/g, ' ').trim(); - + if (!cleanFa) return sub || rawName || item.slug || ''; if (!sub || cleanFa.toLowerCase() === sub.toLowerCase() || /[\u0600-\u06FF]/.test(sub)) { return cleanFa; } - + return `${cleanFa} (${sub})`; } @@ -67,6 +67,7 @@ export default function IngredientWiki() { const [ingredientsWiki, setIngredientsWiki] = useState([]); const [products, setProducts] = useState([]); const [searchQuery, setSearchQuery] = useState(""); + const [visibleCount, setVisibleCount] = useState(8); useEffect(() => { productService.getProducts({ limit: 999 }) @@ -110,7 +111,15 @@ export default function IngredientWiki() { }, 200); } } - }, [termParam, ingredientsWiki]); + }, [termParam, ingredientsWiki, visibleCount]); + + useEffect(() => { + if (!termParam || ingredientsWiki.length === 0) return; + const targetIndex = ingredientsWiki.findIndex(ing => ing.id === termParam); + if (targetIndex >= visibleCount) { + setVisibleCount(Math.ceil((targetIndex + 1) / 8) * 8); + } + }, [termParam, ingredientsWiki, visibleCount]); // Product matcher for each ingredient const getProductsForIngredient = useMemo(() => { @@ -146,11 +155,11 @@ export default function IngredientWiki() { }, [products]); return ( -
+
- + {/* Standard Breadcrumb Navigation & Integrated Back Button */} -
+
router.push('/')}>خانه @@ -183,7 +192,10 @@ export default function IngredientWiki() { type="text" placeholder="جستجوی ماده مؤثره (مثلاً: صدف لب‌سبز، آغوز، بیوتین)..." value={searchQuery} - onChange={(e) => setSearchQuery(e.target.value)} + onChange={(e) => { + setSearchQuery(e.target.value); + setVisibleCount(8); + }} className="w-full bg-white border border-medical-gray-200 rounded-2xl py-2.5 sm:py-3 pr-10 pl-4 text-xs sm:text-sm focus:outline-none focus:ring-2 focus:ring-canina-blue/20 shadow-xs" />
@@ -218,15 +230,20 @@ export default function IngredientWiki() { ); } + const visibleIngredients = filtered.slice(0, visibleCount); + const remainingCount = Math.max(filtered.length - visibleCount, 0); + const nextCount = Math.min(8, remainingCount); + return ( -
- {filtered.map((ing, idx) => { + <> +
+ {visibleIngredients.map((ing, idx) => { const hasIngredient = getProductsForIngredient(ing.id, ing.name); const isTargeted = termParam === ing.id; return ( -
@@ -247,7 +264,7 @@ export default function IngredientWiki() { کد ماده مؤثره: {ing.id}
- +

{ing.description}

@@ -265,7 +282,7 @@ export default function IngredientWiki() { {/* Products Linking */}
-
- -
+ +
{hasIngredient.length > 0 ? hasIngredient.slice(0, 4).map(p => ( - -
- {p.name} + {p.name} { e.currentTarget.src = '/assets/images/canina-product-placeholder.png'; }} />
-

{p.name}

+

{p.name}

)) : (
@@ -321,7 +341,7 @@ export default function IngredientWiki() { {hasIngredient.length > 4 && (
- @@ -330,7 +350,7 @@ export default function IngredientWiki() {
)} - + {/* Decorative backdrop */}
@@ -338,7 +358,24 @@ export default function IngredientWiki() {
); })} -
+
+ + {remainingCount > 0 && ( +
+ + + نمایش {Math.min(visibleCount, filtered.length).toLocaleString('fa-IR')} از {filtered.length.toLocaleString('fa-IR')} ماده + +
+ )} + ); })()}
diff --git a/frontend/application/components/__tests__/IngredientWiki.test.tsx b/frontend/application/components/__tests__/IngredientWiki.test.tsx index 99ede074..3f75c227 100644 --- a/frontend/application/components/__tests__/IngredientWiki.test.tsx +++ b/frontend/application/components/__tests__/IngredientWiki.test.tsx @@ -32,8 +32,8 @@ vi.mock('next/image', () => ({ // Mock next/link vi.mock('next/link', () => ({ - default: ({ children, href, className }: any) => ( - + default: ({ children, href, className, ...props }: any) => ( + {children} ), @@ -217,4 +217,77 @@ describe('IngredientWiki Component', () => { expect(screen.getByText(/آغوز گاو/)).toBeInTheDocument(); }); }); + + it('shows only the first 8 ingredients and loads the next page on demand', async () => { + (productService.getProducts as any).mockResolvedValueOnce({ data: [] }); + const ingredients = Array.from({ length: 18 }, (_, index) => ({ + id: `ingredient-${index + 1}`, + slug: `ingredient-${index + 1}`, + nameFa: `ماده شماره ${index + 1}`, + description: `توضیح ماده ${index + 1}`, + benefits: [], + })); + (api.get as any).mockResolvedValueOnce({ data: ingredients }); + + render(); + + await waitFor(() => expect(screen.getByText('ماده شماره 1')).toBeInTheDocument()); + expect(screen.getByText('ماده شماره 8')).toBeInTheDocument(); + expect(screen.queryByText('ماده شماره 9')).not.toBeInTheDocument(); + expect(screen.getByText(/نمایش ۸ از ۱۸ ماده/)).toBeInTheDocument(); + + fireEvent.click(screen.getByRole('button', { name: /مشاهده ۸ ماده بیشتر/ })); + + expect(screen.getByText('ماده شماره 16')).toBeInTheDocument(); + expect(screen.queryByText('ماده شماره 17')).not.toBeInTheDocument(); + expect(screen.getByText(/نمایش ۱۶ از ۱۸ ماده/)).toBeInTheDocument(); + }); + + it('resets ingredient pagination when the search query changes', async () => { + (productService.getProducts as any).mockResolvedValueOnce({ data: [] }); + const ingredients = Array.from({ length: 12 }, (_, index) => ({ + id: `item-${index + 1}`, + slug: `item-${index + 1}`, + nameFa: index < 10 ? `مشترک ${index + 1}` : `دیگر ${index + 1}`, + description: 'توضیح مشترک', + benefits: [], + })); + (api.get as any).mockResolvedValueOnce({ data: ingredients }); + + render(); + await waitFor(() => expect(screen.getByText('مشترک 1')).toBeInTheDocument()); + fireEvent.click(screen.getByRole('button', { name: /مشاهده ۴ ماده بیشتر/ })); + expect(screen.getByText('دیگر 12')).toBeInTheDocument(); + + fireEvent.change(screen.getByPlaceholderText(/جستجوی ماده مؤثره/), { target: { value: 'مشترک' } }); + expect(screen.getByText('مشترک 8')).toBeInTheDocument(); + expect(screen.queryByText('مشترک 9')).not.toBeInTheDocument(); + }); + + it('uses compact two-column related-product cards on mobile', async () => { + (api.get as any).mockResolvedValueOnce({ data: [{ + id: 'biotin', slug: 'biotin', nameFa: 'بیوتین', description: 'توضیح', benefits: [], + }] }); + (productService.getProducts as any).mockResolvedValueOnce({ data: [{ + id: 'p-1', slug: 'biotin-tabs', name: 'محصول بیوتین', main_ingredients: ['بیوتین'], image: '/biotin.png', + }] }); + + render(); + await waitFor(() => expect(screen.getByText('محصول بیوتین')).toBeInTheDocument()); + + expect(screen.getByTestId('wiki-related-products-grid')).toHaveClass('grid-cols-2'); + expect(screen.getByTestId('wiki-related-product-card')).toHaveClass('p-2'); + expect(screen.getByTestId('wiki-related-product-image')).toHaveClass('h-20'); + }); + + + it('keeps the mobile breadcrumb close to the top edge', () => { + (productService.getProducts as any).mockResolvedValueOnce({ data: [] }); + (api.get as any).mockResolvedValueOnce({ data: [] }); + + const { container } = render(); + expect(container.firstElementChild).toHaveClass('pt-3', 'pb-8', 'sm:py-12'); + expect(container.firstElementChild).not.toHaveClass('py-12'); + }); + });