forked from yourfriendfitz/webComponentsTodo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
170 lines (159 loc) · 5.09 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>lit-html To-Do App</title>
</head>
<body style="height: 100vh; width: 100vw;">
<todo-app></todo-app>
<script type="module">
import { html } from "https://unpkg.com/lit-html/lit-html.js";
import {
component,
useState,
useEffect
} from "https://unpkg.com/haunted/haunted.js";
const asyncGet = async target => {
const data = JSON.parse(sessionStorage.getItem(target));
return data || [];
};
const asyncPost = async (target, payload = {}) => {
sessionStorage.setItem(target, JSON.stringify(payload));
};
class TodoBuilder {
static instance;
constructor() {
this.isActive = true;
}
static getInstance() {
if (TodoBuilder.instance) {
return TodoBuilder.instance;
}
TodoBuilder.instance = new TodoBuilder();
return TodoBuilder.instance;
}
setTitle(title) {
this.title = title;
return this;
}
build() {
return new Todo(this);
}
}
class Todo {
constructor(builder) {
for (let key in builder) {
this[key] = builder[key];
}
}
}
const createTodo = props => {
const builder = TodoBuilder.getInstance();
return builder.setTitle(props.title).build();
};
const TodoApp = () => {
const [todos, setTodos] = useState([]);
const [active, setActive] = useState([]);
const [notActive, setNotActive] = useState([]);
const [title, setTitle] = useState("");
useEffect(async () => {
const data = await asyncGet("todos");
setTodos(data);
}, []);
useEffect(() => {
setActive(todos.filter(({ isActive }) => isActive === true));
setNotActive(todos.filter(({ isActive }) => !isActive));
asyncPost("todos", todos);
}, [todos]);
return html`
<link rel="stylesheet" href="styles.css" />
<div class="container">
<div class="addTodo">
<input
id="title"
type="text"
@change=${e => setTitle(e.target.value)}
/>
<button
type="button"
@click=${() => setTodos([...todos, createTodo({ title })])}
>
✔
</button>
</div>
<div id="todos">
<ul>
<h1>In Progress</h1>
${active.map(
(todo, index) => html`
<li>
<button
@click=${() =>
setTodos([...todos.filter(x => x !== todo)])}
>
❌
</button>
<h3>${todo.title}</h3>
<button
@click=${() =>
setTodos([
...todos.filter(x => x !== todo),
{ ...todo, isActive: false }
])}
>
✔
</button>
</li>
`
)}
<h1>Complete</h1>
${notActive.map(
(todo, index) => html`
<li>
<button
@click=${() =>
setTodos([...todos.filter(x => x !== todo)])}
>
❌
</button>
<h3 class="strikethrough">${todo.title}</h3>
<button
@click=${() =>
setTodos([
...todos.filter(x => x !== todo),
{ ...todo, isActive: true }
])}
>
➖
</button>
</li>
`
)}
</ul>
</div>
</div>
`;
};
customElements.define("todo-app", component(TodoApp));
export { TodoApp, Todo, TodoBuilder, createTodo, asyncGet, asyncPost };
</script>
</body>
<style>
:root {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto",
"Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans",
"Helvetica Neue", sans-serif;
}
body {
background-image: -webkit-gradient(
linear,
left top,
right bottom,
from(#80bfad),
to(#d9d7d7)
);
background-image: linear-gradient(to right bottom, #80bfad, #d9d7d7);
}
</style>
</html>