aboutsummaryrefslogtreecommitdiff
path: root/src/app/web-series/components/searchBar.jsx
blob: 42531bc07aa6f0efe21ad18ff1fae99efd7dab94 (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
"use client";
import styles from "../styles/search.module.css";
import { FaSearch } from "react-icons/fa";
import { useState } from "react";

import { SEARCH_TV } from "./data-fetch";
import SearchResults from "./searchResults";

const SearchBar = () => {
	const [title, setTitle] = useState("");
	const [result, setResults] = useState(null);
	const [loading, setloading] = useState(false);

	const fetch_results = async (title) => {
		setloading(true);
		setResults(await SearchResults(await SEARCH_TV(title)));
		setloading(false);
	};

	return (
		<main className={styles.Main}>
			<section className={styles.InputContainer}>
				<FaSearch
					color="white"
					className={styles.SearchIcon}
					size={22}
				/>
				<input
					placeholder="Enter series title here"
					onChange={(event) => setTitle(event.target.value)}
					onKeyDown={async (e) => {
						if ((e.key === "Enter" || e.code === 13) && title) {
							await fetch_results(e.target.value);
						}
					}}
				></input>
			</section>

			{loading && (
				<p style={{ color: "white", textAlign: "center" }}>
					Please wait while we crunch up all the data
				</p>
			)}
			<section className={styles.SearchResults}>{result}</section>
		</main>
	);
};

export default SearchBar;