Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
93 lines (84 loc) · 2.41 KB
/
Copy pathApp.js
File metadata and controls
93 lines (84 loc) · 2.41 KB
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
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import { withAuthenticator } from 'aws-amplify-react-native';
import { API, graphqlOperation } from 'aws-amplify'
import { listCitys as ListCities} from './src/graphql/queries'
import { createCity as CreateCity } from './src/graphql/mutations'
import { ListItem, Button, FormInput } from 'react-native-elements'
class App extends Component<Props> {
state = { cities: [], name: '', country: ''}
async componentDidMount(){
try {
const {data: {listCitys: {items }}} = await API.graphql(graphqlOperation(ListCities))
this.setState({ cities:items })
console.log('cities: ', items)
} catch (err) {
console.log('error fetching data: ', err)
}
}
onChange = (key,value) => {
this.setState({ [key]: value})
}
createCity = async () => {
const city = {
name: this.state.name,
country: this.state.country,
}
const cities = [...this.state.cities, city] //takes the old array and put the new city
this.setState({cities, name:'', country:''})
try {
await API.graphql(graphqlOperation(CreateCity, {input: city}))
console.log('city successfully created!!')
}catch (err){
console.log('error: ', err)
}
}
render() {
return (
<View style={styles.container}>
<FormInput
onChangeText={v => this.onChange('name', v)}
placeholder = 'City Name'
value={this.state.name}
/>
<FormInput
onChangeText={v => this.onChange('country', v)}
placeholder = 'Country'
value={this.state.country}
/>
<Button
style={{margin:20}}
onPress={this.createCity}
title= 'Add City'
backgroundColor='#ffa100'
/>
{
this.state.cities.map((item, i) => (
<ListItem
key={i}
title={item.name}
subtitle={item.description}
/>
))
}
</View>
);
}
}
export default withAuthenticator(App, { includeGreetings: true}) //greetings add logout button sign out button
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});