106 lines
3.4 KiB
JavaScript
106 lines
3.4 KiB
JavaScript
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 {
|
|
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
|
|
tempsensorDevList: [],
|
|
},
|
|
reducers: {
|
|
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;
|
|
}
|
|
},
|
|
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;
|
|
export const { getCurDeviceTemp, changeMainSubSys } = actions;
|
|
export default reducer;
|