-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathAppWithHooks.js
176 lines (164 loc) · 4.07 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
166
167
168
169
170
171
172
173
174
175
176
import React, {
useEffect, useReducer
} from 'react'
import uuid from 'uuid/v4'
import { API, graphqlOperation } from 'aws-amplify'
import { listTalks} from './graphql/queries'
import { createTalk } from './graphql/mutations'
import { onCreateTalk } from './graphql/subscriptions'
const CLIENTID = uuid()
const initialState = {
error: null,
talks: [],
name: '', description: '', speakerName: '', speakerBio: ''
}
function reducer(state, action) {
switch(action.type) {
case 'set':
return {
...state, talks: action.talks
}
case 'add':
return {
...state,
talks: [
...state.talks, action.talk
]
}
case 'error':
return {
...state, error: true
}
case 'updateInput':
return {
...state,
[action.inputValue]: action.value
}
default:
new Error()
}
}
async function getTalks(dispatch) {
try {
const talkData = await API.graphql(graphqlOperation(listTalks))
console.log('talkData:', talkData)
dispatch({
type: 'set',
talks: talkData.data.listTalks.items
})
} catch (err) {
dispatch({ type: 'error' })
console.log('error fetching talks...', err)
}
}
const updater = (value, inputValue, dispatch) => {
dispatch({
type: 'updateInput',
value,
inputValue
})
}
async function CreateTalk(state, dispatch) {
const { name, description, speakerName, speakerBio } = state
const talk = {
name,
description,
speakerName,
speakerBio,
clientId: CLIENTID
}
const updatedTalkArray = [...state.talks, talk]
dispatch({
type: 'set',
talks: updatedTalkArray
})
try {
await API.graphql(graphqlOperation(createTalk, {
input: talk
}))
console.log('item created!')
} catch (err) {
console.log('error creating talk...', err)
}
}
function App() {
const [state, dispatch] = useReducer(reducer, initialState)
useEffect(() => {
const subscriber = API.graphql(graphqlOperation(onCreateTalk)).subscribe({
next: eventData => {
const talk = eventData.value.data.onCreateTalk
if(CLIENTID === talk.clientId) return
dispatch({ type: "add", talk })
}
})
return () => subscriber.unsubscribe()
}, [])
useEffect(() => {
getTalks(dispatch)
}, [])
console.log('state: ', state)
return (
<div style={styles.container}>
<input
placeholder="name"
style={{ height: 50, margin: 5, backgroundColor: "#ddd" }}
onChange={e => updater(e.target.value, 'name', dispatch)}
value={state.name}
/>
<input
placeholder="description"
style={{ height: 50, margin: 5, backgroundColor: "#ddd" }}
onChange={e => updater(e.target.value, 'description', dispatch)}
value={state.description}
/>
<input
placeholder="speaker name"
style={{ height: 50, margin: 5, backgroundColor: "#ddd" }}
onChange={e => updater(e.target.value, 'speakerName', dispatch)}
value={state.speakerName}
/>
<input
placeholder="speaker bio"
onChange={e => updater(e.target.value, 'speakerBio', dispatch)}
value={state.speakerBio}
style={{ height: 50, margin: 5, backgroundColor: "#ddd" }}
/>
<button onClick={() => CreateTalk(state, dispatch)}>
Create Talk
</button>
{
state.talks.map((talk, index) => (
<div key={index} style={styles.talk}>
<p>{talk.name}</p>
<p>{talk.description}</p>
<p>{talk.speakerName}</p>
<p>{talk.speakerBio}</p>
</div>
))
}
</div>
)
}
const styles = {
talk: {
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