Skip to content
Snippets Groups Projects
mne.routine.ts 3.09 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { api, NetworkElementServiceGetApiArg, NetworkElementServiceParseYangApiArg } from '@api/api'
    import {
        Device,
        setSelectedDevice,
        setSelectedJson,
        setSelectedMne,
    } from '@component/devices/reducer/device.reducer'
    import { createAsyncThunk } from '@reduxjs/toolkit'
    
    import { addRoutine } from '@shared/reducer/routine.reducer'
    
    import { Category, CategoryType } from '@shared/types/category.type'
    
    import { RootState } from 'src/stores'
    import { startListening } from '../../../stores/middleware/listener.middleware'
    
    export const FETCH_MNE_ACTION = 'subscription/device/fetchSelectedMNE'
    
    /**
     * #0
     * Trigger fetch MNE (#1)
     * 
     * Triggered by a selectedDevice
     */
    
    startListening({
    
        predicate: (action) => setSelectedDevice.match(action) && !!action.payload.device && !action.meta?.skipListener,
    
        effect: async (action, listenerApi) => {
    
    
            const device = action.payload.device
    
    
            listenerApi.dispatch(
                addRoutine({
    
                    thunk: fetchSelectedMneThunk,
    
                    category: Category.TAB as CategoryType,
    
    /**
     * #1
     * Fetch MNE
     * 
     * Triggered by #0
     */
    
    const FETCH_MNE_INTERVAL = 5000 // in ms
    export const fetchSelectedMneThunk = createAsyncThunk(
        FETCH_MNE_ACTION,
        async (device: Device, thunkApi) => {
            const { user } = thunkApi.getState() as RootState
    
    Matthias Feyll's avatar
    Matthias Feyll committed
            if (!user.user?.roles || !device.id) {
                throw new Error('Background MNE fetching failed! User data is missing. Reload page or logout and login again')
                // TODO
            }
    
    
            const payload: NetworkElementServiceGetApiArg = {
    
    Matthias Feyll's avatar
    Matthias Feyll committed
                pid: Object.keys(user.user.roles)[0],
    
                timestamp: new Date().getTime().toString(),
                mneid: device.id,
    
            const subscription = thunkApi.dispatch(
                api.endpoints.networkElementServiceGet.initiate(payload, {
                    subscriptionOptions: {
                        pollingInterval: FETCH_MNE_INTERVAL,
                        skipPollingIfUnfocused: true,
                    },
                })
            )
    
            return { ...subscription }
        }
    )
    
    /**
     * #2
     * Received MNE
     * 
     * Triggered by #1
     */
    
    startListening({
        predicate: (action) => api.endpoints.networkElementServiceGet.matchFulfilled(action),
        effect: async (action, listenerApi) => {
    
            listenerApi.dispatch(setSelectedMne(action.payload.mne))
        },
    })
    
    
    /**
     * #3
     * Fetch & receive json
     * 
     * Triggered by #2
     */
    
    startListening({
        predicate: (action) => setSelectedMne.match(action),
        effect: async (action, listenerApi) => {
            const payload: NetworkElementServiceParseYangApiArg = {
                timestamp: new Date().getTime().toString(),
                yang: action.payload.model,
            }
    
            const { data } = await listenerApi.dispatch(
                api.endpoints.networkElementServiceParseYang.initiate(payload)
            )
    
            if (!data?.json) {
                throw new Error('ParseYang response is invalid. The json field is null')
            }
            const json = JSON.parse(data.json)
    
            listenerApi.dispatch(setSelectedJson(json))