import { useMutation } from '@tanstack/react-query'
import { api } from '../api'
import { toast } from 'react-toastify'

export const useFilesMutation = () => {
  const uploadFile = useMutation({
    mutationFn: async (newFile: FormData) => {
      const response = await api.post('/files/upload', newFile, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })

      return response.data.data
    },
    onSuccess: data => {
      toast.success('File uploaded successfully!')
    },
    onError: (error: any) => {
      toast.error(error.response.data.errors[0].cause)
    }
  })

  return { uploadFile }
}
