aboutsummaryrefslogtreecommitdiff
path: root/src/app/manga/components/inputContainer.jsx
blob: 55ab970c2e68dc0cd5d51cd38b81e26db8898d19 (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
"use client";

import {
	Input,
	Progress,
	Card,
	CardBody,
	Image,
	Chip,
} from "@nextui-org/react";
import Link from "next/link";
import { useState } from "react";

import { SearchedMangaResults } from "./requests";

const MangaSearchBox = () => {
	const [searchedMangaTitle, setMangaSearchedTitle] = useState("");
	const [results, setResults] = useState(
		<div className="mt-4 w-full">
			<p className="text-center">
				Start typing and results will show here
			</p>
		</div>
	);

	async function GetResults() {
		if (!searchedMangaTitle) {
			setResults(<></>);
			return;
		}
		setResults(
			<Progress
				size="sm"
				isIndeterminate
				aria-label="Loading..."
				className="mb-4 mt-4 w-full"
			/>
		);

		const data = await SearchedMangaResults(searchedMangaTitle);
		const format = (
			<div className="mt-2 w-full">
				{data && data.results && data.results.length > 0 ? (
					data.results.map((item, index) => (
						<Link href={`/manga/${item.id}`} key={index}>
							<Card
								isPressable
								isBlurred
								isHoverable
								shadow="sm"
								className="mb-2 flex w-full flex-row items-center"
							>
								<Image
									src={item.image}
									width={150}
									isBlurred
									shadow="sm"
									className="p-1"
									fetchPriority="high"
								/>
								<CardBody>
									<p className="text-xl">
										{item.title.english ||
											item.title.romaji}
									</p>
									<section>
										{item.genres &&
											item.genres.map((item, index) => (
												<Chip
													key={index}
													size="sm"
													color="warning"
													variant="faded"
													className="mr-1"
												>
													{item}
												</Chip>
											))}
									</section>
								</CardBody>
							</Card>
						</Link>
					))
				) : (
					<p className="text-center">
						No results found for the searched title
					</p>
				)}
			</div>
		);
		setResults(format);
	}
	return (
		<main>
			<div className="flex w-full flex-wrap">
				<Input
					type="text"
					autoFocus
					label="Manga"
					placeholder="Enter manga/manhwa title"
					value={searchedMangaTitle}
					onChange={(event) => {
						setMangaSearchedTitle(event.target.value);
					}}
					onKeyDown={async (event) => {
						if (event.key === "Enter" || event.code === "Enter") {
							await GetResults();
						}
					}}
				/>
				{results}
			</div>
		</main>
	);
};

export default MangaSearchBox;