admin_ruspet/src/features/flowNodes/hooks/useFlowNodes.ts

158 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import {
flowNodesApi,
type CreateFlowNodeRequest,
type UpdateFlowNodeRequest,
type MoveFlowNodeRequest,
type SaveTreeRequest,
} from '@/api/flowNodes'
import { notifications } from '@mantine/notifications'
export const FLOW_NODES_QUERY_KEY = 'flowNodes'
export function useFlowNodesTree(topicId: number | null) {
return useQuery({
queryKey: [FLOW_NODES_QUERY_KEY, topicId],
queryFn: async () => {
if (!topicId) return null
const response = await flowNodesApi.getTree(topicId)
return response.data
},
enabled: !!topicId,
})
}
export function useCreateFlowNode() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (data: CreateFlowNodeRequest) => flowNodesApi.create(data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [FLOW_NODES_QUERY_KEY] })
notifications.show({
title: 'Успешно',
message: 'Узел создан',
color: 'green',
})
},
onError: (error: any) => {
notifications.show({
title: 'Ошибка',
message: error.response?.data?.error_description || 'Не удалось создать узел',
color: 'red',
})
},
})
}
export function useUpdateFlowNode() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (data: UpdateFlowNodeRequest) => flowNodesApi.update(data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [FLOW_NODES_QUERY_KEY] })
notifications.show({
title: 'Успешно',
message: 'Узел обновлён',
color: 'green',
})
},
onError: (error: any) => {
notifications.show({
title: 'Ошибка',
message: error.response?.data?.error_description || 'Не удалось обновить узел',
color: 'red',
})
},
})
}
export function useDeleteFlowNode() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (id: number) => flowNodesApi.delete(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [FLOW_NODES_QUERY_KEY] })
notifications.show({
title: 'Успешно',
message: 'Узел удалён',
color: 'green',
})
},
onError: (error: any) => {
notifications.show({
title: 'Ошибка',
message: error.response?.data?.error_description || 'Не удалось удалить узел',
color: 'red',
})
},
})
}
export function useSaveFlowTree() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: ({ topicId, data }: { topicId: number; data: SaveTreeRequest }) =>
flowNodesApi.saveTree(topicId, data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [FLOW_NODES_QUERY_KEY] })
notifications.show({
title: 'Успешно',
message: 'Дерево сохранено',
color: 'green',
})
},
onError: (error: any) => {
notifications.show({
title: 'Ошибка',
message: error.response?.data?.error_description || 'Не удалось сохранить дерево',
color: 'red',
})
},
})
}
export function useMoveFlowNode() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: ({ id, data }: { id: number; data: MoveFlowNodeRequest }) =>
flowNodesApi.move(id, data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [FLOW_NODES_QUERY_KEY] })
},
onError: (error: any) => {
notifications.show({
title: 'Ошибка',
message: error.response?.data?.error_description || 'Не удалось переместить узел',
color: 'red',
})
},
})
}
export function useDuplicateFlowNode() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (id: number) => flowNodesApi.duplicate(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [FLOW_NODES_QUERY_KEY] })
notifications.show({
title: 'Успешно',
message: 'Узел дублирован',
color: 'green',
})
},
onError: (error: any) => {
notifications.show({
title: 'Ошибка',
message: error.response?.data?.error_description || 'Не удалось дублировать узел',
color: 'red',
})
},
})
}