Last active
April 24, 2022 03:43
-
-
Save solomon-gumball/a447f87b4e1fd984a4d6ef6f8a8bc9fd 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({ movies }: { movies: Movie[] }): JSX.Element | |
declare function LoadingIndicator(): JSX.Element | |
declare function ErrorMessageUI({ message }: { message: string }): JSX.Element | |
type Movie = { title: string, id: string } | |
type ViewState = { | |
loading: boolean, | |
error?: Error, | |
searchResults?: Movie[], | |
} | |
export default function MovieSearch() { | |
const [searchTerm, setSearchTerm] = useState<string>('') | |
const [viewState, setViewState] = useState<ViewState>({ loading: true }) | |
useEffect(() => { | |
setViewState({ loading: true }) | |
searchForMovies(searchTerm).then(movies => { | |
setViewState({ loading: false, searchResults: movies }) | |
}).catch((error: Error) => { | |
setViewState({ loading: false, error: error }) | |
}) | |
}, [searchTerm]) | |
return ( | |
<div> | |
<input value={searchTerm} onChange={e => setSearchTerm(e.target.value)} /> | |
{viewState.searchResults && <MovieList movies={viewState.searchResults} />} | |
{viewState.loading && <LoadingIndicator />} | |
{viewState.error && <ErrorMessageUI message={viewState.error.message} />} | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment