158 lines
5.1 KiB
TypeScript
158 lines
5.1 KiB
TypeScript
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',
|
||
})
|
||
},
|
||
})
|
||
} |