Newer
Older
import { NetworkElementServiceGetAllFlattenedApiArg, api } from "@api/api";
import { setDevices } from "@component/devices/reducer/device.reducer";
import { createAsyncThunk } from "@reduxjs/toolkit";
import { setUser } from "@shared/reducer/user.reducer";
import { startListening } from "../../../stores/middleware/listener.middleware";
import { FETCH_DEVICE_ACTION } from "./action.routine";
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
// continously fetch devices
const FETCH_DEVICES_INTERVAL = 15000; // in ms
startListening({
actionCreator: setUser,
effect: async (_, listenerApi) => {
listenerApi.dispatch(fetchDevicesThunk());
},
})
export const fetchDevicesThunk = createAsyncThunk(FETCH_DEVICE_ACTION, (_, thunkApi) => {
const { user } = thunkApi.getState() as RootState;
const payload: NetworkElementServiceGetAllFlattenedApiArg = {
pid: Object.keys(user?.user.roles)[0],
timestamp: new Date().getTime().toString(),
}
const subscription = thunkApi.dispatch(api.endpoints.networkElementServiceGetAllFlattened.initiate(payload, {
subscriptionOptions: {
pollingInterval: FETCH_DEVICES_INTERVAL,
skipPollingIfUnfocused: true,
}
}));
return subscription;
});
// save fetched devices
startListening({
predicate: (action) => api.endpoints.networkElementServiceGetAllFlattened.matchFulfilled(action),
effect: async (action, listenerApi) => {
listenerApi.dispatch(setDevices(action.payload.mne));
},
})