import { useCallback, useState } from 'react'

/**
 * `useActionSubmit` is a function that returns a callback function and a boolean indicating whether
 * the callback is currently being executed
 * @param callback - A function that will be called when the submit button is clicked.
 * @returns The submit function and the loading state.
 */
const useActionSubmit = (callback) => {
  const [loading, setLoading] = useState(false)

  const submit = useCallback(
    async (data) => {
      setLoading(true)
      await callback?.(data)
      setLoading(false)
    },
    [callback]
  )

  return [submit, loading]
}

export default useActionSubmit
