Files
winninghunter/src/components/tiktok-shop-import.tsx
T

30 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
export function TikTokShopImport({ planLimit }: { planLimit: number }) {
const router = useRouter();
const [query, setQuery] = useState("");
const [region, setRegion] = useState("US");
const [count, setCount] = useState(Math.min(10, planLimit || 10));
const [busy, setBusy] = useState(false);
const [message, setMessage] = useState("");
async function run() {
setBusy(true); setMessage("");
try {
const response = await fetch("/api/tiktok-shop/import", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ query, region, maxResults: count }) });
const data = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(data.error || "TIKTOK_IMPORT_FAILED");
setMessage(`${data.imported} ürün güncellendi.`); router.refresh();
} catch (error) { setMessage(error instanceof Error ? error.message : "İçe aktarma başarısız."); }
finally { setBusy(false); }
}
return <div className="mb-5 grid gap-3 rounded-3xl bg-white p-4 shadow-soft md:grid-cols-[1fr_120px_130px_130px]">
<input value={query} onChange={(e) => setQuery(e.target.value)} placeholder="TikTok Shop ürününü canlı ara" className="rounded-2xl border border-slate-200 px-4 py-3" />
<select value={region} onChange={(e) => setRegion(e.target.value)} className="rounded-2xl border border-slate-200 px-3"><option>US</option><option>GB</option><option>SG</option><option>MY</option><option>PH</option><option>TH</option><option>VN</option><option>ID</option></select>
<select value={count} onChange={(e) => setCount(Number(e.target.value))} className="rounded-2xl border border-slate-200 px-3">{[10,25,50].filter((v) => v <= planLimit).map((v) => <option key={v} value={v}>{v} ürün</option>)}</select>
<button type="button" onClick={run} disabled={busy || planLimit === 0 || query.trim().length < 2} className="rounded-2xl bg-slate-950 px-4 py-3 font-bold text-white disabled:opacity-50">{busy ? "Getiriliyor…" : planLimit ? "Canlı veriden getir" : "Planı yükselt"}</button>
{message && <p className="text-sm font-semibold text-violet-700 md:col-span-4">{message}</p>}
</div>;
}