using new state management

This commit is contained in:
2026-05-05 12:58:59 +05:30
parent 0a92126b92
commit 34594215f9
3 changed files with 34 additions and 20 deletions

View File

@@ -1,3 +1,9 @@
import {
DashboardMode,
DashboardPeriodType,
DashboardSelectedPeriodId
} from "../Dashboard";
export interface _ChartDataPoint { export interface _ChartDataPoint {
id: string; id: string;
amount: number; amount: number;
@@ -18,11 +24,19 @@ export interface HistoryChartProps {
summary?: string; summary?: string;
tabs: string[]; tabs: string[];
data: ChartData; data: ChartData;
comparison: boolean;
setComparison: (v: boolean) => void;
colorScheme: { colorScheme: {
primary: string; primary: string;
light: string; light: string;
text: string; text: string;
}; };
// State management
mode: DashboardMode;
periodType: DashboardPeriodType;
selectedPeriodId: DashboardSelectedPeriodId;
comparison: boolean;
togglePeriodType: () => void;
setSelectedPeriodId: (id: string | null) => void;
toggleComparison: () => void;
} }

View File

@@ -3,7 +3,7 @@ import { ChartDataPoint, HistoryChartProps, ChartData } from "./HistoryChart.mod
import HistoryChartView from "./HistoryChart.view"; import HistoryChartView from "./HistoryChart.view";
export default function HistoryChart(props: HistoryChartProps) { export default function HistoryChart(props: HistoryChartProps) {
const { tabs, data, periodType, comparison } = props; const { tabs, data, mode, periodType, comparison } = props;
const [activeTab, setActiveTab] = React.useState<string>(tabs[0] || ""); const [activeTab, setActiveTab] = React.useState<string>(tabs[0] || "");
const [startIndex, setStartIndex] = React.useState(0); const [startIndex, setStartIndex] = React.useState(0);
@@ -12,12 +12,8 @@ export default function HistoryChart(props: HistoryChartProps) {
let rawData: ChartDataPoint[] = []; let rawData: ChartDataPoint[] = [];
if (activeDataKey === "daily") { const section = data[activeDataKey];
rawData = data.daily || []; rawData = section?.[periodType] || [];
} else {
const section = data[activeDataKey];
rawData = section?.[periodType] || [];
}
const currentData = rawData; const currentData = rawData;

View File

@@ -26,8 +26,6 @@ interface ViewProps extends HistoryChartProps {
startIndex: number; startIndex: number;
setStartIndex: React.Dispatch<React.SetStateAction<number>>; setStartIndex: React.Dispatch<React.SetStateAction<number>>;
activeDataKey: string; activeDataKey: string;
selectedPeriodId: string | null;
onSelectPeriodId: (id: string | null) => void;
} }
export default function HistoryChartView(props: ViewProps) { export default function HistoryChartView(props: ViewProps) {
@@ -35,11 +33,19 @@ export default function HistoryChartView(props: ViewProps) {
header, header,
summary, summary,
tabs, tabs,
periodType,
onPeriodTypeChange,
comparison,
setComparison,
colorScheme, colorScheme,
// State management
mode,
periodType,
selectedPeriodId,
comparison,
togglePeriodType,
setSelectedPeriodId,
toggleComparison,
// HistoryChart state management
activeTab, activeTab,
setActiveTab, setActiveTab,
currentData, currentData,
@@ -49,8 +55,6 @@ export default function HistoryChartView(props: ViewProps) {
startIndex, startIndex,
setStartIndex, setStartIndex,
activeDataKey, activeDataKey,
selectedPeriodId,
onSelectPeriodId,
} = props; } = props;
const theme = useTheme(); const theme = useTheme();
@@ -103,7 +107,7 @@ export default function HistoryChartView(props: ViewProps) {
</ToggleButtonGroup> </ToggleButtonGroup>
<Box sx={{ display: "flex", alignItems: "center", justifyContent: "space-between", mb: 3 }}> <Box sx={{ display: "flex", alignItems: "center", justifyContent: "space-between", mb: 3 }}>
<ToggleButtonGroup value={periodType} exclusive onChange={(_, v) => v && onPeriodTypeChange(v)} size="small"> <ToggleButtonGroup value={periodType} exclusive onChange={togglePeriodType} size="small">
<ToggleButton value="rolling">Rolling</ToggleButton> <ToggleButton value="rolling">Rolling</ToggleButton>
<ToggleButton value="calendar" disabled={activeDataKey === "daily"}> <ToggleButton value="calendar" disabled={activeDataKey === "daily"}>
Calendar Calendar
@@ -113,7 +117,7 @@ export default function HistoryChartView(props: ViewProps) {
<ToggleButton <ToggleButton
value="compare" value="compare"
selected={comparison} selected={comparison}
onChange={() => setComparison(!comparison)} onChange={toggleComparison}
size="small" size="small"
sx={{ sx={{
textTransform: "none", textTransform: "none",
@@ -169,7 +173,7 @@ export default function HistoryChartView(props: ViewProps) {
return ( return (
<Box <Box
key={point.id} key={point.id}
onClick={() => onSelectPeriodId(isSelected ? null : point.id)} onClick={() => setSelectedPeriodId(isSelected ? null : point.id)}
sx={{ sx={{
flex: 1, flex: 1,
display: "flex", display: "flex",