movie-web/src/views/SearchView.tsx

67 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-02-10 22:27:57 +01:00
import { WatchedMediaCard } from "components/media/WatchedMediaCard";
import { SearchBarInput } from "components/SearchBar";
import { MWMedia, MWMediaType, MWQuery, SearchProviders } from "scrapers";
import { useEffect, useState } from "react";
2022-02-10 23:45:17 +01:00
import { ThinContainer } from "components/layout/ThinContainer";
import { SectionHeading } from "components/layout/SectionHeading";
import { Icons } from "components/Icon";
2022-02-13 18:49:03 +01:00
import { Loading } from "components/layout/Loading";
import { Tagline } from "components/Text/Tagline";
import { Title } from "components/Text/Title";
import { useDebounce } from "hooks/useDebounce";
2022-02-07 23:22:35 +01:00
export function SearchView() {
const [results, setResults] = useState<MWMedia[]>([]);
const [search, setSearch] = useState<MWQuery>({
searchQuery: "",
type: MWMediaType.MOVIE,
});
2022-02-07 23:22:35 +01:00
const debouncedSearch = useDebounce<MWQuery>(search, 2000);
useEffect(() => {
if (debouncedSearch.searchQuery !== "") runSearch(debouncedSearch);
}, [debouncedSearch]);
useEffect(() => {
setResults([]);
}, [search]);
async function runSearch(query: MWQuery) {
const results = await SearchProviders(query);
2022-02-07 23:22:35 +01:00
setResults(results);
}
const isLoading = search.searchQuery !== "" && results.length === 0;
const hasResult = results.length > 0;
2022-02-07 23:22:35 +01:00
return (
2022-02-10 23:45:17 +01:00
<ThinContainer>
{/* input section */}
<div className="mt-36 space-y-16 text-center">
2022-02-10 23:45:17 +01:00
<div className="space-y-4">
2022-02-13 18:49:03 +01:00
<Tagline>Because watching legally is boring</Tagline>
<Title>What movie do you want to watch?</Title>
2022-02-10 23:45:17 +01:00
</div>
<SearchBarInput
onChange={setSearch}
value={search}
placeholder="What movie do you want to watch?"
/>
</div>
{/* results */}
{hasResult ? (
<SectionHeading title="Search results" icon={Icons.SEARCH}>
{results.map((v) => (
<WatchedMediaCard media={v} />
))}
</SectionHeading>
) : null}
{/* Loading icon */}
{isLoading ? (
<Loading className="my-12" text="Fetching your favourite shows..." />
) : null}
2022-02-10 23:45:17 +01:00
</ThinContainer>
);
2022-02-07 23:22:35 +01:00
}