-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
79 lines (66 loc) · 2.53 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const checkBoxList = document.querySelectorAll('.custom-checkbox')
const inputFields = document.querySelectorAll('.goal-input')
const errorLabel = document.querySelector('.error-label')
const progressLabel = document.querySelector('.progress-label')
const progressBar = document.querySelector('.progress-bar')
const progressValue = document.querySelector('.progress-value')
const allQuotes = [
'Raise the bar by completing your goals!',
'Well begun is half done!',
'Just a step away, keep going!',
'Whoa! You just completed all the goals, time to chill :D',
]
const allGoals = JSON.parse(localStorage.getItem('allGoals')) || {}
let completedGoalsCount = Object.values(allGoals).filter(
(goal) => goal.completed
).length
progressValue.style.width = `${(completedGoalsCount / inputFields.length) * 100}%`
progressValue.firstElementChild.innerText = `${completedGoalsCount}/${inputFields.length} completed`
progressLabel.innerText = allQuotes[completedGoalsCount]
checkBoxList.forEach((checkbox) => {
checkbox.addEventListener('click', (e) => {
const allGoalsAdded = [...inputFields].every(function (input) {
return input.value
})
if (allGoalsAdded) {
checkbox.parentElement.classList.toggle('completed')
const inputId = checkbox.nextElementSibling.id
allGoals[inputId].completed = !allGoals[inputId].completed
completedGoalsCount = Object.values(allGoals).filter(
(goal) => goal.completed
).length
progressValue.style.width = `${(completedGoalsCount / inputFields.length) * 100}%`
progressValue.firstElementChild.innerText = `${completedGoalsCount}/${inputFields.length} completed`
progressLabel.innerText = allQuotes[completedGoalsCount]
localStorage.setItem('allGoals', JSON.stringify(allGoals))
} else {
progressBar.classList.add('show-error')
}
})
})
inputFields.forEach((input) => {
if (allGoals[input.id]) {
input.value = allGoals[input.id].name
if (allGoals[input.id].completed) {
input.parentElement.classList.add('completed')
}
}
input.addEventListener('focus', () => {
progressBar.classList.remove('show-error')
})
input.addEventListener('input', (e) => {
if (allGoals[input.id] && allGoals[input.id].completed) {
input.value = allGoals[input.id].name
return
}
if (allGoals[input.id]) {
allGoals[input.id].name = input.value
} else {
allGoals[input.id] = {
name: input.value,
completed: false,
}
}
localStorage.setItem('allGoals', JSON.stringify(allGoals))
})
})