forked from dabit3/aws-amplify-workshop-react
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp-03.js
More file actions
115 lines (101 loc) · 2.89 KB
/
Copy pathApp-03.js
File metadata and controls
115 lines (101 loc) · 2.89 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// src/App.js
import React, { useEffect, useReducer } from 'react'
import { API, graphqlOperation } from 'aws-amplify'
import { withAuthenticator } from 'aws-amplify-react'
import { listPets } from './graphql/queries'
import { createPet as CreatePet } from './graphql/mutations'
// import uuid to create a unique client ID
import uuid from 'uuid/v4'
const CLIENT_ID = uuid()
// create initial state
const initialState = {
name: '', breed: '', age: 0, pets: [], breeds: []
}
// create reducer to update state
function reducer(state, action) {
switch (action.type) {
case 'SETPETS':
return { ...state, pets: action.pets }
case 'SETBREEDS':
return { ...state, breeds: action.breeds }
case 'SETINPUT':
return { ...state, [action.key]: action.value }
default:
return state
}
}
function App() {
const [state, dispatch] = useReducer(reducer, initialState)
useEffect(() => {
getData()
}, [])
async function getData() {
try {
const breedsData = await API.get('breedsapi', '/breeds')
console.log('data from REST API: ', breedsData)
dispatch({ type: 'SETBREEDS', breeds: breedsData })
const petData = await API.graphql(graphqlOperation(listPets))
console.log('data from GRAPHQL: ', petData)
dispatch({ type: 'SETPETS', pets: petData.data.listPets.items })
} catch (err) {
console.log('error fetching data..', err)
}
}
async function createPet() {
const { name, breed, age } = state
if (name === '' || breed === '' || age === 0) return
const pet = {
name, breed, age: parseInt(age)
}
const pets = [...state.pets, pet]
dispatch({ type: 'SETPETS', pets })
console.log('pet:', pet)
try {
await API.graphql(graphqlOperation(CreatePet, { input: pet }))
console.log('item created!')
} catch (err) {
console.log('error creating pet...', err)
}
}
// change state then user types into input
function onChange(e) {
dispatch({ type: 'SETINPUT', key: e.target.name, value: e.target.value })
}
function makeOption(option) {
return <option>{option}</option>
}
// add UI with event handlers to manage user input
return (
<div>
<input
name='name'
placeholder='name'
onChange={onChange}
value={state.name}
/>
<select
name='breed'
placeholder='breed'
onChange={onChange}
value={state.breed}
>{state.breeds.map(makeOption)}</select>
<input
name='age'
placeholder='age'
onChange={onChange}
value={state.age}
/>
<button onClick={createPet}>Create Pet</button>
{
state.pets.map((p, i) => (
<div key={i}>
<h2>{p.name}</h2>
<h4>{p.age}</h4>
<p>{p.breed}</p>
</div>
))
}
</div>
)
}
export default withAuthenticator(App, { includeGreetings: true })