Skip to content
Snippets Groups Projects

Ui refactor style

Merged Matthias Feyll requested to merge ui-refactor-style into ui-integration
8 files
+ 65
114
Compare changes
  • Side-by-side
  • Inline
Files
8
@@ -5,6 +5,7 @@ import {
@@ -5,6 +5,7 @@ import {
} from '@api/api'
} from '@api/api'
import { DeviceViewTabValues } from '@component/devices/view/device.view.tabs'
import { DeviceViewTabValues } from '@component/devices/view/device.view.tabs'
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
 
import { REHYDRATE } from 'redux-persist'
import { RootState } from 'src/stores'
import { RootState } from 'src/stores'
import '../routines/index'
import '../routines/index'
import { startListening } from '/src/stores/middleware/listener.middleware'
import { startListening } from '/src/stores/middleware/listener.middleware'
@@ -32,6 +33,13 @@ const initialState: DeviceSliceState = {
@@ -32,6 +33,13 @@ const initialState: DeviceSliceState = {
selected: null,
selected: null,
}
}
 
interface SetSelectedDeviceType {
 
device: Device | null,
 
options?: {
 
bypassCheck: boolean
 
}
 
}
 
const deviceSlice = createSlice({
const deviceSlice = createSlice({
name: 'device',
name: 'device',
initialState,
initialState,
@@ -46,24 +54,33 @@ const deviceSlice = createSlice({
@@ -46,24 +54,33 @@ const deviceSlice = createSlice({
state.activeTab = action.payload
state.activeTab = action.payload
},
},
setSelectedDevice: {
setSelectedDevice: {
reducer: (state, action: PayloadAction<Device | null, string, { skipListener?: boolean }>) => {
reducer: (state, { payload, meta }: PayloadAction<SetSelectedDeviceType, string, { skipListener?: boolean }>) => {
// do thing if desired device is already selected
/**
if (state.selected?.device.id === action.payload?.id) {
* Do nothing if desired device is already selected
action.meta.skipListener = true
* Bypass the check if the flag is set to true. We
 
* use this bypass to trigger the listener functions
 
* accordingly
 
*/
 
if (!payload?.options?.bypassCheck && state.selected?.device.id === payload.device?.id) {
 
meta.skipListener = true
return
return
}
}
let selectedObject = null;
if (!payload.device) {
if (action.payload) {
throw Error('Passed null device as parameter while bypassing the safety check')
selectedObject = { device: action.payload, mne: null, json: null }
}
 
 
let selectedObject: SelectedObject | null = null;
 
if (payload) {
 
selectedObject = { device: payload.device, mne: null, json: null }
}
}
state.selected = selectedObject
state.selected = selectedObject
},
},
prepare: (device: Device | null) => {
prepare: (payload) => {
return {
return {
payload: device,
payload,
meta: { skipListener: false } // set to true when needed
meta: { skipListener: false }
}
}
}
}
},
},
@@ -107,7 +124,24 @@ startListening({
@@ -107,7 +124,24 @@ startListening({
}
}
// if there are no devices available do set null
// if there are no devices available do set null
const newDevices = action.payload?.[0] || null
const device = action.payload?.[0] || null
listenerApi.dispatch(setSelectedDevice(newDevices))
listenerApi.dispatch(setSelectedDevice({ device } as SetSelectedDeviceType))
},
},
})
})
 
 
 
/**
 
* 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))
 
},
 
})
 
\ No newline at end of file
Loading