You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
1.1 KiB
31 lines
1.1 KiB
/**
|
|
* 删除全部数据 hook,获取可删除分类并提交删除申请。
|
|
*/
|
|
import { useCallback } from 'react';
|
|
import { fetchDeleteAllData, submitDeleteData } from '../api';
|
|
import type { DeleteAllData } from '../types';
|
|
import { useAsyncData } from './useAsyncData';
|
|
|
|
export interface DeleteAllDataState {
|
|
data: DeleteAllData | null;
|
|
loading: boolean;
|
|
error: Error | null;
|
|
refresh: () => void;
|
|
submit: (categoryIds: string[]) => Promise<{ success: boolean; message: string }>;
|
|
}
|
|
|
|
export function useDeleteAllData(): DeleteAllDataState {
|
|
const fetcher = useCallback(async (): Promise<DeleteAllData> => fetchDeleteAllData(), []);
|
|
const { data, loading, error, refresh } = useAsyncData(fetcher);
|
|
|
|
const submit = useCallback(async (categoryIds: string[]) => {
|
|
try {
|
|
return await submitDeleteData(categoryIds);
|
|
} catch (err) {
|
|
console.error('[useDeleteAllData] submit error', err);
|
|
return { success: false, message: '提交失败,请稍后重试' };
|
|
}
|
|
}, []);
|
|
|
|
return { data: data ?? null, loading, error, refresh, submit };
|
|
}
|
|
|