import { useState } from "react"
import { API_URL } from "../../config"

const useAddAnnouncement = () => {
    const [status, setStatus] = useState("Опубликовать")

    const doAdd = async (formData) => {
        setStatus(true)
        try {
            const res = await fetch(API_URL + "/announcement", {
                method: 'PUT',
                body: formData,
            })

            const data = await res.json()

            if (!data.Answer) {
                throw new Error("Не удалось опубликовать объявление")
            }
            setStatus("Опубликовано")

        } catch (err) {
            setStatus(err.message ?? err)
            setTimeout(() => setStatus("Опубликовать"), 10000)
        }
    } 

    return {doAdd, status}
}

export default useAddAnnouncement