This repository has been archived on 2020-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
odyfive/src/store/ui/reducer.ts

57 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-09-29 08:52:39 +00:00
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
2020-09-29 09:31:55 +00:00
import UIStore, { UILayer } from "./state";
2020-09-29 08:52:39 +00:00
const initialState: UIStore = {
layers: [],
};
export interface AddLayerPayload {
id: string;
data: UILayer;
}
export interface LayerActionPayload {
id: string;
}
const uiSlice = createSlice({
name: "ui",
initialState,
reducers: {
addLayer(state, action: PayloadAction<AddLayerPayload>) {
state.layers.push({ ...action.payload, visible: false });
},
removeLayer(state, action: PayloadAction<LayerActionPayload>) {
const index = state.layers.findIndex(
(layer) => layer.id == action.payload.id
);
if (index < 0) {
return;
}
state.layers.splice(index, 1);
},
showLayer(state, action: PayloadAction<LayerActionPayload>) {
const index = state.layers.findIndex(
(layer) => layer.id == action.payload.id
);
if (index < 0) {
return;
}
2020-09-29 09:31:55 +00:00
state.layers[index] = { ...state.layers[index], visible: true };
2020-09-29 08:52:39 +00:00
},
hideLayer(state, action: PayloadAction<LayerActionPayload>) {
const index = state.layers.findIndex(
(layer) => layer.id == action.payload.id
);
if (index < 0) {
return;
}
2020-09-29 09:31:55 +00:00
state.layers[index] = { ...state.layers[index], visible: false };
2020-09-29 08:52:39 +00:00
},
},
});
export const { addLayer, removeLayer, showLayer, hideLayer } = uiSlice.actions;
export default uiSlice.reducer;