-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
245 lines (180 loc) · 7.47 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const loginButton = document.querySelector('#login__btn');
const signupButton = document.querySelector('#signup__btn');
//////////////// LOGIN
const loginModalAppear = () => {
document.querySelector('.login').style.display = 'block';
document.querySelector('#content').style.opacity = 0.6;
// exit modal functionality
const exitLoginButton = document.querySelector('.exit.login');
const exitModal = () => {
document.querySelector('.modal.login').style.display = 'none';
document.querySelector('#content').style.opacity = 1;
};
exitLoginButton.addEventListener('click', exitModal);
}
loginButton.addEventListener('click', loginModalAppear);
//////////////// SIGNUP
const signupModalAppear = () => {
document.querySelector('.signup').style.display = 'block';
document.querySelector('#content').style.opacity = 0.6;
// exit modal functionality
const exitSignupButton = document.querySelector('.exit.signup');
const exitModal = () => {
document.querySelector('.modal.signup').style.display = 'none';
document.querySelector('#content').style.opacity = 1;
signupForm.reset();
};
exitSignupButton.addEventListener('click', exitModal);
}
signupButton.addEventListener('click', signupModalAppear);
//////////////// DISPLAY CURRENT CONTACTS FROM DATABASE (only need to do once to refresh location)
const displayContactBtn = document.querySelector('#display-contacts-btn');
displayContactBtn.addEventListener('click', e => {
// Reload location (to pull contacts from database)
location.reload();
return false;
});
//////////////// ADDING CONTACT TO LIST
const contactForm = document.querySelector('#new-contact-form');
contactForm.addEventListener('submit', event => {
event.preventDefault();
const contactName = document.querySelector('#newContact__input').value;
const contactFrequency = document.querySelector('#newFrequency__input').value;
const ID = auth.currentUser.uid;
let data = {}
if (contactName !== '' && contactFrequency !== '') {
data['name'] = contactName;
data['frequency'] = contactFrequency;
data['id'] = ID;
// Milliseconds > hours
data['created_on'] = Date.now() / 3600000;
db.collection('contacts').doc().set(data);
// Clear input fields
contactForm.reset();
} else {
alert('Please provide both a name and an alert frequency.');
}
// Put the focus back on name input after submit
document.querySelector('#newContact__input').focus();
});
/////////////// CREATE NEW CONTACT AND RENDER ON LIST
const contactList = document.querySelector('.contact-list');
function renderContact(doc) {
let li = document.createElement('li');
let name = document.createElement('span');
let frequency = document.createElement('span');
let div = document.createElement('div');
li.setAttribute('id', doc.id);
name.textContent = doc.data().name;
frequency.textContent = doc.data().frequency;
div.innerHTML = '<i class="fas fa-times cross"></i>';
li.appendChild(name);
li.appendChild(frequency);
li.appendChild(div);
contactList.appendChild(li);
// deleting data
let cross = document.querySelectorAll('.cross');
cross.forEach(cross => {
cross.addEventListener('click', e => {
e.stopPropagation();
let id = e.target.parentElement.parentElement.getAttribute('id');
db.collection('contacts').doc(id).delete();
});
});
};
////////// Render contacts as soon as they're added to the database
db.collection('contacts').onSnapshot(snapshot => {
let changes = snapshot.docChanges();
changes.forEach(change => {
if (change.type === 'added' && change.doc.data().id === auth.currentUser.uid) {
renderContact(change.doc);
} else if (change.type === 'removed') {
let li = document.getElementById(change.doc.id);
contactList.removeChild(li);
}
});
});
//////////// RESET CONTACT LIST ON LOGOUT
const logoutButton = document.querySelector('#logout__btn');
logoutButton.addEventListener('click', event => {
event.preventDefault();
contactList.innerHTML = '';
});
///////////// TIMING LOGIC: IF THE DESIGNATED NUMBER OF HOURS HAS PASSED, EACH LINE ITEM BACKGROUND COLOR WILL CHANGE TO RED
auth.onAuthStateChanged(user => {
if (user) {
// update UI for logged in state
loggedIn.forEach(el => el.style.display = 'block');
loggedOut.forEach(el => el.style.display = 'none');
// app functionality
let userId = auth.currentUser.uid;
let now = Date.now() / 3600000; // Milliseconds > hours
// Query docs in database to pull up only those that match with current user
db.collection('contacts').where('id', '==', userId).get()
.then(snapshot => {
if (snapshot.empty) {
console.log('No matching docs');
}
snapshot.forEach(doc => {
let id = doc.id;
let createdOn = doc.data().created_on;
let frequency = doc.data().frequency;
function timerExpired() { // This function will update the UI each time the timer expires on each list item
document.getElementById(id).classList.add('expired');
document.getElementById(id).childNodes[2].insertAdjacentHTML('beforeend', '<i class="fas fa-check"></i>');
}
// Testing purposes only
// if (now - createdOn > 4 && frequency == 'Daily') {
// timerExpired();
// } else if (now - createdOn > 12 && frequency == 'Twice a Week') {
// timerExpired();
// }
// The timing logic for each frequency option
if (now - createdOn > 24 && frequency == 'Daily') {
timerExpired();
} else if (now - createdOn > 84 && frequency == 'Twice a Week') {
timerExpired();
} else if (now - createdOn > 168 && frequency == 'Weekly') {
timerExpired();
} else if (now - createdOn > 336 && frequency == 'Every Two Weeks') {
timerExpired();
} else if (now - createdOn > 720 && frequency == 'Monthly') {
timerExpired();
}
});
/////// Resetting the interval when the checkmark is clicked by updating the created_on property in the database
let checkmarks = document.querySelectorAll('.fa-check');
checkmarks.forEach(check => {
check.addEventListener('click', e => {
e.stopPropagation();
let itemID = e.target.parentNode.parentNode.id;
// Need parseInt - otherwise will be updated in database as a string instead of a number
let now = parseInt(Date.now() / 3600000);
// Update the created_on property in Firestore so that the timing interval starts over
db.collection('contacts').doc(`${itemID}`).update({ created_on: now });
// Update the list/UI
document.getElementById(`${itemID}`).classList.remove('expired');
e.target.style.display = 'none';
})
});
});
} else {
// update UI for logged out state
loggedIn.forEach(el => el.style.display = 'none');
loggedOut.forEach(el => el.style.display = 'block');
}
})
//////// EVENT LISTENER TO DISPLAY FREQUENCY (LIST ITEM MIDDLE SECTION) ON LARGER SCREENS AND TO HIDE IT ON SMALLER SCREENS
window.addEventListener('resize', e => {
let items = document.querySelector('.contact-list').childNodes;
let itemsArr = Array.from(items);
if (window.innerWidth < 505) {
itemsArr.forEach(item => {
item.childNodes[1].style.display = 'none';
})
} else if (window.innerWidth >= 505) {
itemsArr.forEach(item => {
item.childNodes[1].style.display = 'block';
})
}
})