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

93 lines
2.9 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";
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 {
console.log(data)
let results=[];
data.forEach((p) => {
p.device_list.forEach((d) => {
results.push({ ...d, device_floor: p.full_name });
});
});
console.log("v", [...results]);
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
},
reducers: {
getAllDevice: (state, action) => {},
},
extraReducers: (builder) => {
builder.addCase(fetchSelectedDeviceMenu.fulfilled, (state, { payload }) => {
state.selectedDeviceMenu = payload;
});
builder.addCase(fetchSelectedDeviceList.fulfilled, (state, { payload }) => {
console.log("de", 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;
export const { getAllDevice } = actions;
export default reducer;