-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathgraphqlHooks.js
173 lines (158 loc) · 4.03 KB
/
graphqlHooks.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
166
167
168
169
170
171
172
173
import React, {
useEffect, useReducer
} from 'react'
import {
Text, TextInput, View, Button
} 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:
return state
}
}
async function CreateRestaurant(state, dispatch) {
const { name, description, city } = state
const restaurant = {
name,
description,
city,
clientId: CLIENTID
}
const restaurants = [...state.restaurants, restaurant]
dispatch({
type: 'set',
restaurants
})
try {
await API.graphql(graphqlOperation(createRestaurant, {
input: restaurant
}))
console.log('restaurant created!')
} catch (err) {
console.log('error creating restaurant...', err)
}
}
const callDispatch = (value, inputValue, dispatch) => {
dispatch({
type: 'updateInput',
value,
inputValue
})
}
function App() {
const [state, dispatch] = useReducer(reducer, initialState)
useEffect(() => {
getRestaurants()
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()
}, [])
async function getRestaurants() {
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)
}
}
console.log('state: ', state)
return (
<View style={styles.container}>
<TextInput
placeholder="name"
style={{ height: 50, margin: 5, backgroundColor: "#ddd" }}
onChangeText={value => callDispatch(value, 'name', dispatch)}
value={state.name}
/>
<TextInput
placeholder="description"
style={{ height: 50, margin: 5, backgroundColor: "#ddd" }}
onChangeText={value => callDispatch(value, 'description', dispatch)}
value={state.description}
/>
<TextInput
placeholder="city"
style={{ height: 50, margin: 5, backgroundColor: "#ddd" }}
onChangeText={value => callDispatch(value, 'city', dispatch)}
value={state.city}
/>
<Button
title='Create Restaurant'
onPress={() => CreateRestaurant(state, dispatch)}
/>
{
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 = {
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