-
Notifications
You must be signed in to change notification settings - Fork 39
Exercise 1 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Exercise 1 #4
Changes from all commits
670030c
e92d485
5806bb7
351c776
a774dc2
3a43072
ff37ae3
05b73aa
f9205ca
eedc353
bf7cc1a
39552cc
4f2ed9d
5308740
2f427f8
0eaa09b
ca31b8a
4f4091d
90dc0cd
2a84b8d
26d6c92
62ffd1d
9dd2373
fda2fbd
1e367e3
8bc2e99
fb28578
b2a23e1
e828a71
2a27e38
f5c331b
ab4287b
460e2ad
5fcd9ce
aa7efdc
634b470
1a4280f
726cf10
cc0f812
1e482c3
6668084
f2a7158
2396dfe
542f7d1
260cbcc
48722f4
228cc41
72ba01f
3030f2f
9cdbdff
ecff181
1ddce54
36ee2f8
0fce44f
62372be
2466257
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "monday-u-exercises", | ||
"description": "Welcome to monday-u official github repository! We are very excited to have you here! This is going to be so much fun! Here are a few general details:", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/epent/monday-u-exercises.git" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/epent/monday-u-exercises/issues" | ||
}, | ||
"homepage": "https://github.com/epent/monday-u-exercises#readme" | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Task Manager</title> | ||
<link rel="stylesheet" href="style_ex1.css" /> | ||
<link rel="preconnect" href="https://fonts.googleapis.com" /> | ||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> | ||
<link | ||
href="https://fonts.googleapis.com/css2?family=Dosis:wght@300;400;500;600&display=swap" | ||
rel="stylesheet" | ||
/> | ||
</head> | ||
<body> | ||
<div class="white-box"> | ||
<h1 class="main-header">Task Manager</h1> | ||
<div class="task-input-wrapper"> | ||
<input class="task-input" type="text" placeholder="Add your new task" /> | ||
<button id="add-task-button" class="task-button hithere"> | ||
Add task | ||
</button> | ||
</div> | ||
<ul class="task-list"> | ||
<div id="triangle" class="triangle"></div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "ul" should only have "li" direct children |
||
<div id="box" class="box"></div> | ||
<h2 id="welcome-sentence" class="welcome"> | ||
Can't wait when you add your first task! | ||
</h2> | ||
</ul> | ||
<button id="sort-by-name-button" class="task-button hidden"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you might want to wrap the sort & clear all buttons with a footer container to easier positioning |
||
Sort by name | ||
</button> | ||
<button id="clear-all-button" class="task-button hidden"> | ||
Clear All | ||
</button> | ||
</div> | ||
</body> | ||
<script src="script_ex1.js"></script> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
const addButton = document.querySelector("#add-task-button"); | ||
const triangle = document.querySelector("#triangle"); | ||
const box = document.querySelector("#box"); | ||
const welcome = document.querySelector("#welcome-sentence"); | ||
const addTaskButton = document.querySelector(".task-button"); | ||
const addTaskInput = document.querySelector(".task-input"); | ||
const clearAllButton = document.querySelector("#clear-all-button"); | ||
const sortByNameButton = document.querySelector("#sort-by-name-button"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. having all the elements outside here requires all the dom elements to exist when this file is loaded, which might not be the case in complex web apps that dynamically change the dom depending on the view |
||
const listOfTasks = []; | ||
|
||
addTaskButton.addEventListener("click", addTask); | ||
clearAllButton.addEventListener("click", clearAll); | ||
sortByNameButton.addEventListener("click", sortByName); | ||
|
||
addTaskInput.addEventListener("keypress", (event) => { | ||
if (event.key === "Enter") { | ||
addTask(); | ||
} | ||
}); | ||
|
||
function addTask() { | ||
const task = document.querySelector(".task-input").value; | ||
const text = capitalize(task); | ||
|
||
if (text === "") { | ||
alert("Input cannot be empty"); | ||
} else { | ||
if (listOfTasks.length === 0) { | ||
hideWelcomePart(); | ||
} | ||
listOfTasks.push(text); | ||
createTask(text); | ||
} | ||
} | ||
|
||
function hideWelcomePart() { | ||
addButton.classList.remove("hithere"); | ||
clearAllButton.classList.remove("hidden"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving the welcome page into its own container (maybe with a shared class for some of the styles) will make your life easier here - 1 line of code instead of 6. Also in the current state, every new element you'll add to the task-list container will require you to handle it here as well |
||
sortByNameButton.classList.remove("hidden"); | ||
|
||
triangle.classList.add("hidden"); | ||
box.classList.add("hidden"); | ||
welcome.classList.add("hidden"); | ||
} | ||
|
||
function showWelcomePart() { | ||
addButton.classList.add("hithere"); | ||
clearAllButton.classList.add("hidden"); | ||
sortByNameButton.classList.add("hidden"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here :) |
||
|
||
triangle.classList.remove("hidden"); | ||
box.classList.remove("hidden"); | ||
welcome.classList.remove("hidden"); | ||
} | ||
|
||
function createTask(text) { | ||
//create elements | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good job on the separation of concerns between the functions! |
||
const divElm = document.createElement("div"); | ||
const liElm = document.createElement("li"); | ||
const deleteButton = document.createElement("button"); | ||
|
||
// style elements | ||
divElm.classList.add("task"); | ||
divElm.classList.add("grow"); | ||
liElm.classList.add("task-item"); | ||
deleteButton.classList.add("delete-button"); | ||
|
||
//add textNodes | ||
liElm.appendChild(document.createTextNode(text)); | ||
deleteButton.appendChild(document.createTextNode("X")); | ||
|
||
//add ids | ||
const idText = text.split(" ").join("-"); | ||
divElm.setAttribute("id", `${idText}`); | ||
deleteButton.setAttribute("id", `${idText}-button`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be dangerous as these ids are not unique. |
||
|
||
//append <li> and <button> to <div> | ||
divElm.appendChild(liElm); | ||
divElm.appendChild(deleteButton); | ||
|
||
//append <div> to <ul> | ||
const taskList = document.querySelector(".task-list"); | ||
taskList.appendChild(divElm); | ||
|
||
//add clickListener to button | ||
deleteButton.addEventListener("click", deleteTask); | ||
liElm.addEventListener("click", () => alert(`Task: ${text}`)); | ||
|
||
//clear input | ||
document.querySelector(".task-input").value = ""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be moved into it's own function |
||
} | ||
|
||
function deleteTask({ target }) { | ||
const buttonId = target.id.split("-"); | ||
buttonId.pop(); | ||
|
||
//remove <div> with task | ||
const divElementId = buttonId.join("-"); | ||
document.querySelector(`#${divElementId}`).remove(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider eaither using unique ids for the tasks. You could use UUID or traverse the dom from the target up until you find the task (target.parentElement) |
||
|
||
// remove task from list | ||
const taskText = buttonId.join(" "); | ||
const elementIndex = listOfTasks.findIndex((task) => { | ||
return task === taskText; | ||
}); | ||
listOfTasks.splice(elementIndex, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: removing a task with a not unique name might not remove the one the user wanted. Consider making each task a complex object with id and text properties. |
||
|
||
//show weolcome if list is empty | ||
if (listOfTasks.length === 0) { | ||
showWelcomePart(); | ||
} | ||
} | ||
|
||
function clearAll() { | ||
listOfTasks.forEach((task) => { | ||
const taskId = task.split(" ").join("-"); | ||
document.querySelector(`#${taskId}`).remove(); | ||
}); | ||
listOfTasks.splice(0); | ||
|
||
showWelcomePart(); | ||
} | ||
|
||
function sortByName() { | ||
listOfTasks.forEach((task) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. duplication, consider moving it into it's own function |
||
const taskId = task.split(" ").join("-"); | ||
document.querySelector(`#${taskId}`).remove(); | ||
}); | ||
|
||
listOfTasks.sort(); | ||
console.log(listOfTasks); | ||
|
||
listOfTasks.forEach((task) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
createTask(task); | ||
}); | ||
} | ||
|
||
function capitalize(string) { | ||
const updatedString = string.charAt(0).toUpperCase() + string.slice(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. like! |
||
return updatedString; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice dom modeling!