aboutsummaryrefslogtreecommitdiff
path: root/modern/src/store/devices.js
blob: bb9068f117a6a14c75acb84e69803e27f8575e5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { createSlice, current } from '@reduxjs/toolkit';

const { reducer, actions } = createSlice({
  name: 'devices',
  initialState: {
    items: {},
    filteredItems: {},
    selectedId: null,
  },
  reducers: {
    refresh(state, action) {
      state.items = {};
      action.payload.forEach((item) => state.items[item.id] = item);
    },
    update(state, action) {
      action.payload.forEach((item) => state.items[item.id] = item);
    },
    select(state, action) {
      state.selectedId = action.payload.id;
    },
    unselect(state, action) {
      state.selectedId = null;
    },
    setFilter(state, action) {
      const items = Object.entries(current(state).items);
      state.filteredItems = Object.fromEntries(items.filter(([k, v]) => {
        return v.name.toLowerCase().includes(action.payload.toLowerCase());
      }));
    },
    clearFilter(state, action) {
      state.filteredItems = {};
    },
    remove(state, action) {
      delete state.items[action.payload];
    },
  },
});

export { actions as devicesActions };
export { reducer as devicesReducer };