Skip to content

Commit

Permalink
project lis and todo rows have ids now
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Re committed Apr 14, 2022
1 parent 8893677 commit 3f42131
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
8 changes: 4 additions & 4 deletions dist/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Controller} from "./modules/controller";

window.addEventListener("load", () => {
const controller = new Controller();
controller.listenForProjectAdd();
controller.listenForTodoAdd();
controller.listenForProjects();
controller.initializeUI();
});

/* Next step: Display Todos associated with the active project */
15 changes: 12 additions & 3 deletions src/modules/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@ class Controller {
this.projectInputField = document.querySelector("#projectsInputField");
this.addProjectButton = document.querySelector(".addProjectButton");
this.currentProjectId = 0;
this.allCurrentProjects = document.querySelector("li");
}


initializeUI(){
this.view.displayProjects(this.model.getProjects());
this.listenForProjects();
this.listenForProjectAdd();
this.listenForTodoAdd();
}

listenForProjects() {
this.allCurrentProjects = Array.from(document.querySelectorAll("li"));
this.allCurrentProjects?.forEach((project) => {
project.addEventListener("click", (e) => {
this.currentProjectId = e.target.getAttribute("id");
console.log(this.currentProjectId);
});
});
}
Expand All @@ -35,13 +43,14 @@ class Controller {
let projectTitle = this.projectInputField.value;
this.model.addProject(projectTitle);
this.view.displayProjects(this.model.getProjects());
this.listenForProjects();
});
}

listenForTodoAdd() {
this.addTodoButton.addEventListener("click", () => {
let todoTitle = this.todoInputField.value;
this.model.addTodoToProject(this.currentProjectId);
this.model.addTodoToProject(this.currentProjectId, todoTitle);
this.view.displayTodos(
this.model.getProject(this.currentProjectId).getTodos()
);
Expand Down
4 changes: 1 addition & 3 deletions src/modules/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ class Model {
}

addProject(title){
console.log("inside model: add project");
this.projectList.push(new Project(title));
}

getProject(id){
console.log(this.projectList[id]);
return this.projectList[id];
}

getProjects(){
console.log("inside model: get projects");
return this.projectList;
}

addTodoToProject(id, title, due, prio){
this.projectList[id].addTodo(title,due,prio);
console.log(this.projectList);
}

}
Expand Down
12 changes: 7 additions & 5 deletions src/modules/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,34 @@ class View {

displayProjects(currentProjectsList) {
this.projectList.innerHTML = "";
let id = 0;
currentProjectsList.forEach((project) => {
this.projectList.appendChild(this.createLi(project.title));
this.projectList.appendChild(this.createLi(project.title, id++));
});
}

createLi(projectTitle) {
createLi(projectTitle, id) {
const newLi = document.createElement("li");
newLi.textContent = projectTitle;
let id = parseInt(newLi.previousElementSibling?.getAttribute("id")) +1 || 0;
newLi.setAttribute("id", id);
return newLi;
}

displayTodos(currentTodoArray) {
// Wipeout current list
this.todoTable.innerHTML = "";
let id = 0;
// loop through list
currentTodoArray.forEach((todo) => {
// Create and add a new row to the table
this.todoTable.appendChild(createRow(todo.title));
this.todoTable.appendChild(this.createRow(todo.title, id++));
});
}

createRow(title) {
createRow(title, id) {
const newRow = document.createElement("tr");
newRow.innerHTML = `<td>${title}</td>`;
newRow.setAttribute("id", id);
return newRow;
}
}
Expand Down

0 comments on commit 3f42131

Please sign in to comment.