Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions vanilla/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,29 @@ const SingleTaskClassNames = {
}

const addTask = () => {
// maybe add a helper that wraps document.querySelector(xxx) instead of writing it every time?
const form = document.querySelector('#new_task_form');
// you are only using task_input in this function why not select it directly?
const input = form.task_input.value;

if (input != '') {
// why is this line here and not inside "createTask" ?
// It's nicer to write .xxx > .yyy
Comment thread
davidvaks marked this conversation as resolved.
// and better not select any element by tag-name - always have a class / id / attribute to select by
Comment thread
davidvaks marked this conversation as resolved.
const tableBody = document.querySelector('.incomplete_tasks>tbody');
createTaskNewRow(tableBody, input);
form.task_input.value = '';
}
};

// not sure about the name "row" - it's abstract task creation - maybe it wont be a row in the future?
const createTaskNewRow = (table, taskInput) => {
const checkbox = createCheckbox();
const taskText = createTaskTextElement(taskInput);
const editButton = createEditButton();
const deleteButton = createRemoveButton();

// can all this be in a generic helper that gets any number of table row children cells?
const row = table.insertRow();
row.insertCell(0).appendChild(checkbox);
row.insertCell(1).appendChild(taskText);
Expand All @@ -38,8 +45,10 @@ const createTaskNewRow = (table, taskInput) => {
};

const itemCheckboxChange = (event) => {
// i'm not sure path is encouraged or standard, and even if it was, it's too fragile
// what if you changed the html? this would break. maybe use "Element.closest" ?
const row = event.path[2];
const taskTextCell = row.cells[1];
const taskTextCell = row.cells[1]; // this is fragile too
Comment on lines +48 to +51
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is one of the places I really didn't like.
tried doing
event.target.parentNode() like in the MDN but got an exception for parentNode not being a function...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so yeah closest should work.

const taskTextElement = taskTextCell.querySelector(selectorFor(SingleTaskClassNames.TEXT));

if (event.target.checked) {
Expand All @@ -54,15 +63,15 @@ const itemCheckboxChange = (event) => {
};

const removeItem = (event) => {
const row = event.path[2];
const row = event.path[2]; // dont use path
row.remove();
};

const editItem = (event) => {
const editButton = event.target;
const row = event.path[2];
const taskTextCell = row.cells[1];
const checkbox = row.cells[0].firstChild;
const row = event.path[2]; // no path
const taskTextCell = row.cells[1]; // fragile
const checkbox = row.cells[0].firstChild; // same

if (isItemEditMode(editButton)) {
exitEditModeAndUpdate(checkbox, taskTextCell, editButton);
Expand Down Expand Up @@ -97,13 +106,14 @@ const toggleEditButtonText = (editButton) => {
};

const isItemEditMode = (editButton) => {
// maybe there's a better way than checking the text - it's fragile and could change
Comment thread
davidvaks marked this conversation as resolved.
return editButton.value == ButtonTexts.SAVE;
};

const selectorFor = (className) => '.' + className;

// Elements Creation

// how about a generic element creator helper that also sets props?
Comment thread
davidvaks marked this conversation as resolved.
const createTaskTextElement = (taskInput) => {
const taskText = document.createElement('p');
taskText.textContent = taskInput;
Expand All @@ -120,6 +130,7 @@ const createTaskEditInput = (taskText) => {
};

const createStrikeThroughElement = () => {
// why do you need strikethrough class if you're using del?
Comment thread
davidvaks marked this conversation as resolved.
const strikeThrough = document.createElement('del');
strikeThrough.className = SingleTaskClassNames.STRIKE_THROUGH;
return strikeThrough;
Expand Down