"use client"; import { FormEvent, useState } from "react"; import { useRouter } from "next/navigation"; export function ApifyIngestForm({ configured }: { configured: boolean }) { const router = useRouter(); const [busy, setBusy] = useState(false); const [message, setMessage] = useState(""); const [error, setError] = useState(false); async function submit(event: FormEvent) { event.preventDefault(); const form = new FormData(event.currentTarget); const searchTerms = String(form.get("searchTerms") || "").split(",").map((term) => term.trim()).filter(Boolean); const maxResults = Number(form.get("maxResults")); const maxCostUsd = Number(form.get("maxCostUsd")); if (!window.confirm(`${searchTerms.join(", ")} için en fazla ${maxResults} reklam çekilsin mi? Apify harcama üst sınırı $${maxCostUsd.toFixed(2)}.`)) return; setBusy(true); setMessage(""); try { const response = await fetch("/api/admin/ingest/apify", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ searchTerms, country: form.get("country"), adActiveStatus: form.get("adActiveStatus"), mediaType: form.get("mediaType"), maxResults, maxCostUsd, scrapeAdDetails: form.get("scrapeAdDetails") === "on", includeAboutPage: form.get("includeAboutPage") === "on" }) }); const data = await response.json().catch(() => ({})); if (!response.ok) throw new Error(data.error || "APIFY_INGEST_FAILED"); setError(false); setMessage(`${data.imported} reklam işlendi, ${data.failed} kayıt atlandı.`); router.refresh(); } catch (caught) { setError(true); setMessage(caught instanceof Error ? caught.message : "İşlem başarısız"); } finally { setBusy(false); } } return (
{message &&

{message}

}
); }