eeshaAI commited on
Commit
f33b315
·
1 Parent(s): 03fda90

fix: categories page — All tab + empty category + subcategory search

Browse files
Files changed (2) hide show
  1. package.json +1 -1
  2. src/app/categories/page.tsx +45 -14
package.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
  "name": "nextjs_tailwind_shadcn_ts",
3
- "version": "0.2.11",
4
  "private": true,
5
  "scripts": {
6
  "dev": "next dev -p 3000 2>&1 | tee dev.log",
 
1
  {
2
  "name": "nextjs_tailwind_shadcn_ts",
3
+ "version": "0.2.12",
4
  "private": true,
5
  "scripts": {
6
  "dev": "next dev -p 3000 2>&1 | tee dev.log",
src/app/categories/page.tsx CHANGED
@@ -62,11 +62,32 @@ function CategoriesContent() {
62
  if (result.success) {
63
  setProducts(result.results || result.products || []);
64
  }
65
- } else {
 
66
  const result = await api.products.category(category, sort, 1);
67
  if (result.success) {
68
  setProducts(result.products || []);
69
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  }
71
  setLoading(false);
72
  }, [category, sort, searchQuery]);
@@ -101,6 +122,19 @@ function CategoriesContent() {
101
  }
102
  };
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  const activeCategoryLabel = ALL_CATEGORIES.find(c => c.value === category)?.label || 'All';
105
  const subcats = category ? SUBCATEGORIES[category] || [] : [];
106
 
@@ -131,12 +165,12 @@ function CategoriesContent() {
131
  {/* Category tabs with underline */}
132
  <div className="flex items-center gap-5 px-3 py-2 overflow-x-auto no-scrollbar bg-white border-b border-slate-100">
133
  {ALL_CATEGORIES.map((cat) => {
134
- // "All" navigates to homepage, others filter in-page
135
  if (cat.value === '') {
136
  return (
137
- <Link
138
  key={cat.value}
139
- href="/"
140
  className={`text-sm whitespace-nowrap pb-1.5 transition-colors ${
141
  category === ''
142
  ? 'text-primary font-bold border-b-2 border-primary'
@@ -144,7 +178,7 @@ function CategoriesContent() {
144
  }`}
145
  >
146
  {cat.label}
147
- </Link>
148
  );
149
  }
150
  return (
@@ -169,10 +203,7 @@ function CategoriesContent() {
169
  {subcats.map((sub, i) => (
170
  <button
171
  key={sub}
172
- onClick={() => {
173
- setSearchQuery(sub);
174
- load();
175
- }}
176
  className="flex flex-col items-center gap-1"
177
  >
178
  <div className="w-11 h-11 rounded-full bg-slate-50 border border-slate-100 flex items-center justify-center text-lg">
@@ -319,12 +350,12 @@ function CategoriesContent() {
319
  <div className="flex items-center justify-between mb-4">
320
  <div className="flex items-center gap-2 overflow-x-auto no-scrollbar">
321
  {ALL_CATEGORIES.map((cat) => {
322
- // "All" navigates to homepage
323
  if (cat.value === '') {
324
  return (
325
- <Link
326
  key={cat.value}
327
- href="/"
328
  className={`text-sm font-medium px-4 py-1.5 rounded-full whitespace-nowrap transition-all ${
329
  category === ''
330
  ? 'brand-gradient text-white font-bold'
@@ -332,7 +363,7 @@ function CategoriesContent() {
332
  }`}
333
  >
334
  {cat.emoji} {cat.label}
335
- </Link>
336
  );
337
  }
338
  return (
@@ -389,7 +420,7 @@ function CategoriesContent() {
389
  {subcats.map((sub) => (
390
  <button
391
  key={sub}
392
- onClick={() => { setSearchQuery(sub); load(); }}
393
  className="shrink-0 text-xs font-medium text-black bg-slate-50 hover:bg-primary/10 hover:text-primary px-3 py-1.5 rounded-full border border-slate-100"
394
  >
395
  {sub}
 
62
  if (result.success) {
63
  setProducts(result.results || result.products || []);
64
  }
65
+ } else if (category) {
66
+ // Specific category selected — use the category op
67
  const result = await api.products.category(category, sort, 1);
68
  if (result.success) {
69
  setProducts(result.products || []);
70
  }
71
+ } else {
72
+ // "All" categories — use the all op (category op rejects empty string)
73
+ const result = await api.products.all(100);
74
+ if (result.success) {
75
+ let allProducts = result.products || [];
76
+ // Apply client-side sort since the 'all' op doesn't sort
77
+ if (sort === 'price_low') {
78
+ allProducts = [...allProducts].sort((a, b) => a.price - b.price);
79
+ } else if (sort === 'price_high') {
80
+ allProducts = [...allProducts].sort((a, b) => b.price - a.price);
81
+ } else if (sort === 'popular') {
82
+ allProducts = [...allProducts].sort((a, b) => (b.units_sold || 0) - (a.units_sold || 0));
83
+ } else {
84
+ // newest — sort by created_at descending
85
+ allProducts = [...allProducts].sort((a, b) =>
86
+ new Date(b.created_at || 0).getTime() - new Date(a.created_at || 0).getTime()
87
+ );
88
+ }
89
+ setProducts(allProducts);
90
+ }
91
  }
92
  setLoading(false);
93
  }, [category, sort, searchQuery]);
 
122
  }
123
  };
124
 
125
+ // Search by subcategory label — sets the query and triggers a search.
126
+ // We can't rely on the load() callback because it captures the old searchQuery;
127
+ // instead we call the search API directly with the new value.
128
+ const searchBySubcategory = async (sub: string) => {
129
+ setLoading(true);
130
+ setSearchQuery(sub);
131
+ const result = await api.products.search(sub, null);
132
+ if (result.success) {
133
+ setProducts(result.results || result.products || []);
134
+ }
135
+ setLoading(false);
136
+ };
137
+
138
  const activeCategoryLabel = ALL_CATEGORIES.find(c => c.value === category)?.label || 'All';
139
  const subcats = category ? SUBCATEGORIES[category] || [] : [];
140
 
 
165
  {/* Category tabs with underline */}
166
  <div className="flex items-center gap-5 px-3 py-2 overflow-x-auto no-scrollbar bg-white border-b border-slate-100">
167
  {ALL_CATEGORIES.map((cat) => {
168
+ // "All" clears the category filter (stays on categories page)
169
  if (cat.value === '') {
170
  return (
171
+ <button
172
  key={cat.value}
173
+ onClick={() => handleCategoryChange('')}
174
  className={`text-sm whitespace-nowrap pb-1.5 transition-colors ${
175
  category === ''
176
  ? 'text-primary font-bold border-b-2 border-primary'
 
178
  }`}
179
  >
180
  {cat.label}
181
+ </button>
182
  );
183
  }
184
  return (
 
203
  {subcats.map((sub, i) => (
204
  <button
205
  key={sub}
206
+ onClick={() => searchBySubcategory(sub)}
 
 
 
207
  className="flex flex-col items-center gap-1"
208
  >
209
  <div className="w-11 h-11 rounded-full bg-slate-50 border border-slate-100 flex items-center justify-center text-lg">
 
350
  <div className="flex items-center justify-between mb-4">
351
  <div className="flex items-center gap-2 overflow-x-auto no-scrollbar">
352
  {ALL_CATEGORIES.map((cat) => {
353
+ // "All" clears the category filter (stays on categories page)
354
  if (cat.value === '') {
355
  return (
356
+ <button
357
  key={cat.value}
358
+ onClick={() => handleCategoryChange('')}
359
  className={`text-sm font-medium px-4 py-1.5 rounded-full whitespace-nowrap transition-all ${
360
  category === ''
361
  ? 'brand-gradient text-white font-bold'
 
363
  }`}
364
  >
365
  {cat.emoji} {cat.label}
366
+ </button>
367
  );
368
  }
369
  return (
 
420
  {subcats.map((sub) => (
421
  <button
422
  key={sub}
423
+ onClick={() => searchBySubcategory(sub)}
424
  className="shrink-0 text-xs font-medium text-black bg-slate-50 hover:bg-primary/10 hover:text-primary px-3 py-1.5 rounded-full border border-slate-100"
425
  >
426
  {sub}