Skip to content

Instantly share code, notes, and snippets.

@solomon-gumball
Last active April 24, 2022 03:43
Show Gist options
  • Save solomon-gumball/a447f87b4e1fd984a4d6ef6f8a8bc9fd to your computer and use it in GitHub Desktop.
Save solomon-gumball/a447f87b4e1fd984a4d6ef6f8a8bc9fd to your computer and use it in GitHub Desktop.
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