1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
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 (
<>
<div className="container mx-auto p-4 scrol">
<h1 className="text-3xl font-semibold mb-4">Append Data Page</h1>
<div>
<div className="space-y-3 mb-4">
<label>Search Anime:</label>
<input
type="text"
className="w-full px-3 py-2 border rounded-md text-black"
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
<button
type="button"
className="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600"
onClick={handleSearch}
>
Find Anime{" "}
</button>
</div>
<div className="space-y-3 mb-4">
<label>Get Episodes:</label>
<input
type="number"
placeholder="TMDB ID"
className="w-full px-3 py-2 border rounded-md text-black"
value={tmdbId}
onChange={(e) => setTmdbId(e.target.value)}
/>
<button
type="button"
className="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600"
onClick={handleDetail}
>
Get Details
</button>
</div>
<div className="space-y-3 mb-4">
<label>Override Result:</label>
<textarea
rows="5"
className="w-full px-3 py-2 border rounded-md text-black"
value={override}
onChange={(e) => setOverride(e.target.value)}
/>
<button
type="button"
className="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600"
onClick={handleOverride}
>
Override{" "}
</button>
</div>
<div className="space-y-3 mb-4">
<label className="block text-sm font-medium text-gray-300">
Anime ID:
</label>
<input
type="number"
placeholder="AniList ID"
className="w-full px-3 py-2 border rounded-md text-black"
value={id}
onChange={(e) => setId(e.target.value)}
/>
</div>
<div className="mb-4">
<button
className="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600"
onClick={handleStore}
>
Store Data {season && `Season ${season}`}
</button>
</div>
{!loading && hasilQuery?.some((i) => i?.season) && (
<div className="border rounded-md p-4 mt-4">
<h2 className="text-lg font-semibold mb-2">
Which season do you want to format?
</h2>
<div className="w-full flex gap-2">
{hasilQuery?.map((season, index) => (
<button
type="button"
className="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600"
key={index}
onClick={() => {
setLoading(true);
const data = hasilQuery[index].episodes;
const convertedData = convertData(data);
setSeason(index + 1);
setResultData(convertedData);
console.log(convertedData);
setLoading(false);
}}
>
<p>{season.season} </p>
</button>
))}
</div>
</div>
)}
{!loading && resultData && (
<div className="border rounded-md p-4 mt-4">
<h2 className="text-lg font-semibold mb-2">Season {season}</h2>
<pre>{JSON.stringify(resultData, null, 2)}</pre>
</div>
)}
{!loading && hasilQuery && (
<div className="border rounded-md p-4 mt-4">
{/* <h2 className="text-lg font-semibold mb-2">
Result Data,{" "}
{hasilQuery.length > 0 && `${hasilQuery.length} Seasons`}:
</h2> */}
<div className="flex flex-wrap gap-10">
{!hasilQuery.every((i) => i?.episodes) &&
hasilQuery?.map((i, index) => (
<div
key={i.id}
className="flex flex-col items-center gap-2"
>
<p className="font-karla font-semibold">
{i.releaseDate}
</p>
<Image
src={i.image}
alt="query-image"
width={500}
height={500}
className="w-[160px] h-[210px] object-cover"
/>
<button
className="bg-blue-500 text-white w-[160px] py-1 rounded-md hover:bg-blue-600 text-sm"
onClick={() => {
setTmdbId(i.id);
}}
>
<p className="line-clamp-1 px-1">{i.title}</p>
</button>
</div>
))}
</div>
<pre>{JSON.stringify(hasilQuery, null, 2)}</pre>
</div>
)}
{loading && <Loading />}
</div>
<div>
{/* {resultData && (
<div className="border rounded-md p-4 mt-4">
<h2 className="text-lg font-semibold mb-2">Result Data:</h2>
<pre>{JSON.stringify(resultData, null, 2)}</pre>
</div>
)} */}
</div>
</div>
</>
);
}
|