-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
155 lines (138 loc) · 4.2 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
143
144
145
146
147
148
149
150
151
152
153
154
155
import React, { useState, useEffect } from 'react';
import { View, Text, Button, FlatList, TouchableOpacity, StyleSheet } from 'react-native';
import * as SQLite from 'expo-sqlite';
const db = SQLite.openDatabase('beer.db');
const Gelada = () => {
const [beerData, setBeerData] = useState(null);
const [searchHistory, setSearchHistory] = useState([]);
useEffect(() => {
createBeerTable();
loadSearchHistory();
}, []);
const createBeerTable = () => {
db.transaction(tx => {
tx.executeSql(
`
CREATE TABLE IF NOT EXISTS beers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
brand TEXT,
name TEXT,
style TEXT,
yeast TEXT,
malts TEXT,
alcohol TEXT
)
`
);
});
};
const fetchRandomBeer = async () => {
try {
const response = await fetch('https://random-data-api.com/api/beer/random_beer');
const data = await response.json();
setBeerData(data);
db.transaction(tx => {
if (tx.executeSql) {
tx.executeSql(
'INSERT INTO beers (brand, name, style, yeast, malts, alcohol) VALUES (?, ?, ?, ?, ?, ?)',
[data.brand, data.name, data.style, data.yeast, data.malts, data.alcohol],
(_, resultSet) => {
if (resultSet.rowsAffected > 0) {
console.log('Cerveja inserida!');
}
},
(_, error) => {
console.error('Erro ao inserir!', error);
}
);
} else {
console.warn('executeSql is not available');
}
});
loadSearchHistory();
} catch (error) {
console.error('Erro ao carregar:', error);
}
};
const loadSearchHistory = () => {
db.transaction(tx => {
if (tx.executeSql) {
tx.executeSql('SELECT * FROM beers', [], (_, resultSet) => {
const entries = resultSet.rows.raw().map(row => ({
id: row.id,
brand: row.brand,
name: row.name,
style: row.style,
yeast: row.yeast,
malts: row.malts,
alcohol: row.alcohol,
}));
setSearchHistory(entries);
});
} else {
console.warn('executeSql is not available');
}
});
};
const renderSearchItem = ({ item }) => (
<TouchableOpacity onPress={() => showBeerDetails(item)}>
<View style={styles.searchItem}>
<Text style={styles.searchItemText}>Brand: {item.brand}</Text>
<Text style={styles.searchItemText}>Name: {item.name}</Text>
<Text style={styles.searchItemText}>Style: {item.style}</Text>
</View>
</TouchableOpacity>
);
const showBeerDetails = beer => {
setBeerData(beer);
};
const renderSeparator = () => <View style={styles.separator} />;
return (
<View style={styles.container}>
<View style={styles.buttonContainer}>
<Button title="Desce uma gelada" onPress={fetchRandomBeer} color="orange" />
</View>
{beerData && (
<View style={styles.beerDetails}>
<Text style={styles.beerDetailText}>Brand: {beerData.brand}</Text>
<Text style={styles.beerDetailText}>Name: {beerData.name}</Text>
<Text style={styles.beerDetailText}>Style: {beerData.style}</Text>
<Text style={styles.beerDetailText}>Yeast: {beerData.yeast}</Text>
<Text style={styles.beerDetailText}>Malts: {beerData.malts}</Text>
<Text style={styles.beerDetailText}>Alcohol: {beerData.alcohol}</Text>
</View>
)}
<Text style={styles.searchHistoryTitle}>Na mesa:</Text>
<FlatList
data={searchHistory}
renderItem={renderSearchItem}
keyExtractor={item => item.id.toString()}
ItemSeparatorComponent={renderSeparator}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingTop: '45%',
},
buttonContainer: {
marginTop: 20,
},
beerDetails: {
marginBottom: 20,
},
beerDetailText: {
fontSize: 16,
marginBottom: 5,
},
searchHistoryTitle: {
fontSize: 18,
fontWeight: 'bold',
marginBottom: 20,
},
});
export default Gelada;