|
|
import { PayloadAction, createSlice } from '@reduxjs/toolkit'; |
|
|
import { BrushStartEndIndex } from '../context/brushUpdateContext'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type ChartData = unknown[]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type AppliedChartData = ReadonlyArray<{ value: unknown }>; |
|
|
|
|
|
export type ChartDataState = { |
|
|
chartData: ChartData | undefined; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
computedData: unknown | undefined; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dataStartIndex: number; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dataEndIndex: number; |
|
|
}; |
|
|
|
|
|
export const initialChartDataState: ChartDataState = { |
|
|
chartData: undefined, |
|
|
computedData: undefined, |
|
|
dataStartIndex: 0, |
|
|
dataEndIndex: 0, |
|
|
}; |
|
|
|
|
|
type BrushStartEndIndexActionPayload = Partial<BrushStartEndIndex>; |
|
|
|
|
|
const chartDataSlice = createSlice({ |
|
|
name: 'chartData', |
|
|
initialState: initialChartDataState, |
|
|
reducers: { |
|
|
setChartData(state, action: PayloadAction<ChartData | undefined>) { |
|
|
state.chartData = action.payload; |
|
|
if (action.payload == null) { |
|
|
state.dataStartIndex = 0; |
|
|
state.dataEndIndex = 0; |
|
|
return; |
|
|
} |
|
|
if (action.payload.length > 0 && state.dataEndIndex !== action.payload.length - 1) { |
|
|
state.dataEndIndex = action.payload.length - 1; |
|
|
} |
|
|
}, |
|
|
setComputedData(state, action: PayloadAction<unknown | undefined>) { |
|
|
state.computedData = action.payload; |
|
|
}, |
|
|
setDataStartEndIndexes(state, action: PayloadAction<BrushStartEndIndexActionPayload>) { |
|
|
const { startIndex, endIndex } = action.payload; |
|
|
if (startIndex != null) { |
|
|
state.dataStartIndex = startIndex; |
|
|
} |
|
|
if (endIndex != null) { |
|
|
state.dataEndIndex = endIndex; |
|
|
} |
|
|
}, |
|
|
}, |
|
|
}); |
|
|
|
|
|
export const { setChartData, setDataStartEndIndexes, setComputedData } = chartDataSlice.actions; |
|
|
|
|
|
export const chartDataReducer = chartDataSlice.reducer; |
|
|
|