-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
142 lines (135 loc) · 3.51 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React, { useState, useEffect } from 'react';
import {
SafeAreaView,
StyleSheet,
Text,
TouchableOpacity,
View,
Image
} from 'react-native';
import Autocomplete from 'react-native-autocomplete-input';
const App = () => {
const [movies, setFilms] = useState([]);
const [filteredFilms, setFilteredFilms] = useState([]);
const [selectedValue, setSelectedValue] = useState({});
useEffect(() => {
let i = 0;
let auxArr = [];
while (i < 5) {
i = i + 1
async function fetchData() {
try {
await fetch((`http://www.omdbapi.com/?apikey=5eec5adc&s=love&y=2020&type=movie&page=${i}`)).then((res) => res.json())
.then((json) => {
if (json.Search.length > 0) {
for (let f in json.Search) {
auxArr.push(json.Search[f])
}
};
return {
Search: auxArr
};
})
.then((data) => {
const { Search: movies } = data;
setFilms(movies);
})
} catch (error) {
if (error) throw error;
}
}
fetchData();
}
}, []);
const searchMovie = (query) => {
if (query) {
const regex = new RegExp(`${query.trim()}`, 'i');
setFilteredFilms(
movies.filter((film) => {
console.log(film.Title)
return film.Title.search(regex) >= 0
})
);
} else {
// Si no hay resultado, retorna vacío
setFilteredFilms([]);
}
};
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={styles.container}>
<Autocomplete
autoCapitalize="none"
autoCorrect={false}
containerStyle={styles.autocompleteContainer}
data={filteredFilms}
defaultValue={
JSON.stringify(selectedValue) === '{}' ?
'' :
selectedValue.title
}
onChangeText={(text) => searchMovie(text)}
placeholder="Ingrese el título de la película"
renderItem={({ item }) => (
<TouchableOpacity
onPress={() => {
setSelectedValue(item);
setFilteredFilms([]);
}}>
<Text style={styles.itemText}>
{item.Title}
</Text>
</TouchableOpacity>
)}
/>
<View style={styles.descriptionContainer}>
{movies.length > 0 ? (
<>
<Text>
Título: {JSON.stringify(selectedValue.Title)}
</Text>
<Text>
Año: {JSON.stringify(selectedValue.Year)}
</Text>
<Text>
Tipo: {JSON.stringify(selectedValue.Type) == '"movie"' ? 'Película' : ''}
</Text>
</>
) : (
<Text style={styles.infoText}>
Espere que carguen las películas, por favor!
</Text>
)}
</View>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: "#6788db",
flex: 1,
padding: 16,
marginTop: 0,
},
autocompleteContainer: {
backgroundColor: '#6788db',
borderWidth: 0,
},
descriptionContainer: {
flex: 10,
justifyContent: 'center',
alignContent: 'center'
},
itemText: {
fontSize: 15,
paddingTop: 5,
paddingBottom: 5,
margin: 2,
},
infoText: {
textAlign: 'center',
fontSize: 16,
},
});
export default App;