Skip to content

Commit

Permalink
Implement update function
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Re committed Apr 19, 2022
1 parent 093475f commit cf8ea84
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 39 deletions.
10 changes: 5 additions & 5 deletions dist/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ window.addEventListener("load", () => {
controller.initializeUI();
});

/* Next step: Display Todos associated with the active project */
/* Next step: Enable update function */
54 changes: 25 additions & 29 deletions src/modules/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Controller {
popup;
todoDate;
todoPrio;
isUpdate;
idToUpdate;

constructor() {
this.model = new Model();
Expand All @@ -27,6 +29,8 @@ class Controller {
this.popup = document.querySelector(".popup");
this.todoDate = document.querySelector("#dateInput");
this.todoPrio = document.querySelector("#prio");
this.isUpdate = false;
this.idToUpdate = -1;
}

initializeUI() {
Expand Down Expand Up @@ -69,6 +73,7 @@ class Controller {
this.view.displayProjects(this.model.getProjects());
this.listenForProjects();
this.listenForDescriptions();
this.listenForEdit();
}

listenForTodoAdd() {
Expand All @@ -87,12 +92,22 @@ class Controller {
let todoTitle = this.todoInputField.value;
let todoDate = this.todoDate.value;
let todoPrio = this.todoPrio.value;
this.model.addTodoToProject(
this.currentProjectId,
todoTitle,
todoDate,
todoPrio
);
if(this.isUpdate){
// If we update dont create a new todo but overwrite the old one
// The id of the todo to update is saved in a global variable
// Also get the description as the popup is showing at this flow
let todoDescription = document.querySelector(`tr[id='${this.idToUpdate}']`).nextElementSibling.firstElementChild.firstElementChild.value;
this.model.updateTodo(this.currentProjectId, this.idToUpdate, todoTitle, todoDate, todoPrio, todoDescription);
this.isUpdate=false;
} else {
this.model.addTodoToProject(
this.currentProjectId,
todoTitle,
todoDate,
todoPrio
);
}
// Display all todos after the change
this.view.displayTodos(
this.model.getProject(this.currentProjectId).getTodos()
);
Expand Down Expand Up @@ -123,32 +138,13 @@ class Controller {
const editIcons = Array.from(document.querySelectorAll(".edit-icon"));
editIcons.forEach((icon)=>{
icon.addEventListener("click", ()=>{
// Get id of todo to edit
let thisTodoId =
// Set the global update flag to true so the add button handlers can know.
this.isUpdate=true;
// Get id of todo to edit and save it in global variable
this.idToUpdate =
+icon.parentNode.parentNode.previousElementSibling.getAttribute("id");
// Get data from the popup + descr
this.popup.classList.add("active");
// Hide the Add button (its listening to add a new todo)
this.popup.lastElementChild.style.display="none";
// Disable the trash can while editing
icon.parentElement.nextElementSibling.firstChild.style.display="none";
// Also disable other todos to be clicked
// Add an update button (which will update not add a new one)
const updateButton = document.createElement("button");
updateButton.textContent="update";
this.popup.appendChild(updateButton);
// Add event listener to the update button
let todoTitle = this.todoInputField.value;
let todoDate = this.todoDate.value;
let todoPrio = this.todoPrio.value;
let todoDescription = icon.parentNode.previousElementSibling.firstChild.value;
this.model.getProject(this.currentProjectId).setTodo(
thisTodoId,
todoTitle,
todoDate,
todoPrio,
todoDescription,
);
})
})
}
Expand Down
7 changes: 7 additions & 0 deletions src/modules/entities.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
class Todo {
static id = 0;
title;
due;
prio;
description;

constructor(title, due, prio) {
this.id = ++Todo.id;
this.title = title;
this.due = due;
this.prio = prio;
console.log(this.id);
}

getId(){
return this.id;
}

setDescription(text){
Expand Down
4 changes: 4 additions & 0 deletions src/modules/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class Model {

}

updateTodo(projectId, todoId, title, due, prio, description){
this.projectList[projectId].setTodo(todoId, title, due, prio, description);
}

}


Expand Down
4 changes: 0 additions & 4 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,3 @@ footer {
.active {
display: block;
}

svg {
pointer-events: auto;
}

0 comments on commit cf8ea84

Please sign in to comment.