|
|
@@ -19,18 +19,6 @@ type AreaResponse = InferResponseType<typeof areaClient.index.$get, 200>['data']
|
|
|
type CreateAreaRequest = InferRequestType<typeof areaClient.index.$post>['json'];
|
|
|
type UpdateAreaRequest = InferRequestType<typeof areaClient[':id']['$put']>['json'];
|
|
|
|
|
|
-// 统一操作处理函数
|
|
|
-const handleOperation = async (operation: () => Promise<void>) => {
|
|
|
- try {
|
|
|
- await operation();
|
|
|
- // toast.success('操作成功');
|
|
|
- } catch (error) {
|
|
|
- // toast.error('操作失败,请重试');
|
|
|
- throw error;
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-
|
|
|
export const AreaManagement: React.FC = () => {
|
|
|
const queryClient = useQueryClient();
|
|
|
const [expandedNodes, setExpandedNodes] = useState<Set<number>>(new Set());
|
|
|
@@ -66,10 +54,11 @@ export const AreaManagement: React.FC = () => {
|
|
|
// 创建省市区
|
|
|
const createMutation = useMutation({
|
|
|
mutationFn: async (data: CreateAreaRequest) => {
|
|
|
- await handleOperation(async () => {
|
|
|
- const res = await areaClientManager.get().index.$post({ json: data });
|
|
|
- if (res.status !== 201) throw new Error('创建省市区失败');
|
|
|
- });
|
|
|
+ const res = await areaClientManager.get().index.$post({ json: data });
|
|
|
+ if (res.status !== 201) {
|
|
|
+ const errorResponse = await res.json();
|
|
|
+ throw new Error(errorResponse.message || '创建省市区失败');
|
|
|
+ }
|
|
|
},
|
|
|
onSuccess: async (_, variables) => {
|
|
|
// 显示成功提示
|
|
|
@@ -99,21 +88,22 @@ export const AreaManagement: React.FC = () => {
|
|
|
// 等待所有进行中的查询完成
|
|
|
await queryClient.refetchQueries({ queryKey: ['areas-tree-province'] });
|
|
|
},
|
|
|
- onError: () => {
|
|
|
- toast.error('创建失败,请重试');
|
|
|
+ onError: (error) => {
|
|
|
+ toast.error(error.message || '创建失败,请重试');
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 更新省市区
|
|
|
const updateMutation = useMutation({
|
|
|
mutationFn: async ({ id, data }: { id: number; data: UpdateAreaRequest }) => {
|
|
|
- await handleOperation(async () => {
|
|
|
- const res = await areaClientManager.get()[':id'].$put({
|
|
|
- param: { id },
|
|
|
- json: data
|
|
|
- });
|
|
|
- if (res.status !== 200) throw new Error('更新省市区失败');
|
|
|
+ const res = await areaClientManager.get()[':id'].$put({
|
|
|
+ param: { id },
|
|
|
+ json: data
|
|
|
});
|
|
|
+ if (res.status !== 200) {
|
|
|
+ const errorResponse = await res.json();
|
|
|
+ throw new Error(errorResponse.message || '更新省市区失败');
|
|
|
+ }
|
|
|
},
|
|
|
onSuccess: async () => {
|
|
|
// 显示成功提示
|
|
|
@@ -132,20 +122,21 @@ export const AreaManagement: React.FC = () => {
|
|
|
|
|
|
await queryClient.refetchQueries({ queryKey: ['areas-tree-province'] });
|
|
|
},
|
|
|
- onError: () => {
|
|
|
- toast.error('更新失败,请重试');
|
|
|
+ onError: (error) => {
|
|
|
+ toast.error(error.message || '更新失败,请重试');
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 删除省市区
|
|
|
const deleteMutation = useMutation({
|
|
|
mutationFn: async (id: number) => {
|
|
|
- await handleOperation(async () => {
|
|
|
- const res = await areaClientManager.get()[':id'].$delete({
|
|
|
- param: { id }
|
|
|
- });
|
|
|
- if (res.status !== 204) throw new Error('删除省市区失败');
|
|
|
+ const res = await areaClientManager.get()[':id'].$delete({
|
|
|
+ param: { id }
|
|
|
});
|
|
|
+ if (res.status !== 204) {
|
|
|
+ const errorResponse = await res.json();
|
|
|
+ throw new Error(errorResponse.message || '删除省市区失败');
|
|
|
+ }
|
|
|
},
|
|
|
onSuccess: async () => {
|
|
|
// 显示成功提示
|
|
|
@@ -164,21 +155,22 @@ export const AreaManagement: React.FC = () => {
|
|
|
|
|
|
await queryClient.refetchQueries({ queryKey: ['areas-tree-province'] });
|
|
|
},
|
|
|
- onError: () => {
|
|
|
- toast.error('删除失败,请重试');
|
|
|
+ onError: (error) => {
|
|
|
+ toast.error(error.message || '删除失败,请重试');
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 启用/禁用省市区
|
|
|
const toggleStatusMutation = useMutation({
|
|
|
mutationFn: async ({ id, isDisabled }: { id: number; isDisabled: number }) => {
|
|
|
- await handleOperation(async () => {
|
|
|
- const res = await areaClientManager.get()[':id'].$put({
|
|
|
- param: { id },
|
|
|
- json: { isDisabled }
|
|
|
- });
|
|
|
- if (res.status !== 200) throw new Error('更新省市区状态失败');
|
|
|
+ const res = await areaClientManager.get()[':id'].$put({
|
|
|
+ param: { id },
|
|
|
+ json: { isDisabled }
|
|
|
});
|
|
|
+ if (res.status !== 200) {
|
|
|
+ const errorResponse = await res.json();
|
|
|
+ throw new Error(errorResponse.message || '更新省市区状态失败');
|
|
|
+ }
|
|
|
},
|
|
|
onSuccess: async () => {
|
|
|
// 显示成功提示
|
|
|
@@ -197,8 +189,8 @@ export const AreaManagement: React.FC = () => {
|
|
|
|
|
|
await queryClient.refetchQueries({ queryKey: ['areas-tree-province'] });
|
|
|
},
|
|
|
- onError: () => {
|
|
|
- toast.error('状态切换失败,请重试');
|
|
|
+ onError: (error) => {
|
|
|
+ toast.error(error.message || '状态切换失败,请重试');
|
|
|
}
|
|
|
});
|
|
|
|