From 7e50a9af65df9b8948fcbfe135ddb6f9c412e4e1 Mon Sep 17 00:00:00 2001 From: Christian Vidal Wolf Date: Wed, 21 Jan 2026 16:49:11 +0100 Subject: [PATCH] feat: add explicit metric toggle for Weekly Sales sorting - Added Units/Spend toggle to the toolbar. - Refactored column sorting to use the active toggle metric. - Simplified sort cycling to just toggle direction (DESC/ASC). - Fixed JSX structure and code duplication. --- components/WeeklyGrid.tsx | 59 ++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/components/WeeklyGrid.tsx b/components/WeeklyGrid.tsx index 11404f6..3a15ce5 100644 --- a/components/WeeklyGrid.tsx +++ b/components/WeeklyGrid.tsx @@ -19,6 +19,7 @@ const WeeklyGrid: React.FC = ({ data }) => { const [searchTerm, setSearchTerm] = useState(''); const [currentPage, setCurrentPage] = useState(1); + const [activeMetric, setActiveMetric] = useState<'units' | 'spend'>('units'); // Default sort: most recent week, descending, units const [sortConfig, setSortConfig] = useState(() => { @@ -35,17 +36,12 @@ const WeeklyGrid: React.FC = ({ data }) => { const handleSort = (weekKey: string) => { setSortConfig(prev => { - if (prev?.key === weekKey) { - // Cycle: (Units, desc) -> (Units, asc) -> (Spend, desc) -> (Spend, asc) - if (prev.metric === 'units') { - if (prev.direction === 'desc') return { key: weekKey, direction: 'asc', metric: 'units' }; - return { key: weekKey, direction: 'desc', metric: 'spend' }; - } else { - if (prev.direction === 'desc') return { key: weekKey, direction: 'asc', metric: 'spend' }; - return { key: weekKey, direction: 'desc', metric: 'units' }; - } + if (prev?.key === weekKey && prev.metric === activeMetric) { + // Just toggle direction if already sorting by this week and metric + return { key: weekKey, direction: prev.direction === 'asc' ? 'desc' : 'asc', metric: activeMetric }; } - return { key: weekKey, direction: 'desc', metric: 'units' }; + // Start fresh with DESC for the current active metric + return { key: weekKey, direction: 'desc', metric: activeMetric }; }); }; @@ -112,19 +108,36 @@ const WeeklyGrid: React.FC = ({ data }) => { return (
- {/* Toolbar: Search & Pagination */} -
-
- setSearchTerm(e.target.value)} - className="w-full bg-slate-950 border border-white/10 rounded-lg px-4 py-2 text-base text-white focus:outline-none focus:ring-2 focus:ring-indigo-500 transition-all pl-10" - /> - - - + {/* Toolbar: Search & Metric Toggle & Pagination */} +
+
+
+ setSearchTerm(e.target.value)} + className="w-full bg-slate-950 border border-white/10 rounded-lg px-4 py-2 text-base text-white focus:outline-none focus:ring-2 focus:ring-indigo-500 transition-all pl-10" + /> + + + +
+ +
+ + +