init
This commit is contained in:
25
src/routes/(user)/recycle/-hooks/useBatchRemoveFile.ts
Normal file
25
src/routes/(user)/recycle/-hooks/useBatchRemoveFile.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { removeFileByIds } from "@/apis/file";
|
||||
import { toast } from "sonner";
|
||||
|
||||
export const useBatchRemoveFile = (mutationOptions?: any) => {
|
||||
const {
|
||||
mutateAsync: batchRemoveMutateAsync,
|
||||
isPending: batchRemoveIsPending,
|
||||
} = useMutation({
|
||||
mutationFn: removeFileByIds,
|
||||
...(mutationOptions || {}),
|
||||
});
|
||||
|
||||
const batchRemove = (ids: number[]) => {
|
||||
toast.promise(batchRemoveMutateAsync(ids), {
|
||||
loading: "删除中",
|
||||
success: "删除成功",
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
batchRemove,
|
||||
batchRemoveIsPending,
|
||||
};
|
||||
};
|
||||
112
src/routes/(user)/recycle/-hooks/useFetchFiles.ts
Normal file
112
src/routes/(user)/recycle/-hooks/useFetchFiles.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { useState } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { getFilePage } from "@/apis/file";
|
||||
import { useReactTable } from "@tanstack/react-table";
|
||||
import { getCoreRowModel } from "@tanstack/react-table";
|
||||
|
||||
type UseFetchFilesProps = {
|
||||
useColumns: any;
|
||||
queryKey: any[];
|
||||
params: any;
|
||||
};
|
||||
|
||||
export const useFetchFiles = ({
|
||||
useColumns,
|
||||
queryKey,
|
||||
params,
|
||||
}: UseFetchFilesProps) => {
|
||||
// 当前打开的目录层级
|
||||
const [openDirectorys, setOpenDirectorys] = useState([]);
|
||||
// 文件目录ID
|
||||
const [parentId, setParentId] = useState(0);
|
||||
// 分页
|
||||
const [pagination, setPagination] = useState({
|
||||
pageIndex: 0,
|
||||
pageSize: 10,
|
||||
});
|
||||
// 选中行
|
||||
const [rowSelection, setRowSelection] = useState({});
|
||||
|
||||
// 查询数据
|
||||
const query = useQuery<any>({
|
||||
queryKey: [...queryKey, pagination, parentId, params],
|
||||
queryFn: () =>
|
||||
getFilePage({
|
||||
parentId: !parentId ? undefined : parentId,
|
||||
parentNotInvalid: !parentId,
|
||||
pageSize: pagination.pageSize,
|
||||
pageNo: pagination.pageIndex + 1,
|
||||
...params,
|
||||
}).then((res) => res.data),
|
||||
});
|
||||
|
||||
// 切换上级ID
|
||||
const handleChangeParentId = (record: any) => {
|
||||
setParentId(record.id);
|
||||
// 重置分页
|
||||
table.setPageIndex(0);
|
||||
// @ts-ignore
|
||||
setOpenDirectorys((prev: any) => [...prev, record]);
|
||||
// 重置选中数据
|
||||
table.resetRowSelection();
|
||||
};
|
||||
|
||||
// 表格
|
||||
const columns = useColumns(handleChangeParentId);
|
||||
const table = useReactTable({
|
||||
data: query?.data?.entityList || [],
|
||||
columns,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
manualPagination: false,
|
||||
rowCount: query?.data?.totalCount || 0,
|
||||
pageCount: query?.data?.pageCount || 0,
|
||||
onPaginationChange: setPagination,
|
||||
autoResetPageIndex: false,
|
||||
onRowSelectionChange: setRowSelection,
|
||||
state: {
|
||||
pagination,
|
||||
rowSelection,
|
||||
},
|
||||
});
|
||||
|
||||
const changeOpenDirectorys = (record?: any) => {
|
||||
setParentId(record?.id);
|
||||
// 重置分页
|
||||
table.setPageIndex(0);
|
||||
// @ts-ignore
|
||||
setOpenDirectorys((prev: any[]) => {
|
||||
const index = prev.findIndex((item: any) => item.id === record?.id);
|
||||
if (index > -1) {
|
||||
return [...prev.slice(0, index + 1)];
|
||||
}
|
||||
return [];
|
||||
});
|
||||
// 重置选中数据
|
||||
table.resetRowSelection();
|
||||
};
|
||||
|
||||
// 重置
|
||||
const reset = () => {
|
||||
setParentId(0);
|
||||
// 重置分页
|
||||
table.setPageIndex(0);
|
||||
// @ts-ignore
|
||||
setOpenDirectorys([]);
|
||||
// 重置选中数据
|
||||
table.resetRowSelection();
|
||||
};
|
||||
|
||||
return {
|
||||
parentId,
|
||||
setParentId,
|
||||
handleChangeParentId,
|
||||
openDirectorys,
|
||||
changeOpenDirectorys,
|
||||
pagination,
|
||||
table,
|
||||
query,
|
||||
reset,
|
||||
rowSelection,
|
||||
setRowSelection,
|
||||
};
|
||||
};
|
||||
23
src/routes/(user)/recycle/-hooks/useRecoverFile.ts
Normal file
23
src/routes/(user)/recycle/-hooks/useRecoverFile.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { recoverFile } from "@/apis/file";
|
||||
import { toast } from "sonner";
|
||||
|
||||
export const useRecoverFile = (mutationOptions?: any) => {
|
||||
const { mutateAsync: recoverMutateAsync, isPending: recoverIsPending } =
|
||||
useMutation({
|
||||
mutationFn: recoverFile,
|
||||
...(mutationOptions || {}),
|
||||
});
|
||||
|
||||
const recover = (id: number) => {
|
||||
toast.promise(recoverMutateAsync(id), {
|
||||
loading: "恢复中",
|
||||
success: "恢复成功",
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
recover,
|
||||
recoverIsPending,
|
||||
};
|
||||
};
|
||||
23
src/routes/(user)/recycle/-hooks/useRemoveFile.ts
Normal file
23
src/routes/(user)/recycle/-hooks/useRemoveFile.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { removeFile } from "@/apis/file";
|
||||
import { toast } from "sonner";
|
||||
|
||||
export const useRemoveFile = (mutationOptions?: any) => {
|
||||
const { mutateAsync: removeMutateAsync, isPending: removeIsPending } =
|
||||
useMutation({
|
||||
mutationFn: removeFile,
|
||||
...(mutationOptions || {}),
|
||||
});
|
||||
|
||||
const remove = (id: number) => {
|
||||
toast.promise(removeMutateAsync(id), {
|
||||
loading: "删除中",
|
||||
success: "删除成功",
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
remove,
|
||||
removeIsPending,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user