| 1 | import { createSlice, current, prepareAutoBatched } from '@reduxjs/toolkit';
|
|---|
| 2 | import { castDraft } from 'immer';
|
|---|
| 3 |
|
|---|
| 4 | /**
|
|---|
| 5 | * The properties inside this state update independently of each other and quite often.
|
|---|
| 6 | * When selecting, never select the whole state because you are going to get
|
|---|
| 7 | * unnecessary re-renders. Select only the properties you need.
|
|---|
| 8 | *
|
|---|
| 9 | * This is why this state type is not exported - don't use it directly.
|
|---|
| 10 | */
|
|---|
| 11 |
|
|---|
| 12 | var initialState = {
|
|---|
| 13 | settings: {
|
|---|
| 14 | layout: 'horizontal',
|
|---|
| 15 | align: 'center',
|
|---|
| 16 | verticalAlign: 'middle',
|
|---|
| 17 | itemSorter: 'value'
|
|---|
| 18 | },
|
|---|
| 19 | size: {
|
|---|
| 20 | width: 0,
|
|---|
| 21 | height: 0
|
|---|
| 22 | },
|
|---|
| 23 | payload: []
|
|---|
| 24 | };
|
|---|
| 25 | var legendSlice = createSlice({
|
|---|
| 26 | name: 'legend',
|
|---|
| 27 | initialState,
|
|---|
| 28 | reducers: {
|
|---|
| 29 | setLegendSize(state, action) {
|
|---|
| 30 | state.size.width = action.payload.width;
|
|---|
| 31 | state.size.height = action.payload.height;
|
|---|
| 32 | },
|
|---|
| 33 | setLegendSettings(state, action) {
|
|---|
| 34 | state.settings.align = action.payload.align;
|
|---|
| 35 | state.settings.layout = action.payload.layout;
|
|---|
| 36 | state.settings.verticalAlign = action.payload.verticalAlign;
|
|---|
| 37 | state.settings.itemSorter = action.payload.itemSorter;
|
|---|
| 38 | },
|
|---|
| 39 | addLegendPayload: {
|
|---|
| 40 | reducer(state, action) {
|
|---|
| 41 | state.payload.push(castDraft(action.payload));
|
|---|
| 42 | },
|
|---|
| 43 | prepare: prepareAutoBatched()
|
|---|
| 44 | },
|
|---|
| 45 | replaceLegendPayload: {
|
|---|
| 46 | reducer(state, action) {
|
|---|
| 47 | var {
|
|---|
| 48 | prev,
|
|---|
| 49 | next
|
|---|
| 50 | } = action.payload;
|
|---|
| 51 | var index = current(state).payload.indexOf(castDraft(prev));
|
|---|
| 52 | if (index > -1) {
|
|---|
| 53 | state.payload[index] = castDraft(next);
|
|---|
| 54 | }
|
|---|
| 55 | },
|
|---|
| 56 | prepare: prepareAutoBatched()
|
|---|
| 57 | },
|
|---|
| 58 | removeLegendPayload: {
|
|---|
| 59 | reducer(state, action) {
|
|---|
| 60 | var index = current(state).payload.indexOf(castDraft(action.payload));
|
|---|
| 61 | if (index > -1) {
|
|---|
| 62 | state.payload.splice(index, 1);
|
|---|
| 63 | }
|
|---|
| 64 | },
|
|---|
| 65 | prepare: prepareAutoBatched()
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | });
|
|---|
| 69 | export var {
|
|---|
| 70 | setLegendSize,
|
|---|
| 71 | setLegendSettings,
|
|---|
| 72 | addLegendPayload,
|
|---|
| 73 | replaceLegendPayload,
|
|---|
| 74 | removeLegendPayload
|
|---|
| 75 | } = legendSlice.actions;
|
|---|
| 76 | export var legendReducer = legendSlice.reducer; |
|---|