Last active
April 25, 2022 01:27
-
-
Save solomon-gumball/ba19cd39249ea3d6fb66334e48dc0109 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare function searchForMovies(query?: string): Promise<Movie[]> | |
declare function MovieList(props: { movies: Movie[] }): JSX.Element | |
declare function LoadingIndicator(): JSX.Element | |
declare function ErrorMessageUI(props: { message: string }): JSX.Element | |
type Movie = { title: string, id: string } | |
export function MovieSearch() { | |
const [searchTerm, setSearchTerm] = useState<string>('') | |
const [searchResults, setSearchResults] = useState<Movie[]>() | |
const [isLoading, setIsLoading] = useState(false) | |
const [error, setError] = useState<Error>() | |
useEffect(() => { | |
setIsLoading(true) | |
searchForMovies(searchTerm).then(data => { | |
setSearchResults(data) | |
}).catch((error: Error) => { | |
setError(error) | |
}).finally(() => { | |
setIsLoading(false) | |
}) | |
}, [searchTerm]) | |
return ( | |
<div> | |
<input value={searchTerm} onChange={e => setSearchTerm(e.target.value)} /> | |
{searchResults && <MovieList movies={searchResults} />} | |
{isLoading && <LoadingIndicator />} | |
{error && <ErrorMessageUI message={error.message} />} | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment