75 lines
1.7 KiB
TypeScript
75 lines
1.7 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import * as commandService from "@/services/command.service";
|
|
|
|
const COMMAND_QUERY_KEYS = {
|
|
all: ["commands"] as const,
|
|
list: () => [...COMMAND_QUERY_KEYS.all, "list"] as const,
|
|
detail: (id: number) => [...COMMAND_QUERY_KEYS.all, "detail", id] as const,
|
|
};
|
|
|
|
/**
|
|
* Hook để lấy danh sách lệnh
|
|
*/
|
|
export function useGetCommandList(enabled = true) {
|
|
return useQuery({
|
|
queryKey: COMMAND_QUERY_KEYS.list(),
|
|
queryFn: () => commandService.getCommandList(),
|
|
enabled,
|
|
staleTime: 5 * 60 * 1000, // 5 minutes
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook để thêm lệnh mới
|
|
*/
|
|
export function useAddCommand() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (data: any) => commandService.addCommand(data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: COMMAND_QUERY_KEYS.list(),
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook để cập nhật lệnh
|
|
*/
|
|
export function useUpdateCommand() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({
|
|
commandId,
|
|
data,
|
|
}: {
|
|
commandId: number;
|
|
data: any;
|
|
}) => commandService.updateCommand(commandId, data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: COMMAND_QUERY_KEYS.list(),
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook để xóa lệnh
|
|
*/
|
|
export function useDeleteCommand() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (commandId: number) => commandService.deleteCommand(commandId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: COMMAND_QUERY_KEYS.list(),
|
|
});
|
|
},
|
|
});
|
|
}
|