Skip to content

Instantly share code, notes, and snippets.

@MichaelStett
Last active November 3, 2020 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelStett/7947f9c61aea5b82fc03a6283313d5fd to your computer and use it in GitHub Desktop.
Save MichaelStett/7947f9c61aea5b82fc03a6283313d5fd to your computer and use it in GitHub Desktop.
Script #2
const addTodo = (desc, date) => {
let todo = new Todo(desc, date);
console.log(`Add Task... ${todo.id}`);
todos.push(todo);
localStorage.setItem('todos', JSON.stringify(todos));
// resetowanie wartości pól
addTodoInput.value = addTodoInput.defaultValue;
addTodoDate.value = addTodoDate.defaultValue;
};
var error = document.getElementById("errorMessage");
addTodoButton.addEventListener('click', (event) => {
event.preventDefault();
// Walidacja
if ((addTodoInput.value.length >= 3 && addTodoInput.value.length <= 255 ) && new Date(addTodoDate.value) >= new Date(today)) {
addTodo(addTodoInput.value, addTodoDate.value);
refreshTodoList(todos);
error.textContent = "";
error.style.display = "none";
} else {
error.style.display = "block";
error.style.color = "red";
error.textContent = "Error: todo description must have more than 3 letters and date has to be greater or equal to current date.";
}
});
todoList.addEventListener('click', (event) => {
event.preventDefault();
// Delete
if (event.target.classList.contains('deleteTodoButton')) {
let li = event.target.parentElement.parentElement.parentElement;
let id = li.getAttribute("id");
console.log(`Remove Task... ${id}`);
todos = todos.filter((item) => item.id != id);
addToLocalStorage(todos);
}
// Checkbox
if (event.target.classList.contains('completeTodo')) {
let li = event.target.parentElement.parentElement.parentElement.parentElement;
let id = li.getAttribute('id');
console.log(`Complete Task... ${id}`);
// XOR - negowanie wartości
todos.find(x => x.id == id).completed ^= true;
addToLocalStorage(todos);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment