aboutsummaryrefslogtreecommitdiff
path: root/src/app/search/page.jsx
blob: 00309f6e373876a150df4e23bf76df61f56d09f1 (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
"use client";

import "./search.css";
import { FaSearch } from "react-icons/fa"; // Import the search icon from react-icons library
import { useState } from "react";
import Image from "next/image";
import Link from "next/link";
import testFunction from "./api/fetchInfo";

export default function Input() {
	const [searchedAnime, setSearchedAnime] = useState(null);
	const [loading, setLoading] = useState(null);
	const [search1, setSearch] = useState(null);

	const handleKeyPress = async (event) => {
		if (
			(event.code === "Enter" ||
				event.key === "Enter" ||
				event.code === 13) &&
			searchedAnime != ""
		) {
			setLoading(true);
			let x = await testFunction(searchedAnime);
			setSearch(x);
			setLoading(false);
		} else if (
			(event.code === "Enter" ||
				event.key === "Enter" ||
				event.code === 13) &&
			searchedAnime === ""
		) {
			alert("Input cannot be empty");
		}
	};

	return (
		<div>
			<div className="inputContainer">
				<div className="searchContainer">
					<FaSearch className="searchIcon" />
					<input
						onChange={(event) =>
							setSearchedAnime(event.target.value)
						}
						onKeyDown={(event) => handleKeyPress(event)}
						placeholder="Enter anime title"
					></input>
				</div>
			</div>

			{loading && (
				<p style={{ color: "white", textAlign: "center" }}>
					Please wait while we crunch all the data for you.
				</p>
			)}
			<div className="animeEntry">
				{search1 ? (
					search1.results && search1.results.length > 0 ? (
						search1.results.map((item, index) => (
							<Link
								key={index}
								href={`/info/${item.id}`}
								style={{ textDecoration: "none" }}
							>
								<div className="anime">
									<p>{item.title}</p>
									<Image
										src={item.image}
										className="animeImage"
										width={120}
										height={160}
										alt="Drama Poster"
									/>
								</div>
							</Link>
						))
					) : (
						<div style={{ margin: "0px auto" }}>
							<p style={{ color: "white" }}>No results found</p>
						</div>
					)
				) : null}
			</div>
		</div>
	);
}