-
Notifications
You must be signed in to change notification settings - Fork 0
/
TodoPage.js
158 lines (115 loc) · 4.08 KB
/
TodoPage.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
import { h, Fragment } from 'preact'
import {useState, useEffect} from 'preact/hooks'
import { Input, Form, Button, Modal, DatePicker } from 'antd'
import TodoContext from '../context/TodoContext'
import TodoItems from '../components/TodoItems'
import { useModal } from '../hooks'
import { Select } from 'antd'
// const API_ROUTE='localhost:3000'
const API_ROUTER='https://pwa-postgre.heroukuapp.com'
const TodoPage = (props) => {
const [todoItems, setTodoItems] = useState([])
console.log('use State')
const [showModal, openModal, closeModal] = useModal()
const [form] = Form.useForm()
const updateTodo = (indice, nuevoTodo) =>{
console.log('Update Todo Indice',indice)
const nuevoTodoList = [...todoItems]
nuevoTodoList[indice] = nuevoTodo
setTodoItems (nuevoTodoList)
}
const removeTodo = index => {
const nuevoTodoList = [...todoItems]
nuevoTodoList.splice(index,1 )
console.log('Nuevo Todo List Eliminat', nuevoTodoList)
setTodoItems(nuevoTodoList)
}
useEffect (() => {
fetch(`${API_ROUTE}/api/1/todos`)
.then(res => res.json())
.then((data) => {
setTodoItems(
data
)
})
}, [])
const addTodo = e => {
// Gets Raw Data from the Form
// Transform the Date into usable format
const todo = form.getFieldsValue()
console.log(todo)
fetch(`${API_ROUTE}/api/1/todos` ,{
method: 'POST',
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
body: JSON.stringify(todo)
})
.then(res => res.json())
.then(res => {
console.log("res", res)
})
console.log(todo)
// Adds the Todo Item to the array
// Resets Form
console.log(form)
form.resetFields()
// Closes the Form
closeModal()
setTodoItems([...todoItems, todo])
}
console.log('Todo', todoItems)
console.log('Show Modal', showModal)
return (
<Fragment>
<h1 className="text-center mt-2" style="color: white; font-weight:bold;"> Things To Do</h1>
<div className="text-center" > <Button id="addtodo" onClick={openModal}> Add </Button></div>
<Modal id="modal" title='Add Form'
visible={(()=> {
console.log('Show Modal Add Form', showModal)
return showModal
})()}
onCancel={closeModal}
footer={[
<Button onClick={closeModal} >
Cancel
</Button>,
<Button onClick={addTodo}>Submit</Button>
]}
>
<Form form={form} >
<Form.Item
label='Todo'
name='todo'
>
<Input />
</Form.Item>
<Form.Item
label='Descripcion'
name='descripcion'
>
<Input/>
</Form.Item>
<Form.Item
label='Estado'
name='status'
>
<Select defaultValue="pendiente" style={{ width: 120 }} >
<Option value="pendiente">Pendiente</Option>
<Option value="realizado">Realizado</Option>
</Select>
</Form.Item>
<Form.Item
label='Date'
name='endDate'
>
<DatePicker ></DatePicker>
</Form.Item>
</Form>
</Modal>
<TodoContext.Provider value={{todoList : todoItems, updateTodo, removeTodo}}><TodoItems todos={todoItems}></TodoItems></TodoContext.Provider>
</Fragment>
)
}
export default TodoPage