/
index.js
143 lines (131 loc) · 3.97 KB
/
index.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
import { useState, useEffect } from 'react'
import Head from 'next/head'
import styles from '../styles/Home.module.css'
const ToDo = ({ content, isCompleted, onChange, onDelete }) => {
const cards = ['card', 'card2', 'card3', 'card4', 'card5']
return (
<div className={styles[cards[Math.floor(Math.random() * cards.length)]]}>
<div
className={styles.text}
style={{ textDecoration: isCompleted ? 'line-through' : '' }}
>
{content}
</div>
<div className={styles.reverseWrapper}>
<input
type="checkbox"
className={styles.check}
checked={isCompleted}
onChange={onChange}
/>
<button className={styles.delBtn} onClick={onDelete}>
✕
</button>
</div>
</div>
)
}
export default function Home() {
const [newContent, setNewContent] = useState('')
const [toDos, setToDos] = useState([])
const getToDos = async () => {
const resp = await fetch('api/todos')
const toDos = await resp.json()
setToDos(toDos)
}
const createToDo = async () => {
await fetch('api/todos', {
method: 'post',
body: JSON.stringify({ content: newContent }),
})
await getToDos()
}
const updateToDo = async (todo) => {
let newBody = {
...todo,
isCompleted: !todo.isCompleted,
}
await fetch(`api/todos/${todo.key}`, {
method: 'put',
body: JSON.stringify(newBody),
})
await getToDos()
}
const deleteToDo = async (tid) => {
await fetch(`api/todos/${tid}`, { method: 'delete' })
setTimeout(getToDos, 300)
}
useEffect(() => {
getToDos()
}, [])
const completed = toDos.filter((todo) => todo.isCompleted)
const notCompleted = toDos.filter((todo) => !todo.isCompleted)
return (
<div className={styles.container}>
<Head>
<title>deta + next.js</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<header className={styles.header}>
<h2>
<a href="https://www.deta.sh">deta base</a> +{' '}
<a href="https://nextjs.org">next.js</a> to dos
</h2>
</header>
<main className={styles.main}>
<div className={styles.incomplete}>
<div className={styles.firstRow}>
<div className={styles.title}>to dos</div>
<div className={styles.reverseWrapper}>
<input
className={styles.inpt}
onChange={(e) => setNewContent(e.target.value)}
></input>
<button className={styles.addBtn} onClick={createToDo}>
✛
</button>
</div>
</div>
<div className={styles.scrolly}>
{notCompleted.map((todo, index) => (
<ToDo
key={todo.key}
content={`${index + 1}. ${todo.content}`}
isCompleted={todo.isCompleted}
onChange={() => updateToDo(todo)}
onDelete={() => deleteToDo(todo.key)}
/>
))}
</div>
</div>
<div className={styles.complete}>
<div className={styles.firstRow}>
<div className={styles.title}>done</div>
</div>
<div className={styles.scrolly}>
{completed.map((todo, index) => (
<ToDo
key={todo.key}
content={`${index + 1}. ${todo.content}`}
isCompleted={todo.isCompleted}
onChange={() => updateToDo(todo)}
onDelete={() => deleteToDo(todo.key)}
/>
))}
</div>
</div>
</main>
<footer className={styles.footer}>
<a
href="https://deta.sh?ref=create-next-deta-app"
target="_blank"
rel="noopener noreferrer"
>
powered by
<img src="/deta.svg" alt="Deta Logo" className={styles.logo} />
deta & next.js
</a>
</footer>
</div>
)
}