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

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-01-31 23:15:50 +08:00
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import { userAuthBaseUrl, userInfoBaseUrl } from "@CON";
import { ajaxRes, deviceBuiListBaseUrl } from "@UTIL";
import { fetchBuiList } from "./buildingSlice";
export const fetchUserInfo = createAsyncThunk("user/fetchUserInfo", async (token, thunkAPI) => {
const res = await axios.post(userInfoBaseUrl);
console.log("user/fetchUserInfo", res);
return ajaxRes(res, thunkAPI);
});
export const fetchUserAuthPages = createAsyncThunk(
"user/fetchUserAuthPages",
async (token, thunkAPI) => {
const res = await axios.post(userAuthBaseUrl);
thunkAPI.dispatch(fetchBuiList());
return ajaxRes(res, thunkAPI);
},
);
const userSlice = createSlice({
name: "user",
initialState: {
userAuthPages: [],
userInfo: {},
},
reducers: {},
extraReducers: (builder) => {
builder.addCase(fetchUserInfo.fulfilled, (state, { payload }) => {
state.userInfo = payload;
});
builder.addCase(fetchUserAuthPages.fulfilled, (state, { payload }) => {
state.userAuthPages = payload;
});
},
});
const { reducer } = userSlice;
export default reducer;