asyncTaskStore.ts
1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { create } from 'zustand';
import type { AsyncTask, PaginatedResponse } from '../types';
import { asyncTaskService } from '../services/asyncTaskService';
interface AsyncTaskState {
tasks: AsyncTask[];
total: number;
page: number;
pageSize: number;
totalPages: number;
loading: boolean;
fetchTasks: (params?: {
page?: number;
page_size?: number;
status?: string;
task_id?: string;
}) => Promise<void>;
cancelTask: (id: string) => Promise<void>;
}
export const useAsyncTaskStore = create<AsyncTaskState>((set, get) => ({
tasks: [],
total: 0,
page: 1,
pageSize: 20,
totalPages: 0,
loading: false,
fetchTasks: async (params) => {
set({ loading: true });
try {
const p = { ...params };
if (!p.page) p.page = get().page;
if (!p.page_size) p.page_size = get().pageSize;
const res: PaginatedResponse<AsyncTask> = await asyncTaskService.list(p);
set({
tasks: res.items,
total: res.total,
page: res.page,
pageSize: res.page_size,
totalPages: res.total_pages,
loading: false,
});
} catch (e) {
console.error('获取异步任务列表失败:', e);
set({ loading: false });
}
},
cancelTask: async (id) => {
await asyncTaskService.cancel(id);
await get().fetchTasks();
},
}));