aboutsummaryrefslogtreecommitdiff
path: root/src/app/kdrama/page.js
blob: fdd42c73d7dcf43bd9d5801fe4b70c4ae02fa5a9 (plain) (blame)
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
"use client"

import "./kdrama.css"

import { useState } from "react";
import ReactPlayer from "react-player";
import Image from 'next/image';

import {
	fetchAnimeInfo,
	fetchDramaInfo,
	fetchVideoLinks,
} from "./api/fetchAnime.js";

export default function Kdrama() {

	const [searchTitle, setSearchTitle] = useState("");
	const [searchedDrama, setSearchedDrama] = useState(null);
	async function handleKeyPresses(event) {
		if (
			(event.code === "Enter" ||
				event.code === 13 ||
				event.key === "Enter") &&
			searchTitle != ""
		) {
			const info = await fetchAnimeInfo(searchTitle);
			setSearchedDrama(info);
			document.getElementById("popup").style.display = "flex";
		}
	}

	const [details, setDetails] = useState(null);
	async function handleDramaSearch(input) {
		const drama_info = await fetchDramaInfo(input);
		setDetails(drama_info);
		document.getElementById("intro").style.display = "none";
		document.getElementById("videoContainer").style.display = "flex";
	}

	const [videoLink, setVideoLink] = useState(null);
	const [episodeNo, setEpisodeNo] = useState("");
	async function get_video_link(ep_id, drama_id, episode) {
		const link = await fetchVideoLinks(drama_id, ep_id);
		const video_link = link.sources[0].url;
		setVideoLink(video_link);
		setEpisodeNo(episode);
	}

	return (
		<main className="main">
			<div className="navbar">
				<p>Dramaverse</p>
				<input
					placeholder="Enter drama title"
					onChange={(event) => setSearchTitle(event.target.value)}
					onKeyDown={(event) => handleKeyPresses(event)}
				/>
			</div>

			<div className="intro" id="intro">
				<p className="introText">
					Start by searching for some dramas
				</p>
				<p className="introText2">
					Look for the search box above.
				</p>
			</div>

			<div className="videoContainer" id="videoContainer">
				<div className="dramaInfoContainer">
					{details && (
						<div className="dramaInfo">
							<div className="titleContainer">
								<p className="dramaTitle">
									{details.title}
								</p>
								<Image
									className="dramaImage"
									src={details.image}
									width={"160"}
									height={"240"}
									alt="Drama"
								/>
							</div>
							<p className="dramaDescription">
								{details.description}
							</p>
							<div className="episodesButtonsContainer">
								{details.episodes.map((eps, index) => (
									<button
										key={index}
										className="episodeButton"
										onClick={() =>
											get_video_link(
												eps.id,
												details.id,
												eps.episode
											)
										}
									>
										{eps.episode}
									</button>
								))}
							</div>
							<p
								style={{
									color: "white",
									fontFamily: "Atkinson Hyperlegible",
									color: "#FF6868",
								}}
							>
								Playing episode {episodeNo}
							</p>
							{videoLink && (
								<div className="videoPlayer">
									<ReactPlayer
										url={videoLink}
										autoPlay
										controls
										width={"90%"}
										height={"auto"}
										id="thing"
									/>
								</div>
							)}
						</div>
					)}
				</div>
			</div>

			<div className="popup" id="popup">
				<div className="popupEntries">
					{searchedDrama &&
						searchedDrama.results.map((item, index) => (
							<div
								className="popupEntry"
								key={index}
								onClick={() => handleDramaSearch(item.id)}
							>
								<p>{item.title}</p>
								<Image
									src={item.image}
									alt={item.title}
									width={"200"}
									height={"180"}
								/>
							</div>
						))}
				</div>

				<div
					className="closeButton"
					onClick={() =>
						(document.getElementById("popup").style.display =
							"none")
					}
				>
					<button>Close</button>
				</div>
			</div>
		</main>

	)
}