ibms-dome/ibms_react/src/stores/systemSlice.js
2023-01-31 23:15:50 +08:00

38 lines
989 B
JavaScript

import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { mainSubBaseUrl } from "@CON";
import axios from "axios";
export const fetchSysMainSub = createAsyncThunk(
"systemList/fetchSysMainSub",
async (building_tag, { fulfillWithValue }) => {
const res = await axios({
method: "post",
url: mainSubBaseUrl,
data: { building_tag },
});
let result = [];
res.data.history_Main_Systems.forEach((mainSys) => {
mainSys.history_Sub_systems.forEach((subSys) => {
result.push({ ...subSys, main_system_tag: mainSys.main_system_tag });
});
});
return fulfillWithValue(result);
},
);
const systemSlice = createSlice({
name: "systemList",
initialState: {
mainSub: [],
},
reducers: {},
extraReducers: (builder) => {
builder.addCase(fetchSysMainSub.fulfilled, (state, { payload }) => {
state.mainSub = payload;
});
},
});
const { reducer, actions } = systemSlice;
export default reducer;