|
|
@ -2,27 +2,73 @@ |
|
|
|
|
|
|
|
import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit"; |
|
|
|
import { Calibration, Rail } from "../../services/apiTypes"; |
|
|
|
import { getRailTypes } from "../../services/standardRail/standardRail"; |
|
|
|
import { getCalibrationTypes } from "../../services/calibration/calibration"; |
|
|
|
import { deleteRailTypes, getRailTypes } from "../../services/standardRail/standardRail"; |
|
|
|
import { deleteCalibrationTypes, getCalibrationTypes } from "../../services/calibration/calibration"; |
|
|
|
|
|
|
|
export interface BaseDataState { |
|
|
|
loading: boolean; |
|
|
|
error: string; |
|
|
|
railTypes: Rail[]; |
|
|
|
calibrationTypes: Calibration[]; |
|
|
|
} |
|
|
|
const initialState: BaseDataState = { |
|
|
|
loading: false, |
|
|
|
error: "", |
|
|
|
// 轨型列表
|
|
|
|
railTypes: [], |
|
|
|
// 核校类型列表
|
|
|
|
calibrationTypes: [], |
|
|
|
}; |
|
|
|
|
|
|
|
export const fetchRailTypes = createAsyncThunk("fetchRailTypes", async () => { |
|
|
|
const res = await getRailTypes(); |
|
|
|
return res.success ? res.data : []; |
|
|
|
// 可以添加泛型参数,第一个是 成功返回类型,第二个是 入参类型,第三个是 reject返回类型
|
|
|
|
// createAsyncThunk<Rail[], void, { rejectValue: { msg: string } }>
|
|
|
|
export const fetchRailTypes = createAsyncThunk("fetchRailTypes", async (_, thunkAPI) => { |
|
|
|
try { |
|
|
|
const res = await getRailTypes(); |
|
|
|
if (res.success) { |
|
|
|
return res.data; |
|
|
|
} else { |
|
|
|
return thunkAPI.rejectWithValue({ msg: res.data.info }); |
|
|
|
} |
|
|
|
} catch (error: any) { |
|
|
|
return thunkAPI.rejectWithValue({ msg: (error.message as string) || "请求失败" }); |
|
|
|
} |
|
|
|
}); |
|
|
|
export const fetchCalibrationTypes = createAsyncThunk("fetchCalibrationTypes", async () => { |
|
|
|
const res = await getCalibrationTypes(); |
|
|
|
return res.success ? res.data : []; |
|
|
|
export const deleteRailType = createAsyncThunk("deleteRailType", async (ids: number[], thunkAPI) => { |
|
|
|
try { |
|
|
|
const res = await deleteRailTypes(ids); |
|
|
|
if (res.success) { |
|
|
|
thunkAPI.dispatch(fetchRailTypes()); |
|
|
|
} else { |
|
|
|
return thunkAPI.rejectWithValue({ msg: res.data.info }); |
|
|
|
} |
|
|
|
} catch (error: any) { |
|
|
|
return thunkAPI.rejectWithValue({ msg: (error.message as string) || "请求失败" }); |
|
|
|
} |
|
|
|
}); |
|
|
|
export const fetchCalibrationTypes = createAsyncThunk("fetchCalibrationTypes", async (_, thunkAPI) => { |
|
|
|
try { |
|
|
|
const res = await getCalibrationTypes(); |
|
|
|
if (res.success) { |
|
|
|
return res.data; |
|
|
|
} else { |
|
|
|
return thunkAPI.rejectWithValue({ msg: res.data.info }); |
|
|
|
} |
|
|
|
} catch (error: any) { |
|
|
|
return thunkAPI.rejectWithValue({ msg: (error.message as string) || "请求失败" }); |
|
|
|
} |
|
|
|
}); |
|
|
|
export const deleteCalibrationType = createAsyncThunk("deleteCalibrationType", async (ids: number[], thunkAPI) => { |
|
|
|
try { |
|
|
|
const res = await deleteCalibrationTypes(ids); |
|
|
|
if (res.success) { |
|
|
|
thunkAPI.dispatch(fetchCalibrationTypes()); |
|
|
|
} else { |
|
|
|
return thunkAPI.rejectWithValue({ msg: res.data.info }); |
|
|
|
} |
|
|
|
} catch (error: any) { |
|
|
|
return thunkAPI.rejectWithValue({ msg: (error.message as string) || "请求失败" }); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
export const baseDataSlice = createSlice({ |
|
|
@ -45,11 +91,31 @@ export const baseDataSlice = createSlice({ |
|
|
|
builder.addCase(fetchCalibrationTypes.fulfilled, (state, action) => { |
|
|
|
state.calibrationTypes = action.payload; |
|
|
|
}); |
|
|
|
|
|
|
|
builder.addMatcher( |
|
|
|
action => action.type.endsWith("/pending"), |
|
|
|
state => { |
|
|
|
state.loading = true; |
|
|
|
state.error = ""; |
|
|
|
} |
|
|
|
); |
|
|
|
builder.addMatcher( |
|
|
|
action => action.type.endsWith("/fulfilled"), |
|
|
|
state => { |
|
|
|
state.loading = false; |
|
|
|
state.error = ""; |
|
|
|
} |
|
|
|
); |
|
|
|
builder.addMatcher( |
|
|
|
action => action.type.endsWith("/rejected"), |
|
|
|
(state, action: PayloadAction<{ msg: string }>) => { |
|
|
|
state.loading = false; |
|
|
|
state.error = action.payload?.msg || "请求失败"; |
|
|
|
} |
|
|
|
); |
|
|
|
}, |
|
|
|
}); |
|
|
|
|
|
|
|
// 导出加减的方法
|
|
|
|
export const { updateRailTypes, updateCalibrationTypes } = baseDataSlice.actions; |
|
|
|
// export const { updateRailTypes, updateCalibrationTypes } = baseDataSlice.actions;
|
|
|
|
|
|
|
|
// 默认导出
|
|
|
|
export default baseDataSlice.reducer; |