Skip to content
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

Add Core Functionality With Some Extras #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,34 @@
<meta charset="UTF-8">
<title>Flatiron Task Lister</title>
<link rel="stylesheet" href="./style.css">
<script defer src="./src/index.js"></script>

Choose a reason for hiding this comment

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

What does the defer attribute do?

Copy link
Author

Choose a reason for hiding this comment

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

It defers loading the script to after the DOM has loaded. I like placing the script tag up top.

</head>
<body>
<div id="main-content">
<h1>Task Lister Lite™️</h1>

<form id="create-task-form" action="" method="POST">
<form id="create-task-form" action="#" method="POST">
<label for="new-task-description">Task description:</label>
<input type="text" id="new-task-description" name="new-task-description" placeholder="description">
<input type="submit" value="Create New Task">
<label for="new-task-priority">Priority:</label>
<select name="new-task-priority" id="task-priority">
<option value="" disabled>Please select...</option>
<option value="low" >Low</option>
<option value="medium" >Medium</option>
<option value="high">High</option>
</select>
<input type="submit" class="btn" value="Create New Task">
</form>

<div id="list">
<h2>My Todos:</h2>
<ul id="tasks">
</ul>
<table id="task-table">
</table>
</div>

<script src="./src/index.js"></script>

</div>

</body>
Expand Down
168 changes: 165 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,165 @@
document.addEventListener("DOMContentLoaded", () => {
// your code here
});
const form = document.querySelector('#create-task-form')
const submitButton = document.querySelector("#create-task-form input[type='submit']")

const createNewTask = (e) => {
e.preventDefault()
const taskText = e.target['new-task-description'].value
const taskPriority = e.target['new-task-priority'].value
if(taskText !== '' && taskPriority !== ''){
handleTaskPOST(taskText, taskPriority)
.then(data => {
const editBtn = createEditButton(data.id);
const deleteBtn = createDeleteButton(data.id)
tr = createRow(data.id, taskText, taskPriority, deleteBtn, editBtn)
document.querySelector('#task-table').appendChild(tr)
document.querySelector("#new-task-description").value = ''
document.querySelector("#task-priority").value = ''
})
.catch(error => {
console.log(error)
})
}
}

const createRow = (id, taskText, taskPriority, deleteBtn, editBtn) => {
const tr = document.createElement('tr')
tr.id = id
tr.className = taskPriority
const td1 = document.createElement('td')
td1.textContent = taskText
const td2 = document.createElement('td')
td2.appendChild(deleteBtn)
const td3 = document.createElement('td')
td3.appendChild(editBtn)
tr.appendChild(td1)
tr.appendChild(td2)
tr.appendChild(td3)
return tr
}

const createEditButton = (id) => {
const editBtn = document.createElement('button')
editBtn.style.marginLeft = '20px'
editBtn.textContent = 'Edit'
editBtn.className = 'btn'
editBtn.setAttribute('task-id',id)
editBtn.addEventListener('click', editTask)
return editBtn
}

const createDeleteButton = (id) => {
const btn = document.createElement('button')
btn.style.marginLeft = '20px'
btn.textContent = 'Delete'
btn.className = 'btn'
btn.addEventListener('click', handleTaskDELETE)
btn.setAttribute('task-id',id)
return btn
}

const handleTaskPOST = (taskText, taskPriority) => {
return fetch('http://localhost:3000/tasks',{
method: "POST",
headers: {'Content-Type' : 'application/json'},
body: JSON.stringify({task: taskText, priority: taskPriority})
})
.then(res => res.json())
.then(data => data)
.catch(error => {
alert('Ooops, something went wrong when trying to write data to the server')
console.log(error)
})
}

const handleTaskDELETE = (e) => {
const id = e.target.attributes['task-id'].value
const task = e.target.closest('tr').firstChild.textContent
fetch(`http://localhost:3000/tasks/${id}`,{
method: 'DELETE',
})
.then(res => res.json())
.then(data => {
e.target.closest('tr').remove()
})
.catch(error => {
alert(`Ooops, something went wrong when trying to delete task: ${task}`)
console.log(error)
})
}

const handleTaskPATCH = (e) => {
e.preventDefault()
const taskText = e.target['new-task-description'].value
const taskPriority = e.target['new-task-priority'].value
id = submitButton.attributes['task-id'].value
fetch(`http://localhost:3000/tasks/${id}`,{
method: "PATCH",
headers: {
'Content-Type' : 'application/json'
},
body: JSON.stringify({
task: taskText,
priority: taskPriority
})
})
.then(res => res.json())
.then(data => {
resetAfterPATCH();
})
.catch(error => {
alert(`Something went wrong while trying to update task: ${taskText}`)
console.log(error)
})
}

