Skip to content
Snippets Groups Projects
device.reducer.ts 3.78 KiB
Newer Older
  • Learn to ignore specific revisions
  • import {
        NetworkelementFlattenedManagedNetworkElement,
        NetworkelementManagedNetworkElement,
    
        PndPrincipalNetworkDomain
    
    } from '@api/api'
    import { DeviceViewTabValues } from '@component/devices/view/device.view.tabs'
    
    import { createSlice, PayloadAction } from '@reduxjs/toolkit'
    
    import { RootState } from 'src/stores'
    import '../routines/index'
    import { startListening } from '/src/stores/middleware/listener.middleware'
    
    export type Device = NetworkelementFlattenedManagedNetworkElement
    
    interface SelectedObject {
    
        mne: NetworkelementManagedNetworkElement | null
    
        json: JSON | null
    
    export interface DeviceSliceState {
    
        devices: Device[]
        pnds: PndPrincipalNetworkDomain[]
    
        activeTab: DeviceViewTabValues
    
        selected: SelectedObject | null
    
    }
    
    const initialState: DeviceSliceState = {
        devices: [],
    
        pnds: [],
    
        activeTab: DeviceViewTabValues.METADATA,
    
    }
    
    const deviceSlice = createSlice({
        name: 'device',
        initialState,
        reducers: {
    
            setDevices: (state, action: PayloadAction<Device[] | undefined>) => {
                state.devices = action.payload || []
    
            setPnds: (state, action: PayloadAction<PndPrincipalNetworkDomain[] | undefined>) => {
                state.pnds = action.payload || []
    
            },
            setActiveTab: (state, action: PayloadAction<DeviceViewTabValues>) => {
                state.activeTab = action.payload
            },
    
            setSelectedDevice: {
                reducer: (state, action: PayloadAction<Device | null, string, { skipListener?: boolean }>) => {
                    // do thing if desired device is already selected
                    if (state.selected?.device.id === action.payload?.id) {
                        action.meta.skipListener = true
                        return
                    }
    
                    let selectedObject = null;
                    if (action.payload) {
                        selectedObject = { device: action.payload, mne: null, json: null }
                    }
    
                    state.selected = selectedObject
                },
                prepare: (device: Device | null) => {
                    return {
                        payload: device,
                        meta: { skipListener: false } // set to true when needed
                    }
                }
    
            },
            setSelectedMne: (state, action: PayloadAction<NetworkelementManagedNetworkElement>) => {
    
                if (!state.selected) {
                    throw new Error('Can not find corresponding device')
                }
    
                // safety check to prevent possible race conditions
                if (state.selected.device.id !== action.payload.id) {
                    // TODO proper error handling by retry fetching the device object
                    throw new Error('Device and mne id does not match')
    
                state.selected.mne = action.payload
    
            },
    
            setSelectedJson: (state, action: PayloadAction<JSON>) => {
    
                if (!state.selected) {
    
                    throw new Error('Selected Device is null where it shouldn´t be null')
                }
    
    
                state.selected.json = action.payload || null
    
    export const { setDevices, setActiveTab, setSelectedDevice, setSelectedMne, setSelectedJson, setPnds } =
    
        deviceSlice.actions
    
    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) {
    
    
            // if there are no devices available do set null
            const newDevices = action.payload?.[0] || null
            listenerApi.dispatch(setSelectedDevice(newDevices))