<form class="js-toDoForm">
<input type="text" placeholder="Write a to do " />
</form>
<h1 id="PENDING">PENDING</h1>
<ul class="js-toDoList"></ul>
<h1 id="FINISHED">FINISHED</h1>
<ul class="js-toDoFList"></ul>
const toDoForm = document.querySelector(".js-toDoForm"),
toDoInput = toDoForm.querySelector("input"),
ul = document.querySelector(".js-toDoList"),
toDoFList = document.querySelector("#FINISHED"),
toDoFList2 = document.querySelector(".js-toDoFList");
let toDos = [];
let finishedToDos = [];
function BackToDo(event) {
const checkBtn = document.createElement("button");
const li = event.target.parentNode;
li.removeChild(event.target);
ul.appendChild(li);
checkBtn.innerText = "✅";
li.appendChild(checkBtn);
checkBtn.addEventListener("click", finishToDo);
const modifiedToDos = finishedToDos.filter(function (toDo) {
return toDo.id !== parseInt(li.id);
});
const cleanToDos = finishedToDos.filter(function (toDo) {
return toDo.id === parseInt(li.id);
});
toDos.push(cleanToDos[0]);
finishedToDos = modifiedToDos;
saveToDos();
}
function finishToDo(event) {
const backBtn = document.createElement("button");
const li = event.target.parentNode;
li.removeChild(event.target);
toDoFList2.appendChild(li);
backBtn.innerText = "⏪";
li.appendChild(backBtn);
backBtn.addEventListener("click", BackToDo);
const modifiedToDos = toDos.filter(function (toDo) {
return toDo.id !== parseInt(li.id);
});
const cleanToDos = toDos.filter(function (toDo) {
return toDo.id === parseInt(li.id);
});
finishedToDos.push(cleanToDos[0]);
toDos = modifiedToDos;
saveToDos();
}
function deleteToDo(event) {
const li = event.target.parentNode;
const ul = li.parentNode;
ul.removeChild(li);
const cleanToDos = toDos.filter(function (toDo) {
return toDo.id !== parseInt(li.id);
});
const cleanFToDos = finishedToDos.filter(function (toDo) {
return toDo.id !== parseInt(li.id);
});
toDos = cleanToDos;
finishedToDos = cleanFToDos;
saveToDos();
}
function saveToDos() {
localStorage.setItem("PENDING", JSON.stringify(toDos));
localStorage.setItem("FINISHED", JSON.stringify(finishedToDos));
}
let indexNum = 1;
function paintFinishToDo(text) {
const li = document.createElement("li");
const delBtn = document.createElement("button");
const backBtn = document.createElement("button");
const span = document.createElement("span");
const newId = indexNum;
delBtn.innerText = "❌";
delBtn.addEventListener("click", deleteToDo);
backBtn.innerText = "⏪";
backBtn.addEventListener("click", BackToDo);
span.innerText = text;
li.appendChild(span);
li.appendChild(delBtn);
li.appendChild(backBtn);
li.id = newId;
toDoFList2.appendChild(li);
const toDoObj = {
text: text,
id: newId
};
finishedToDos.push(toDoObj);
saveToDos();
indexNum += 1;
}
function paintToDo(text) {
const li = document.createElement("li");
const delBtn = document.createElement("button");
const checkBtn = document.createElement("button");
const span = document.createElement("span");
const newId = indexNum;
delBtn.innerText = "❌";
delBtn.addEventListener("click", deleteToDo);
checkBtn.innerText = "✅";
checkBtn.addEventListener("click", finishToDo);
span.innerText = text;
li.appendChild(span);
li.appendChild(delBtn);
li.appendChild(checkBtn);
li.id = newId;
ul.appendChild(li);
const toDoObj = {
text: text,
id: newId
};
toDos.push(toDoObj);
saveToDos();
indexNum += 1;
}
function handleSubmit(event) {
event.preventDefault();
const currentValue = toDoInput.value;
paintToDo(currentValue);
toDoInput.value = "";
}
function loadToDos() {
const loadedToDos = localStorage.getItem("PENDING");
const loadedFinishedToDos = localStorage.getItem("FINISHED");
if (loadedToDos) {
const parsedToDos = JSON.parse(loadedToDos);
parsedToDos.forEach(function (toDo) {
paintToDo(toDo.text);
});
}
if (loadedFinishedToDos !== null ) {
const parsedFinishedToDos = JSON.parse(loadedFinishedToDos);
parsedFinishedToDos.forEach(function (toDo) {
console.log(toDo);
console.log(toDo.text);
paintFinishToDo(toDo.text);
});
}
}
function init() {
loadToDos();
toDoForm.addEventListener("submit", handleSubmit);
}
init();