const resetAfterPATCH = () => {
document.querySelector("#new-task-description").value = ''
document.querySelector("#task-priority").value = ''
submitButton.className = 'btn'
submitButton.removeAttribute('task-id')
submitButton.value = 'Create New Task'
form.removeEventListener('submit', handleTaskPATCH)
form.addEventListener('submit',createNewTask)
document.querySelectorAll('tr').forEach(tr => {
tr.remove()
})
initApp();
}

const editTask = (e) => {
const task = e.target.closest('tr').firstChild.textContent
const priority = e.target.closest('tr').className
const id = e.target.attributes['task-id'].value
document.querySelector("#new-task-description").value = task
document.querySelector("#task-priority").value = priority
submitButton.className = 'editBtn'
submitButton.value = 'Edit Task'
submitButton.setAttribute('task-id',id)
form.removeEventListener('submit', createNewTask)
form.addEventListener('submit',handleTaskPATCH)
}

const renderTasks = (body) => {
document.querySelector("#new-task-description").value = ''
document.querySelector("#task-priority").value = ''
document.querySelectorAll('tr').forEach(tr => console.log(tr))
body.forEach(item => {
const editBtn = createEditButton(item.id)
const deleteBtn = createDeleteButton(item.id)
tr = createRow(item.id, item.task, item.priority, deleteBtn, editBtn)
document.querySelector('#task-table').appendChild(tr)
})
}

const initApp = () => {
fetch('http://localhost:3000/tasks')
.then(res => res.json())
.then(body => renderTasks(body))
.catch(error => {
alert('Ooops, looks like something went wrong when trying to retrieve tasks from db.json')
console.log(error)
})
}

form.addEventListener('submit', createNewTask)
document.addEventListener('DOMContentLoaded', initApp)
97 changes: 83 additions & 14 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,80 @@ html {
body {
min-height: 100%;
min-width: 100%;
background: linear-gradient(pink, violet);
background: linear-gradient(#f7d9d9, #f1c0c0);
display: flex;
justify-content: center;
align-items: center;
margin: 0;
padding: 0;
}

select {
font-size: 16px;
padding: 8px;
border-radius: 4px;
border: 1px solid #ccc;
/* background-color: #fff; */
color: #333;
}

.high {
font-size: 16px;
padding: 8px;
color: #f90404;
}

.medium {
font-size: 16px;
padding: 8px;
color: #eeb60e;
}

.low {
font-size: 16px;
padding: 8px;
color: #01b82f;
}

/* Basic button styles */
.btn {
background-color: #007BFF; /* Blue background */
border: none; /* Remove default border */
color: white; /* White text */
padding: 10px 20px; /* Padding for size */
text-align: center; /* Center text */
text-decoration: none; /* Remove underline */
display: inline-block; /* Inline-block for layout */
font-size: 16px; /* Font size */
margin: 4px 2px; /* Margin for spacing */
cursor: pointer; /* Pointer cursor on hover */
border-radius: 25px; /* Rounded corners */
transition: background-color 0.3s ease; /* Smooth transition for hover */
}

.editBtn {
background-color: #fb7a02; /* Blue background */
border: none; /* Remove default border */
color: white; /* White text */
padding: 10px 20px; /* Padding for size */
text-align: center; /* Center text */
text-decoration: none; /* Remove underline */
display: inline-block; /* Inline-block for layout */
font-size: 16px; /* Font size */
margin: 4px 2px; /* Margin for spacing */
cursor: pointer; /* Pointer cursor on hover */
border-radius: 25px; /* Rounded corners */
transition: background-color 0.3s ease; /* Smooth transition for hover */
}

/* Hover state */
.editBtn:hover {
background-color: #be6601; /* Darker blue on hover */
}

button {
font-size: 7px;
/* Hover state */
.btn:hover {
background-color: #0056b3; /* Darker blue on hover */
}

#main-content {
Expand All @@ -20,18 +89,18 @@ button {
justify-content: center;
align-items: center;
flex-direction: column;
border: 1px solid #333;
background: rgba(255, 255, 255, 0.3);
padding: 15px 20px 18px 18px;
border-radius: 5px;
box-shadow: 2px 5px 5px #333;
border: 1px solid #ccc;
background: rgba(255, 255, 255, 0.9);
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

#list {
margin: 10px;
border: 1px solid #333;
background: rgba(255, 255, 255, 0.75);
padding: 15px 20px 18px 18px;
border-radius: 5px;
box-shadow: 2px 5px 5px #333;
margin: 10px 0;
border: 1px solid #ccc;
background: rgba(255, 255, 255, 0.9);
padding: 15px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}