-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
126 lines (104 loc) · 3.78 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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 calendar = document.querySelectorAll('.calendar p');
let getWeekDays = document.querySelector('.weekDays');
let CurrentDate = document.querySelector('.mainDay');
function needWeekDays(){
var days = [
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
];
let currentDate = new Date();
let dayOfWeek = days[currentDate.getDay()];
return dayOfWeek;
}
getWeekDays.innerHTML = needWeekDays();
function getCurrDate (){
let months = [
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
];
let currDate = new Date();
let day = currDate.getDate();
let monthIndex = currDate.getMonth();
let year = currDate.getFullYear();
let monthName = months[monthIndex];
return day + " " + monthName + " " + year;
}
CurrentDate.innerHTML = getCurrDate();
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 for chill :D',
]
// const allGoals = JSON.parse(localStorage.getItem('allGoals')) || {
// first: {
// name: '',
// completed: false,
// },
// second: {
// name: '',
// completed: false,
// },
// third: {
// name: '',
// completed: false,
// },
// }
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))
})
});