ibms-dome/ibms_react/src/stores/deviceListSlice.js

106 lines
3.4 KiB
JavaScript
Raw Normal View History

2023-01-31 23:15:50 +08:00
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import axios from "axios";
import { deviceBuiMenuBaseUrl, deviceListBaseUrl, deviceFloorBaseUrl } from "@CON";
import { ajaxRes } from "@UTIL";
2023-02-06 00:06:41 +08:00
// 獲取小類中總覽資料
2023-01-31 23:15:50 +08:00
export const fetchSelectedDeviceMenu = createAsyncThunk(
"deviceList/fetchSelectedDeviceMenu",
async ({ main_system_tag, sub_system_tag }, thunkAPI) => {
const { selectedBuiTag } = thunkAPI.getState().buildingInfo;
const res = await axios.post(deviceBuiMenuBaseUrl, {
building_tag: selectedBuiTag,
main_system_tag,
sub_system_tag,
});
return ajaxRes(res, thunkAPI);
},
);
export const fetchSelectedDeviceList = createAsyncThunk(
"deviceList/fetchSelectedDeviceList",
async ({ sub_system_tag }, { getState, fulfillWithValue, rejectWithValue }) => {
const { selectedBuiTag } = getState().buildingInfo;
const res = await axios.post(deviceListBaseUrl, {
building_tag: selectedBuiTag,
sub_system_tag,
});
const { code = 9999, msg, data = null } = res;
if (code !== "0000" || !data) {
return rejectWithValue(msg);
} else {
let results=[];
data.forEach((p) => {
p.device_list.forEach((d) => {
results.push({ ...d, device_floor: p.full_name });
});
});
return fulfillWithValue([...results]);
}
},
);
export const fetchSelectedDevFlTags = createAsyncThunk(
"deviceList/fetchSelectedDevFlTags",
async ({ sub_system_tag }, thunkAPI) => {
const { selectedBuiTag } = thunkAPI.getState().buildingInfo;
const res = await axios.post(deviceFloorBaseUrl, {
building_tag: selectedBuiTag,
sub_system_tag,
});
return ajaxRes(res, thunkAPI);
},
);
const deviceListSlice = createSlice({
name: "deviceList",
initialState: {
allDeviceList: [],
selectedDeviceMenu: {}, //總覽
selectedDeviceList: [],
selectedDeviceSysTag: "", // ELEV
selectedDeviceNameTag: "", // EL
selectedDeviceFloorTags: [], // R2F
selectedDeviceMaster: "", // BANK1
selectedDeviceLastName: "", // ELEV
selectedDeviceSerialTag: "", // N1
2023-02-06 00:06:41 +08:00
tempsensorDevList: [],
2023-01-31 23:15:50 +08:00
},
reducers: {
2023-02-06 00:06:41 +08:00
getAllDevice: (state, action) => { },
getCurDeviceTemp: (state, { payload }) => {
const { device_number, temp } = payload;
state.tempsensorDevList = state.tempsensorDevList.map((sensor) => {
if (sensor.some(s => s.device_number == device_number)) {
return { ...sensor, temp }
}
return sensor
})
},
changeMainSubSys: (state, { payload }) => {
const { main_system_tag, sub_system_tag } = payload;
state.selectedDeviceSysTag = main_system_tag;
state.selectedDeviceNameTag = sub_system_tag;
}
2023-01-31 23:15:50 +08:00
},
extraReducers: (builder) => {
builder.addCase(fetchSelectedDeviceMenu.fulfilled, (state, { payload }) => {
state.selectedDeviceMenu = payload;
});
builder.addCase(fetchSelectedDeviceList.fulfilled, (state, { payload }) => {
state.selectedDeviceList = payload;
});
builder.addCase(fetchSelectedDevFlTags.fulfilled, (state, { payload }) => {
state.selectedDeviceFloorTags = payload.map((p) => {
return { floor_tag: p.floor_tag, floor_guid: p.floor_guid };
});
});
},
});
const { reducer, actions } = deviceListSlice;
2023-02-06 00:06:41 +08:00
export const { getCurDeviceTemp, changeMainSubSys } = actions;
2023-01-31 23:15:50 +08:00
export default reducer;