-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex(amplify-example).js
148 lines (139 loc) · 3.32 KB
/
index(amplify-example).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
import { useReducer } from 'react'
import { API, graphqlOperation } from 'aws-amplify'
import nanoid from 'nanoid'
import produce from 'immer'
import config from '../src/aws-exports'
import {
createTodo,
deleteTodo,
createTodoList,
} from '../src/graphql/mutations'
import { getTodoList } from '../src/graphql/queries'
const MY_ID = nanoid()
API.configure(config)
const reducer = (state, action) => {
switch (action.type) {
case 'add-todo': {
return produce(state, (draft) => {
draft.todos.push(action.payload)
})
}
case 'delete-todo': {
const index = state.todos.findIndex(({ id }) => action.payload === id)
if (index === -1) return state
return produce(state, (draft) => {
draft.todos.splice(index, 1)
})
}
case 'reset-current': {
return produce(state, (draft) => {
draft.currentName = ''
})
}
case 'set-current': {
return produce(state, (draft) => {
draft.currentName = action.payload
})
}
default: {
return state
}
}
}
const createToDo = async (dispatch, currentToDo) => {
const todo = {
id: nanoid(),
name: currentToDo,
createdAt: `${Date.now()}`,
completed: false,
todoTodoListId: 'global',
userId: MY_ID,
}
dispatch({ type: 'add-todo', payload: todo })
dispatch({ type: 'reset-current' })
try {
await API.graphql(graphqlOperation(createTodo, { input: todo }))
} catch (err) {
dispatch({ type: 'set-current', payload: todo.name })
console.warn('Error adding to do ', err)
}
}
const deleteToDo = async (dispatch, id) => {
dispatch({ type: 'delete-todo', payload: id })
try {
await API.graphql({
...graphqlOperation(deleteTodo),
variables: { input: { id } },
})
} catch (err) {
console.warn('Error deleting to do ', err)
}
}
const App = (props) => {
const [state, dispatch] = useReducer(reducer, {
todos: props.todos,
currentName: '',
})
return (
<div>
<h3>Add a Todo</h3>
<form
onSubmit={(ev) => {
ev.preventDefault()
createToDo(dispatch, state.currentName)
}}
>
<input
value={state.currentName}
onChange={(e) => {
dispatch({ type: 'set-current', payload: e.target.value })
}}
/>
<button type="submit">Create Todo</button>
</form>
<h3>Todos List</h3>
{state.todos.map((todo, index) => (
<p key={index}>
<a href={`/todo/${todo.id}`}>{todo.name}</a>
<button
onClick={() => {
deleteToDo(dispatch, todo.id)
}}
>
delete
</button>
</p>
))}
</div>
)
}
export const getStaticProps = async () => {
let result = await API.graphql(
graphqlOperation(getTodoList, { id: 'global' })
)
if (result.errors) {
console.log('Failed to fetch todolist.', result.errors)
throw new Error(result.errors[0].message)
}
if (result.data.getTodoList !== null) {
return {
props: {
todos: result.data.getTodoList.todos.items,
},
}
}
await API.graphql(
graphqlOperation(createTodoList, {
input: {
id: 'global',
createdAt: `${Date.now()}`,
},
})
)
return {
props: {
todos: [],
},
}
}
export default App