-
Notifications
You must be signed in to change notification settings - Fork 0
/
task-list.html
168 lines (151 loc) · 4.18 KB
/
task-list.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task Manager</title>
<link rel="stylesheet" href="https://unpkg.com/normalize.css/normalize.css">
<style type="text/css">
body {
font-family: 'Arial', sans-serif;
padding: 20px;
font-size: 16px;
background-color: #f4f4f9;
}
.task {
display: flex;
align-items: center;
justify-content: space-between;
background: white;
padding: 10px 20px;
margin-bottom: 10px;
border-radius: 5px;
border-left: 5px solid;
}
.high {
border-color: red;
}
.medium {
border-color: orange;
}
.low {
border-color: green;
}
.completed {
text-decoration: line-through;
color: #bbb;
}
.remove-button {
color: red;
cursor: pointer;
border: none;
background: none;
font-size: 16px;
}
</style>
<script src="https://unpkg.com/[email protected]"></script>
</head>
<body>
<div id="app">
<form @submit.prevent="addTask">
<!-- Adding tasks form -->
<input ref="newTaskInput" placeholder="Add new task" v-model="newTask">
<select v-model="newPriority">
<option disabled value="">Select priority</option>
<option>High</option>
<option>Medium</option>
<option>Low</option>
</select>
<button type="submit">Add Task</button>
</form>
<!-- Task filtering -->
<input placeholder="Filter tasks..." v-model="filterText">
<!-- If there no tasks -->
<div v-if="tasks.length === 0">
<p>No tasks found. Try changing the filter or add some tasks!</p>
</div>
<div v-else>
<h3>Your Tasks</h3>
<!-- This is a list of tasks -->
<div v-for="(task, index) in filteredTasks" :key="task.id"
:class="{'task': true, completed: task.done, [task.priority.toLowerCase()]: true}">
<div>
<!-- Is task done? -->
<input type="checkbox" v-model="task.done">
<!-- Task description -->
<span>{{task.description}}</span>
</div>
<!-- Removing a task -->
<button class="remove-button" @click="removeTask(index)">✖</button>
</div>
</div>
</div>
<script>
const { createApp, onMounted, onBeforeMount, ref, watch, watchEffect, computed } = Vue;
const App = {
setup() {
const tasks = ref(
JSON.parse(
localStorage.getItem('tasks') ?? '[]'
)
);
const filteredTasks = computed(
() => tasks.value.filter(
task => task.description.toLowerCase()
.includes(filterText.value.toLowerCase())
)
);
const newTaskInput = ref(null);
const newTask = ref('');
const newPriority = ref('');
const filterText = ref('');
onMounted(() => {
newTaskInput.value.focus();
});
const addTask = () => {
if (newTask.value !== '' && newPriority.value !== '') {
tasks.value.unshift({
id: tasks.value.length + 1,
description: newTask.value,
priority: newPriority.value,
done: false
});
newTask.value = '';
newPriority.value = '';
}
}
const removeTask = (index) => {
tasks.value.splice(index, 1);
};
// watch(newTask, (newValue, oldValue) => {
// console.log(
// `New value is ${newValue}, old was ${oldValue}`
// );
// }, { immediate: true });
// watchEffect(() => {
// console.log(
// `New task input value is ${newTask.value}`
// )
// });
watch(tasks, () => {
localStorage.setItem(
'tasks',
JSON.stringify(tasks.value)
)
}, { deep: true });
return {
newTaskInput,
tasks,
addTask,
newTask,
newPriority,
removeTask,
filterText,
filteredTasks
}
},
};
createApp(App).mount('#app');
</script>
</body>
</html>