| 1 | import { createSlice } from '@reduxjs/toolkit';
|
|---|
| 2 | import { castDraft } from 'immer';
|
|---|
| 3 |
|
|---|
| 4 | /**
|
|---|
| 5 | * This is the data that's coming through main chart `data` prop
|
|---|
| 6 | * Recharts is very flexible in what it accepts so the type is very flexible too.
|
|---|
| 7 | * This will typically be an object, and various components will provide various `dataKey`
|
|---|
| 8 | * that dictates how to pull data from that object.
|
|---|
| 9 | *
|
|---|
| 10 | * TL;DR: before dataKey
|
|---|
| 11 | *
|
|---|
| 12 | * @inline
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * So this is the same unknown type as ChartData but this is after the dataKey has been applied.
|
|---|
| 17 | * We still don't know what the type is - that depends on what exactly it was before the dataKey application,
|
|---|
| 18 | * and the dataKey can return whatever anyway - but let's keep it separate as a form of documentation.
|
|---|
| 19 | *
|
|---|
| 20 | * TL;DR: ChartData after dataKey.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | export var initialChartDataState = {
|
|---|
| 24 | chartData: undefined,
|
|---|
| 25 | computedData: undefined,
|
|---|
| 26 | dataStartIndex: 0,
|
|---|
| 27 | dataEndIndex: 0
|
|---|
| 28 | };
|
|---|
| 29 | var chartDataSlice = createSlice({
|
|---|
| 30 | name: 'chartData',
|
|---|
| 31 | initialState: initialChartDataState,
|
|---|
| 32 | reducers: {
|
|---|
| 33 | setChartData(state, action) {
|
|---|
| 34 | state.chartData = castDraft(action.payload);
|
|---|
| 35 | if (action.payload == null) {
|
|---|
| 36 | state.dataStartIndex = 0;
|
|---|
| 37 | state.dataEndIndex = 0;
|
|---|
| 38 | return;
|
|---|
| 39 | }
|
|---|
| 40 | if (action.payload.length > 0 && state.dataEndIndex !== action.payload.length - 1) {
|
|---|
| 41 | state.dataEndIndex = action.payload.length - 1;
|
|---|
| 42 | }
|
|---|
| 43 | },
|
|---|
| 44 | setComputedData(state, action) {
|
|---|
| 45 | state.computedData = action.payload;
|
|---|
| 46 | },
|
|---|
| 47 | setDataStartEndIndexes(state, action) {
|
|---|
| 48 | var {
|
|---|
| 49 | startIndex,
|
|---|
| 50 | endIndex
|
|---|
| 51 | } = action.payload;
|
|---|
| 52 | if (startIndex != null) {
|
|---|
| 53 | state.dataStartIndex = startIndex;
|
|---|
| 54 | }
|
|---|
| 55 | if (endIndex != null) {
|
|---|
| 56 | state.dataEndIndex = endIndex;
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | });
|
|---|
| 61 | export var {
|
|---|
| 62 | setChartData,
|
|---|
| 63 | setDataStartEndIndexes,
|
|---|
| 64 | setComputedData
|
|---|
| 65 | } = chartDataSlice.actions;
|
|---|
| 66 | export var chartDataReducer = chartDataSlice.reducer; |
|---|