Newer
Older
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
NetworkelementFlattenedManagedNetworkElement,
NetworkelementManagedNetworkElement,
PndPrincipalNetworkDomain
} from '@api/api'
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { refreshUpdateTimer } from '@shared/reducer/routine.reducer'
import { Category, CategoryType } from '@shared/types/category.type'
import { REHYDRATE } from 'redux-persist'
import { RootState } from 'src/stores'
import '../routines/index'
import { startListening } from '/src/stores/middleware/listener.middleware'
export type Device = NetworkelementFlattenedManagedNetworkElement
interface SelectedObject {
device: Device
mne: NetworkelementManagedNetworkElement | null
json: JSON | null
}
export interface DeviceSliceState {
devices: Device[]
pnds: PndPrincipalNetworkDomain[]
selected: SelectedObject | null
}
const initialState: DeviceSliceState = {
plugins: [],
}
interface SetSelectedDeviceType {
device: Device | null,
options?: {
bypassCheck: boolean
}
}
const deviceSlice = createSlice({
name: 'plugins',
initialState,
reducers: {
setPlugins: (state, action: PayloadAction<Plugin[] | undefined>) => {
state.devices = action.payload || []
},
},
})
export const { setDevices, setSelectedDevice, setSelectedMne, setSelectedJson, setPnds } =
deviceSlice.actions
export default deviceSlice.reducer
export const deviceReducerPath = deviceSlice.reducerPath
// add default selected device if no selected device is set
startListening({
predicate: (action) => setDevices.match(action),
effect: async (action, listenerApi) => {
const { device: state } = listenerApi.getOriginalState() as RootState
if (state.selected) {
return
}
// if there are no devices available do set null
const device = action.payload?.[0] || null
listenerApi.dispatch(setSelectedDevice({ device } as SetSelectedDeviceType))
},
})
startListening({
predicate: (action) => setSelectedMne.match(action),
effect: async (action, listenerApi) => {
listenerApi.dispatch(refreshUpdateTimer(Category.TAB as CategoryType))
},
})
startListening({
predicate: (action) => setDevices.match(action),
effect: async (action, listenerApi) => {
listenerApi.dispatch(refreshUpdateTimer(Category.DEVICE as CategoryType))
},
})
/**
* On startup reset the selected device
*/
startListening({
predicate: ({ type }: any) => type === REHYDRATE,
effect: async (_, listenerApi) => {
const { device: state } = listenerApi.getState() as RootState
const device = state.selected?.device
if (!device) {
return
}
listenerApi.dispatch(setSelectedDevice({ device, options: { bypassCheck: true } } as SetSelectedDeviceType))
},
})