Skip to content

Commit 3fef702

Browse files
committed
updated chartStore
1 parent 20e8318 commit 3fef702

1 file changed

Lines changed: 70 additions & 30 deletions

File tree

ethernet-view/src/components/ChartMenu/ChartStore.ts

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,86 @@ import { NumericMeasurementInfo, MeasurementId } from 'common';
33

44
export type ChartId = string;
55

6-
type ChartSet = Map<ChartId, NumericMeasurementInfo[]>;
7-
6+
type ChartInfo = {
7+
chartId: ChartId,
8+
measurements: NumericMeasurementInfo[]
9+
};
10+
type ChartSet = ChartInfo[];
811
type ChartStore = {
912
charts: ChartSet;
1013
addChart: (chartId: ChartId, initialMeasurement: NumericMeasurementInfo) => void;
1114
removeChart: (chartId: ChartId) => void;
12-
addMeasurement: (chartId: ChartId, measurement: NumericMeasurementInfo) => void;
13-
removeMeasurement: (chartId: ChartId, measurementId: MeasurementId) => void;
15+
addMeasurementToChart: (chartId: ChartId, measurement: NumericMeasurementInfo) => void;
16+
removeMeasurementFromChart: (chartId: ChartId, measurementId: MeasurementId) => void;
17+
getMeasurementsFromChart: (chartId: ChartId) => NumericMeasurementInfo[] | undefined;
1418
};
1519

16-
export const useChartStore = create<ChartStore>((set) => ({
17-
charts: new Map(),
20+
export const useChartStore = create<ChartStore>((set, get) => ({
21+
charts: [] as ChartSet,
22+
1823
addChart: (chartId: ChartId, initialMeasurement: NumericMeasurementInfo) => {
19-
set(() => ({
20-
charts: new Map().set(chartId, [initialMeasurement])
24+
const newChart = {
25+
chartId: chartId,
26+
measurements: [initialMeasurement]
27+
};
28+
29+
set(state => ({
30+
...state,
31+
charts: [...state.charts, newChart]
2132
}));
2233
},
2334
removeChart: (chartId: ChartId) => {
24-
set((state) => ({
25-
charts: new Map([...state.charts].filter(([id, _]) => id !== chartId))
26-
}));
35+
const chartsDraft = get().charts;
36+
const newCharts = chartsDraft.filter(chart => chart.chartId !== chartId);
37+
38+
set(state => ({
39+
...state,
40+
charts: newCharts
41+
}));
2742
},
28-
addMeasurement: (chartId: ChartId, measurement: NumericMeasurementInfo) => {
29-
set((state) => ({
30-
charts: new Map([...state.charts].map(([id, measurements]) => {
31-
if (id === chartId) {
32-
measurements.push(measurement);
33-
}
34-
return [id, measurements];
35-
}))
36-
}));
43+
addMeasurementToChart: (chartId: ChartId, measurement: NumericMeasurementInfo) => {
44+
const chartsDraft = get().charts;
45+
const chartIndex = chartsDraft.findIndex(chart => chart.chartId === chartId);
46+
if(chartIndex != -1) {
47+
const chart = chartsDraft[chartIndex];
48+
if(!isMeasurementInChart(measurement.id, chart)) {
49+
chart.measurements.push(measurement);
50+
51+
set(state => ({
52+
...state,
53+
charts: [
54+
...state.charts.slice(0, chartIndex),
55+
chart,
56+
...state.charts.slice(chartIndex + 1)
57+
]
58+
}));
59+
}
60+
}
3761
},
38-
removeMeasurement: (chartId: ChartId, measurementId: MeasurementId) => {
39-
set((state) => ({
40-
charts: new Map([...state.charts].map(([id, measurements]) => {
41-
if (id === chartId) {
42-
return [id, measurements.filter((m) => m.id !== measurementId)];
43-
}
44-
return [id, measurements];
45-
}))
46-
}));
62+
removeMeasurementFromChart: (chartId: ChartId, measurementId: MeasurementId) => {
63+
const chartsDraft = get().charts;
64+
const chartIndex = chartsDraft.findIndex(chart => chart.chartId === chartId);
65+
if(chartIndex != -1) {
66+
const chart = chartsDraft[chartIndex];
67+
const newMeasurements = chart.measurements.filter(measurement => measurement.id !== measurementId);
68+
chart.measurements = newMeasurements;
69+
70+
set(state => ({
71+
...state,
72+
charts: [
73+
...state.charts.slice(0, chartIndex),
74+
chart,
75+
...state.charts.slice(chartIndex + 1)
76+
]
77+
}));
78+
}
79+
},
80+
getMeasurementsFromChart: (chartId: ChartId) => {
81+
const chart = get().charts.find(chart => chart.chartId === chartId);
82+
return chart ? chart.measurements : undefined;
4783
}
48-
}));
84+
}));
85+
86+
function isMeasurementInChart(measurementId: MeasurementId, chart: ChartInfo) {
87+
return chart.measurements.some(measurement => measurement.id === measurementId);
88+
}

0 commit comments

Comments
 (0)