Skip to content
Snippets Groups Projects
plugin.reducer.ts 2.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • Matthias Feyll's avatar
    Matthias Feyll committed
    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))
        },
    })