Skip to content

Commit 5ed3ab5

Browse files
Update app.js
First update in a long while!! Done quite a bit in this one. So, first of all, the major changes are that you can now actually add tasks! There's the visual tasks with all your task info that pop up once you click the 'submit' button. So that's good! All tasks render immediately and everything works okay! Other than that, I added a little bit of code to change filter button appearances when clicked. Class changing and all that, you know. My first time trying it out!! Lastly, I've made a little change to the updateTaskCount function so that it says '1 item left' instead of '1 items left' when you've only got one task left. Of course, it still says '(number of tasks) items left' for other numbers, including 0. I'm pretty proud of all the stuff I've done so far, and I'm excited to be one step closer to finishing KittyTasker! I think my biggest challenge now is adding in the kitty assets.
1 parent 86fc07d commit 5ed3ab5

1 file changed

Lines changed: 103 additions & 6 deletions

File tree

app.js

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ function updateDocumentTitle () {
3737
function updateTaskCount() {
3838
itemsLeft.textContent = tasks.length;
3939
updateDocumentTitle();
40+
41+
if(tasks.length === 1) {
42+
itemsLeft.textContent = `${tasks.length} item left`;
43+
} else {
44+
itemsLeft.textContent = `${tasks.length} items left`;
45+
}
4046
}
4147

4248
/**
@@ -66,6 +72,55 @@ function createTaskObject(description, priority, date) {
6672

6773
}
6874

75+
/**
76+
* Renders a single task element
77+
* @param {Object} taskData - The task data object
78+
* @returns {HTMLElement} - The created task list item
79+
*/
80+
function renderTaskElement(taskData) {
81+
// Crete the list item element
82+
var li = document.createElement('li');
83+
li.className = 'task-item';
84+
85+
86+
// Add the priority class
87+
li.classList.add('task-item-' + taskData.priority);
88+
89+
// Create and add the checkbox
90+
var checkbox = document.createElement('input');
91+
checkbox.type = 'checkbox';
92+
checkbox.className = 'task-checkbox';
93+
li.appendChild(checkbox);
94+
95+
// Create and add content container
96+
var content = document.createElement('div');
97+
content.className = 'task-content';
98+
99+
// Add task title
100+
var title = document.createElement('p');
101+
title.className = 'task-text';
102+
title.textContent = taskData.description;
103+
content.appendChild(title);
104+
105+
// Add task details
106+
var details = document.createElement('small');
107+
details.className = 'task-details';
108+
details.textContent = 'Priority: ' + taskData.priority + ' | Due: ' + taskData.date;
109+
content.appendChild(details);
110+
111+
// Add content to the list item
112+
li.appendChild(content);
113+
114+
// Add delete button
115+
var deleteBtn = document.createElement('button');
116+
deleteBtn.className = 'delete-btn';
117+
deleteBtn.textContent = 'Delete';
118+
li.appendChild(deleteBtn);
119+
120+
return li;
121+
}
122+
123+
69124
/**
70125
* Handle task form submission
71126
* @param {Event} e - The form submission event
@@ -92,14 +147,53 @@ function handleFormSubmit(e) {
92147
// Add to tasks array
93148
tasks.push(newTask);
94149

95-
// Update the UI
96-
updateTaskCount();
97-
98-
console.log('Task added:', newTask);
99-
console.log('Total tasks:', tasks.length)
150+
// Render the new task
151+
renderTasks();
100152

101153
// Reset form
102154
taskForm.reset();
155+
}
156+
157+
/**
158+
* Renders all tasks in the tasks array
159+
*/
160+
function renderTasks() {
161+
// Clear exisiting tasks from the DOM
162+
taskList.innerHTML = '';
163+
164+
// Render each task
165+
for (var i = 0; i < tasks.length; i++) {
166+
var taskElement = renderTaskElement(tasks[i]);
167+
taskList.appendChild(taskElement);
168+
}
169+
170+
// Update task count
171+
updateTaskCount();
172+
}
173+
174+
/**
175+
* Handle filter button clicks
176+
* @param {Event} e - The click event
177+
*/
178+
function handleFilterClick(e) {
179+
// Check if a fliter button was clicked
180+
if(e.target.classList.contains('filter-btn')) {
181+
// Get the filter type from the data-filter attribute
182+
var filterType = e.target.getAttribute('data-filter');
183+
184+
// Update the current filter
185+
currentFilter = filterType;
186+
187+
// Update the active class on filter buttons
188+
for (var i = 0; i < filterButtons.length; i++) {
189+
filterButtons[i].classList.remove('active');
190+
}
191+
e.target.classList.add('active');
192+
193+
console.log('Filter changed to: ', filterType);
194+
195+
// Actual filtering coming soon
196+
}
103197
}
104198

105199
/**
@@ -123,10 +217,13 @@ function initApp() {
123217

124218
// Add event listeners
125219
taskForm.addEventListener('submit', handleFormSubmit);
220+
document.querySelector('nav').addEventListener('click', handleFilterClick);
221+
222+
// Render tasks (will be empty intially)
223+
renderTasks();
126224

127225
// Update the document title
128226
updateDocumentTitle();
129-
130227
console.log('KittyTasker app intialised');
131228
}
132229

0 commit comments

Comments
 (0)