diff --git a/components/WeeklyGrid.tsx b/components/WeeklyGrid.tsx index 6c004da..11404f6 100644 --- a/components/WeeklyGrid.tsx +++ b/components/WeeklyGrid.tsx @@ -9,6 +9,7 @@ interface WeeklyGridProps { type SortConfig = { key: string; // weekKey direction: 'asc' | 'desc'; + metric: 'units' | 'spend'; } | null; const ROWS_PER_PAGE = 50; @@ -19,10 +20,10 @@ const WeeklyGrid: React.FC = ({ data }) => { const [searchTerm, setSearchTerm] = useState(''); const [currentPage, setCurrentPage] = useState(1); - // Default sort: most recent week, descending + // Default sort: most recent week, descending, units const [sortConfig, setSortConfig] = useState(() => { if (weeks.length > 0) { - return { key: weeks[0], direction: 'desc' }; + return { key: weeks[0], direction: 'desc', metric: 'units' }; } return null; }); @@ -35,9 +36,16 @@ const WeeklyGrid: React.FC = ({ data }) => { const handleSort = (weekKey: string) => { setSortConfig(prev => { if (prev?.key === weekKey) { - return { key: weekKey, direction: prev.direction === 'asc' ? 'desc' : 'asc' }; + // 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' }; + } } - return { key: weekKey, direction: 'desc' }; + return { key: weekKey, direction: 'desc', metric: 'units' }; }); }; @@ -57,8 +65,9 @@ const WeeklyGrid: React.FC = ({ data }) => { const result = [...filteredRows]; if (sortConfig) { result.sort((a, b) => { - const valA = a.unitsByWeek[sortConfig.key] || 0; - const valB = b.unitsByWeek[sortConfig.key] || 0; + const metricKey = sortConfig.metric === 'units' ? 'unitsByWeek' : 'spendByWeek'; + const valA = a[metricKey][sortConfig.key] || 0; + const valB = b[metricKey][sortConfig.key] || 0; if (sortConfig.direction === 'asc') { return valA - valB; } @@ -95,7 +104,7 @@ const WeeklyGrid: React.FC = ({ data }) => { const isPositive = pct >= 0; return ( - + {isPositive ? '▲' : '▼'}{Math.abs(pct).toFixed(0)}% ); @@ -111,34 +120,34 @@ const WeeklyGrid: React.FC = ({ data }) => { placeholder="Search SKU, Title, or Line..." value={searchTerm} onChange={(e) => setSearchTerm(e.target.value)} - className="w-full bg-slate-950 border border-white/10 rounded-lg px-4 py-2 text-sm text-white focus:outline-none focus:ring-2 focus:ring-indigo-500 transition-all pl-10" + 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" /> - +
- + Showing {Math.min(sortedRows.length, (currentPage - 1) * ROWS_PER_PAGE + 1)}-{Math.min(sortedRows.length, currentPage * ROWS_PER_PAGE)} of {sortedRows.length}
- + {currentPage} / {Math.max(1, totalPages)}
@@ -149,33 +158,36 @@ const WeeklyGrid: React.FC = ({ data }) => { - + {weeks.map(week => ( ))} - + {weeks.map((week, idx) => ( - - {weeks.map((week, idx) => { @@ -199,16 +211,16 @@ const WeeklyGrid: React.FC = ({ data }) => { const prevVal = row.unitsByWeek[weeks[idx + 1]] || 0; const spend = row.spendByWeek[week] || 0; return ( - -
Product DetailsProduct Details handleSort(week)} > -
- {week.split('-')[1]}/{week.split('-')[0].slice(-2)} +
+ {week.split('-')[1]}/{week.split('-')[0].slice(-2)} {sortConfig?.key === week && ( - {sortConfig.direction === 'asc' ? '↑' : '↓'} +
+ {sortConfig.direction === 'asc' ? '↑' : '↓'} + {sortConfig.metric} +
)} -
Units / Spend
+
Units / Spend
TOTALSTOTALS +
{weekTotals[week].units.toLocaleString('de-DE')} {renderGrowth(weekTotals[week].units, weekTotals[weeks[idx + 1]]?.units)}
-
+
€{weekTotals[week].spend.toLocaleString('de-DE', { minimumFractionDigits: 0, maximumFractionDigits: 0 })}
@@ -187,11 +199,11 @@ const WeeklyGrid: React.FC = ({ data }) => { {paginatedRows.length > 0 ? ( paginatedRows.map((row) => (
+
- {row.sku} - {row.title} - {row.line} + {row.sku} + {row.title} + {row.line}
-
-
- 0 ? (sortConfig?.key === week ? 'text-indigo-400' : 'text-white') : 'text-slate-700'}`}> +
+
+
+ 0 ? (sortConfig?.key === week && sortConfig.metric === 'units' ? 'text-indigo-400' : 'text-white') : 'text-slate-700'}`}> {val > 0 ? val.toLocaleString('de-DE') : '-'} {val > 0 && renderGrowth(val, prevVal)}
{spend > 0 && ( - + €{spend.toLocaleString('de-DE', { minimumFractionDigits: 0, maximumFractionDigits: 0 })} )} @@ -220,7 +232,7 @@ const WeeklyGrid: React.FC = ({ data }) => { )) ) : (
+ No SKUs found matching "{searchTerm}"