aboutsummaryrefslogtreecommitdiff
path: root/src/app/anime/components/search.jsx
blob: 2631791aa7e3611eda6c0c0b943c2b80bb596e26 (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
"use client";

import { FaSearch } from "react-icons/fa";
import { useState } from "react";
import { Input, Link, Button, Progress } from "@nextui-org/react";

import SearchResults from "./search_results";

const SearchBar = () => {
	const [title, setTitle] = useState("");
	const [searchResults, setSearchResults] = useState(null);
	const [loading, setLoading] = useState(false);

	const handleSearchInput = async (title) => {
		setSearchResults(null);
		setLoading(
			<Progress
				size="sm"
				isIndeterminate
				aria-label="Loading..."
				className="w-full mt-2 lg:w-1/2"
			/>
		);
		setSearchResults(await SearchResults(title));
		setLoading(false);
	};

	return (
		<main>
			<section>
				<div className="flex w-full md:flex-nowrap gap-2 lg:w-1/2">
					<Input
						type="text"
						label="Search for anime"
						placeholder="Enter anime title here"
						autoComplete="off"
						onKeyDown={async (event) => {
							if (
								event.code === "Enter" ||
								event.key === "Enter" ||
								event.code === 13
							) {
								await handleSearchInput(title);
							}
						}}
						onChange={(event) => {
							if (event.target.value.trim() != "") {
								setTitle(event.target.value);
							}
						}}
						startContent={<FaSearch />}
					/>

					<Link href={"/anime/continueWatching"}>
						<Button color="primary">History</Button>
					</Link>
				</div>
				{loading}
			</section>
			<div className="mt-2">{searchResults}</div>
		</main>
	);
};

export default SearchBar;