Browse Source

添加删除轨型和核校的异步Action

dev
zhangjiming 5 months ago
parent
commit
b4cbc4bd36
  1. 2
      src/pages/measure/components/MeasureAction.tsx
  2. 23
      src/pages/measure/components/MeasureConfig.tsx
  3. 84
      src/store/features/baseDataSlice.ts

2
src/pages/measure/components/MeasureAction.tsx

@ -184,7 +184,7 @@ export default function MeasureAction() {
dispatch(fetchRailTypes()); dispatch(fetchRailTypes());
dispatch(fetchCalibrationTypes()); dispatch(fetchCalibrationTypes());
} }
}, [baseData.railTypes.length, dispatch]);
}, [baseData.railTypes, dispatch]);
/** ----------------------- WebSocket 消息处理 ----------------------- **/ /** ----------------------- WebSocket 消息处理 ----------------------- **/
useEffect(() => { useEffect(() => {

23
src/pages/measure/components/MeasureConfig.tsx

@ -32,8 +32,8 @@ export default function MeasureConfig() {
if (res.status !== 0) { if (res.status !== 0) {
messageApi.error(res.data.info); messageApi.error(res.data.info);
} else { } else {
dispatch(updateRailTypeId(values["railType"]))
dispatch(updateCalibrationTypeId(values["calibrationType"]))
dispatch(updateRailTypeId(values["railType"]));
dispatch(updateCalibrationTypeId(values["calibrationType"]));
navigate("../action"); navigate("../action");
} }
}); });
@ -44,7 +44,7 @@ export default function MeasureConfig() {
dispatch(fetchRailTypes()); dispatch(fetchRailTypes());
dispatch(fetchCalibrationTypes()); dispatch(fetchCalibrationTypes());
} }
}, [baseData.railTypes.length, dispatch]);
}, [baseData.railTypes, dispatch]);
const [form] = Form.useForm(); const [form] = Form.useForm();
@ -54,16 +54,22 @@ export default function MeasureConfig() {
const fieldValue: Record<string, any> = { const fieldValue: Record<string, any> = {
username: userInfo.nickname || "", username: userInfo.nickname || "",
direction: "左", direction: "左",
}
};
if (baseData.railTypes.length > 0) { if (baseData.railTypes.length > 0) {
fieldValue.railType = baseData.railTypes[0].id
fieldValue.railType = baseData.railTypes[0].id;
} }
if (baseData.calibrationTypes.length > 0) { if (baseData.calibrationTypes.length > 0) {
fieldValue.calibrationType = baseData.calibrationTypes[0].id
fieldValue.calibrationType = baseData.calibrationTypes[0].id;
} }
form.setFieldsValue(fieldValue); form.setFieldsValue(fieldValue);
}, [baseData.calibrationTypes, baseData.railTypes, form]); }, [baseData.calibrationTypes, baseData.railTypes, form]);
// useEffect(() => {
// if (!baseData.loading && baseData.error) {
// messageApi.error(baseData.error);
// }
// }, [baseData.error, baseData.loading, messageApi]);
return ( return (
<> <>
{contextHolder} {contextHolder}
@ -127,6 +133,11 @@ export default function MeasureConfig() {
</Button> </Button>
</Form.Item> </Form.Item>
{/* <Form.Item label={null}>
<Button type="primary" size="large" style={{ width: 220 }} onClick={() => dispatch(deleteRailType([1]))}>
</Button>
</Form.Item> */}
</Form> </Form>
</div> </div>
</> </>

84
src/store/features/baseDataSlice.ts

@ -2,27 +2,73 @@
import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit"; import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import { Calibration, Rail } from "../../services/apiTypes"; 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 { export interface BaseDataState {
loading: boolean;
error: string;
railTypes: Rail[]; railTypes: Rail[];
calibrationTypes: Calibration[]; calibrationTypes: Calibration[];
} }
const initialState: BaseDataState = { const initialState: BaseDataState = {
loading: false,
error: "",
// 轨型列表 // 轨型列表
railTypes: [], railTypes: [],
// 核校类型列表 // 核校类型列表
calibrationTypes: [], calibrationTypes: [],
}; };
export const fetchRailTypes = createAsyncThunk("fetchRailTypes", async () => {
// 可以添加泛型参数,第一个是 成功返回类型,第二个是 入参类型,第三个是 reject返回类型
// createAsyncThunk<Rail[], void, { rejectValue: { msg: string } }>
export const fetchRailTypes = createAsyncThunk("fetchRailTypes", async (_, thunkAPI) => {
try {
const res = await getRailTypes(); const res = await getRailTypes();
return res.success ? res.data : [];
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 () => {
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(); const res = await getCalibrationTypes();
return res.success ? res.data : [];
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({ export const baseDataSlice = createSlice({
@ -45,11 +91,31 @@ export const baseDataSlice = createSlice({
builder.addCase(fetchCalibrationTypes.fulfilled, (state, action) => { builder.addCase(fetchCalibrationTypes.fulfilled, (state, action) => {
state.calibrationTypes = action.payload; 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; export default baseDataSlice.reducer;
Loading…
Cancel
Save