import { useMutation, useQueryClient } from '@tanstack/react-query'
import { createChartComponent, deleteChartComponent } from '@/api/data'
import { message } from 'antd'

// 创建
export function useCreateChart() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: (data: { name: string; type: string; content: string }) => createChartComponent(data),
    onSuccess: () => {
      message.success('创建成功')
      queryClient.invalidateQueries({ queryKey: ['chartComponentList'] })
    },
  })
}

// 删除
export function useDeleteChart() {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: (data: { id: string }) => deleteChartComponent(data),
    onSuccess: () => {
      message.success('删除成功')
      queryClient.invalidateQueries({ queryKey: ['chartComponentList'] })
    },
  })
}
