import Loading from "@/components/shared/loading"; import Image from "next/image"; import { useState } from "react"; import { toast } from "sonner"; // Define a function to convert the data function convertData(episodes) { const convertedData = episodes.map((episode) => ({ episode: episode.episode, title: episode?.title, description: episode?.description || null, img: episode?.img?.hd || episode?.img?.mobile || null, // Use hd if available, otherwise use mobile })); return convertedData; } export default function AppendMeta({ api }) { const [id, setId] = useState(); const [resultData, setResultData] = useState(null); const [query, setQuery] = useState(""); const [tmdbId, setTmdbId] = useState(); const [hasilQuery, setHasilQuery] = useState([]); const [season, setSeason] = useState(); const [override, setOverride] = useState(); const [loading, setLoading] = useState(false); const handleSearch = async () => { try { setLoading(true); setResultData(null); const res = await fetch(`${api}/meta/tmdb/${query}`); const json = await res.json(); const data = json.results.filter((i) => i.type === "TV Series"); setHasilQuery(data); setLoading(false); } catch (err) { console.log(err); } }; const handleDetail = async () => { try { setLoading(true); const res = await fetch(`${api}/meta/tmdb/info/${tmdbId}?type=TV%20Series `); const json = await res.json(); const data = json.seasons; setHasilQuery(data); setLoading(false); } catch (err) { console.log(err); } }; const handleStore = async () => { try { setLoading(true); if (!resultData && !id) { console.log("No data to store"); setLoading(false); return; } const data = await fetch("/api/v2/admin/meta", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ id: id, data: resultData, }), }); if (data.status === 200) { const json = await data.json(); toast.success(json.message); setLoading(false); } } catch (err) { console.log(err); } }; const handleOverride = async () => { setResultData(JSON.parse(override)); }; return ( <>
{JSON.stringify(resultData, null, 2)}
{i.releaseDate}
{JSON.stringify(hasilQuery, null, 2)}
{JSON.stringify(resultData, null, 2)}