|
| 1 | +import Autocomplete from 'react-native-autocomplete-input'; |
| 2 | +import React, { useEffect, useState } from 'react'; |
| 3 | +import { SWAPI, Movies, Movie, filterMovies } from './swapi'; |
| 4 | +import { MovieDetails } from './MovieDetails'; |
| 5 | +import { Platform, StyleSheet, Text, TouchableOpacity, View, SafeAreaView } from 'react-native'; |
| 6 | + |
| 7 | +function StarWarsMovieFinder(): React.ReactElement { |
| 8 | + const [allMovies, setAllMovies] = useState<Movies>([]); |
| 9 | + const [query, setQuery] = useState(''); |
| 10 | + const queriedMovies = React.useMemo(() => filterMovies(allMovies, query), [allMovies, query]); |
| 11 | + |
| 12 | + const [firstMovieSuggestion] = queriedMovies; |
| 13 | + const suggestions: Movies = React.useMemo( |
| 14 | + () => |
| 15 | + firstMovieSuggestion?.compareTitle(query) |
| 16 | + ? [] // Close suggestion list in case movie title matches query |
| 17 | + : queriedMovies, |
| 18 | + [queriedMovies, query, firstMovieSuggestion], |
| 19 | + ); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + (async function fetchMovies() { |
| 23 | + setAllMovies(await SWAPI.getAllMovies()); |
| 24 | + })(); |
| 25 | + }, []); |
| 26 | + |
| 27 | + const isLoading = !allMovies.length; |
| 28 | + const placeholder = isLoading ? 'Loading data...' : 'Enter Star Wars film title'; |
| 29 | + |
| 30 | + return ( |
| 31 | + <SafeAreaView style={styles.saveView}> |
| 32 | + <View style={styles.container}> |
| 33 | + <View style={styles.autocompleteContainer}> |
| 34 | + <Autocomplete |
| 35 | + editable={!isLoading} |
| 36 | + autoCorrect={false} |
| 37 | + data={suggestions} |
| 38 | + value={query} |
| 39 | + onChangeText={setQuery} |
| 40 | + placeholder={placeholder} |
| 41 | + flatListProps={{ |
| 42 | + keyboardShouldPersistTaps: 'always', |
| 43 | + keyExtractor: (movie: Movie) => movie.episodeId.toString(), |
| 44 | + renderItem: ({ item }) => ( |
| 45 | + <TouchableOpacity onPress={() => setQuery(item.title)}> |
| 46 | + <Text style={styles.itemText}>{item.title}</Text> |
| 47 | + </TouchableOpacity> |
| 48 | + ), |
| 49 | + }} |
| 50 | + /> |
| 51 | + </View> |
| 52 | + |
| 53 | + <View style={styles.descriptionContainer}> |
| 54 | + {firstMovieSuggestion ? ( |
| 55 | + <MovieDetails movie={firstMovieSuggestion} /> |
| 56 | + ) : ( |
| 57 | + <Text style={styles.infoText}>Enter Title of a Star Wars movie</Text> |
| 58 | + )} |
| 59 | + </View> |
| 60 | + </View> |
| 61 | + </SafeAreaView> |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +const styles = StyleSheet.create({ |
| 66 | + saveView: { |
| 67 | + flex: 1, |
| 68 | + }, |
| 69 | + container: { |
| 70 | + position: 'relative', |
| 71 | + backgroundColor: '#F5FCFF', |
| 72 | + flex: 1, |
| 73 | + |
| 74 | + // Android requiers padding to avoid overlapping |
| 75 | + // with content and autocomplete |
| 76 | + paddingTop: 50, |
| 77 | + |
| 78 | + // Make space for the default top bar |
| 79 | + ...Platform.select({ |
| 80 | + android: { |
| 81 | + marginTop: 25, |
| 82 | + }, |
| 83 | + default: { |
| 84 | + marginTop: 0, |
| 85 | + }, |
| 86 | + }), |
| 87 | + }, |
| 88 | + itemText: { |
| 89 | + fontSize: 15, |
| 90 | + margin: 2, |
| 91 | + }, |
| 92 | + descriptionContainer: { |
| 93 | + // `backgroundColor` needs to be set otherwise the |
| 94 | + // autocomplete input will disappear on text input. |
| 95 | + backgroundColor: '#F5FCFF', |
| 96 | + marginTop: 8, |
| 97 | + }, |
| 98 | + infoText: { |
| 99 | + textAlign: 'center', |
| 100 | + }, |
| 101 | + autocompleteContainer: { |
| 102 | + // Hack required to make the autocomplete |
| 103 | + // work on Andrdoid |
| 104 | + flex: 1, |
| 105 | + left: 0, |
| 106 | + position: 'absolute', |
| 107 | + right: 0, |
| 108 | + top: 0, |
| 109 | + zIndex: 1, |
| 110 | + padding: 5, |
| 111 | + }, |
| 112 | +}); |
| 113 | + |
| 114 | +export default StarWarsMovieFinder; |
0 commit comments