Skip to content
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
1 change: 1 addition & 0 deletions 65-advanced-todo-list/before/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<title>Advanced Todo List</title>
<link rel="stylesheet" href="styles.css">
<script src="script.js" defer></script>
</head>
<body>
<ul id="list">
Expand Down
76 changes: 76 additions & 0 deletions 65-advanced-todo-list/before/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const form = document.querySelector('#new-todo-form')
const todoInput = document.querySelector('#todo-input')
const list = document.querySelector('#list')
const template = document.querySelector('#list-item-template')
const LOCAL_STORAGE_PREFIX = 'ADVANCED_TODO_LIST'
const TODOS_STORAGE_KEY = `${LOCAL_STORAGE_PREFIX}-todos`
let todos = loadTodos()
todos.forEach((todo) => renderTodo(todo))

list.addEventListener('change', (e) => {
if (!e.target.matches('[data-list-item-checkbox]')) return

// Get the todo that is clicked on
const parent = e.target.closest('.list-item')
const todoId = parent.dataset.todoId
const todo = todos.find((t) => t.id === todoId)
// Toggle the complete property to be equal to the checkbox value
todo.complete = e.target.checked
// Save our updated todo
saveTodos()
})

list.addEventListener('click', (e) => {
if (!e.target.matches('[data-button-delete]')) return

const parent = e.target.closest('.list-item')
const todoId = parent.dataset.todoId

//Remove todo from screen
parent.remove()
//Remove todo from list
todos = todos.filter((todo) => todo.id !== todoId)
//Save the new todos
saveTodos()
})

form.addEventListener('submit', (e) => {
//Prevent refresh of page when form is submitted
e.preventDefault()

//gets value inside of input
const todoName = todoInput.value
if (todoName === '') return
const newTodo = {
name: todoName,
complete: false,
id: new Date().valueOf().toString(),
}
todos.push(newTodo)
renderTodo(newTodo)
saveTodos()
todoInput.value = ''
})

function renderTodo(todo) {
//Get all content inside template, clone it, and store in variable. 'true' ensures all code is cloned.
const templateClone = template.content.cloneNode(true)
const listItem = templateClone.querySelector('.list-item')
listItem.dataset.todoId = todo.id
const textElement = templateClone.querySelector('[data-list-item-text]')
textElement.innerText = todo.name
const checkbox = templateClone.querySelector('[data-list-item-checkbox]')
checkbox.checked = todo.complete
list.appendChild(templateClone)
}

function saveTodos() {
localStorage.setItem(TODOS_STORAGE_KEY, JSON.stringify(todos))
}

function loadTodos() {
const todosString = localStorage.getItem(TODOS_STORAGE_KEY)
return JSON.parse(todosString) || []
}

//test