/
AppWithHooks.js
166 lines (153 loc) · 4 KB
/
AppWithHooks.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
156
157
158
159
160
161
162
163
164
165
import React, {
useEffect, useReducer
} from 'react'
import {TextInput, Button, ScrollView, StyleSheet, Text, View} from 'react-native'
import uuid from 'uuid/v4'
import { API, graphqlOperation } from 'aws-amplify'
import { listRestaurants} from './src/graphql/queries'
import { createRestaurant } from './src/graphql/mutations'
import { onCreateRestaurant } from './src/graphql/subscriptions'
const CLIENTID = uuid()
const initialState = {
error: null,
restaurants: [],
name: '', description: '', city: '',
}
function reducer(state, action) {
switch(action.type) {
case 'set':
return {
...state, restaurants: action.restaurants
}
case 'add':
return {
...state,
restaurants: [
...state.restaurants, action.restaurant
]
}
case 'error':
return {
...state, error: true
}
case 'updateInput':
return {
...state,
[action.inputValue]: action.value
}
default:
new Error()
}
}
async function getRestaurants(dispatch) {
try {
const restaurantData = await API.graphql(graphqlOperation(listRestaurants))
console.log('restaurantData:', restaurantData)
dispatch({
type: 'set',
restaurants: restaurantData.data.listRestaurants.items
})
} catch (err) {
dispatch({ type: 'error' })
console.log('error fetching restaurants...', err)
}
}
const updater = (value, inputValue, dispatch) => {
dispatch({
type: 'updateInput',
value,
inputValue
})
}
async function CreateRestaurant(state, dispatch) {
const { name, description, city } = state
const restaurant = {
name,
description,
city,
clientId: CLIENTID
}
const updatedRestaurantArray = [...state.restaurants, restaurant]
dispatch({
type: 'set',
restaurants: updatedRestaurantArray
})
try {
await API.graphql(graphqlOperation(createRestaurant, {
input: restaurant
}))
console.log('item created!')
} catch (err) {
console.log('error creating restaurant...', err)
}
}
function App() {
const [state, dispatch] = useReducer(reducer, initialState)
useEffect(() => {
const subscriber = API.graphql(graphqlOperation(onCreateRestaurant)).subscribe({
next: eventData => {
const restaurant = eventData.value.data.onCreateRestaurant
if(CLIENTID === restaurant.clientId) return
dispatch({ type: "add", restaurant })
}
})
return () => subscriber.unsubscribe()
}, [])
useEffect(() => {
getRestaurants(dispatch)
}, [])
console.log('state: ', state)
return (
<View style={styles.container}>
<TextInput
onChangeText={v => updater(v, 'name', dispatch)}
value={state.name}
style={{ height: 50, margin: 5, backgroundColor: "#ddd" }}
/>
<TextInput
placeholder="description"
style={{ height: 50, margin: 5, backgroundColor: "#ddd" }}
onChangeText={v => updater(v, 'description', dispatch)}
value={state.description}
/>
<TextInput
style={{ height: 50, margin: 5, backgroundColor: "#ddd" }}
onChangeText={v => updater(v, 'city', dispatch)}
value={state.city}
/>
<Button onPress={() => CreateRestaurant(state, dispatch)} title='Create Restaurant' />
{
state.restaurants.map((restaurant, index) => (
<View key={index} style={styles.restaurant}>
<Text>{restaurant.name}</Text>
<Text>{restaurant.description}</Text>
<Text>{restaurant.city}</Text>
</View>
))
}
</View>
)
}
const styles = StyleSheet.create({
restaurant: {
padding: 15,
borderBottomWidth: 2
},
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#F5FCFF',
paddingTop: 80
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
export default App