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
import { api, NetworkElementServiceGetApiArg } from "@api/api";
import { Device, setSelectedDevice, setSelectedMne } from "@reducer/device.reducer";
import { CATEGORIES, triggerSubscription } from "@reducer/subscription.reducer";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { RootState } from "src/stores";
import { THUNK_TYPE } from ".";
import { startListening } from "../../../src/stores/middleware/listener.middleware";
import { FETCH_MNE_ACTION } from "./action.subscription";
// fetch mne if selected device is set
startListening({
predicate: (action) => setSelectedDevice.match(action) && !!action.payload,
effect: async (action, listenerApi) => {
listenerApi.dispatch(triggerSubscription({category: CATEGORIES.TAB, thunkType: THUNK_TYPE.MNE, payload: action.payload}));
},
})
const FETCH_MNE_INTERVAL = 5000; // in ms
export const fetchSelectedMneThunk = createAsyncThunk(FETCH_MNE_ACTION, async (device: Device, thunkApi) => {
const { user } = thunkApi.getState() as RootState;
const payload: NetworkElementServiceGetApiArg = {
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};
});
// save fetched mne
startListening({
predicate: (action) => api.endpoints.networkElementServiceGet.matchFulfilled(action),
effect: async (action, listenerApi) => {
listenerApi.dispatch(setSelectedMne(action.payload.mne));
},
})