-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
219 lines (188 loc) · 9.32 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
document.addEventListener('DOMContentLoaded', function() {
// define elements from the DOM
var accountButton = document.getElementById('accountButton');
var userInfo = document.getElementById('userInfo');
var userInitial = document.getElementById('userInitial');
var userName = document.getElementById('userName');
var registrationModal = document.getElementById('registrationModal');
var loginModal = document.getElementById('loginModal');
// check if user is logged in
var isLoggedIn = function() {
return localStorage.getItem('user') !== null;
};
// update the UI based on login status
if (isLoggedIn()) {
showUserInfo();
} else {
showAccountButton();
}
// account button click
accountButton.addEventListener('click', function() {
registrationModal.style.display = 'block'; // show modal
registrationModal.style.opacity = '0';
setTimeout(() => {
registrationModal.style.transition = 'opacity 0.3s ease-in'; // smooth modal transition (fade in)
registrationModal.style.opacity = '1';
}, 10);
});
// close modals
window.closeModal = function(modalId) {
var modal = document.getElementById(modalId);
modal.style.transition = 'opacity 0.3s ease-out'; // smooth modal transition (fade out)
modal.style.opacity = '0';
setTimeout(() => {
modal.style.display = 'none';
}, 300);
};
// close modal when clicking outside of the modal content (foreground)
window.addEventListener('click', function(event) {
var registrationModal = document.getElementById('registrationModal');
var loginModal = document.getElementById('loginModal');
if (event.target === registrationModal) {
closeModal('registrationModal');
}
if (event.target === loginModal) {
closeModal('loginModal');
}
});
// registration form
document.getElementById('registrationForm').addEventListener('submit', function(event) {
event.preventDefault(); // no reload
// get email, username, password
var regEmail = document.getElementById('regEmail').value;
var regUsername = document.getElementById('regUsername').value;
var regPassword = document.getElementById('regPassword').value;
// save user to local storage (insecure, check the 'Reflection' section of the google doc for more info)
localStorage.setItem('user', JSON.stringify({ email: regEmail, username: regUsername, password: regPassword }));
registrationModal.style.display = 'none'; // close
loginModal.style.display = 'block'; // open login
});
// login form
document.getElementById('loginForm').addEventListener('submit', function(event) {
event.preventDefault(); // no reload
// user from localstorage (insecure, check the 'Reflection' section of the google doc for more info)
var storedUser = JSON.parse(localStorage.getItem('user'));
// check credentials in localstorage
if (storedUser && storedUser.email === document.getElementById('loginEmail').value && storedUser.password === document.getElementById('loginPassword').value) {
var loginModal = document.getElementById('loginModal');
loginModal.style.transition = 'opacity 0.3s ease-out'; // smooth modal transition (fade out) on successful login
loginModal.style.opacity = '0';
setTimeout(() => {
closeModal('loginModal');
showUserInfo();
updateSampleReview();
loadReviews(); // load the reviews after login
}, 300);
} else {
alert('Invalid email or password.'); // TODO: make an actual popup rather than an alert box
}
});
// check notifications (placeholder, no real functionality for now lol)
document.querySelector('.notification').addEventListener('click', function() {
alert("You're all caught up!");
});
// update sample review based on login status
function updateSampleReview() {
var sampleReviewName = document.getElementById('sampleReviewName');
var sampleReviewAvatar = document.getElementById('sampleReviewAvatar');
var editReviewButton = document.getElementById('editReview');
if (isLoggedIn()) {
var user = JSON.parse(localStorage.getItem('user'));
sampleReviewName.textContent = user.username;
sampleReviewAvatar.textContent = user.username.charAt(0).toUpperCase();
editReviewButton.style.display = 'block';
} else {
sampleReviewName.textContent = "Sample User";
sampleReviewAvatar.textContent = "S";
editReviewButton.style.display = 'none';
}
}
// edit the sample review
document.getElementById('editReview').addEventListener('click', function() {
var sampleReviewText = document.getElementById('sampleReviewText');
var currentText = sampleReviewText.textContent; // get the current text
var editTextarea = document.createElement('textarea'); // create a textarea element (TODO: Style this to make it dark)
editTextarea.value = currentText;
editTextarea.style.width = "100%";
sampleReviewText.parentNode.replaceChild(editTextarea, sampleReviewText); // replace the sample review text with the textarea (edit)
this.textContent = 'Save';
this.addEventListener('click', function saveHandler() { // add a click event listener to save the edited text
var newText = editTextarea.value;
sampleReviewText.textContent = newText;
editTextarea.parentNode.replaceChild(sampleReviewText, editTextarea);
this.textContent = 'Edit';
if (isLoggedIn()) {
var user = JSON.parse(localStorage.getItem('user'));
user.sampleReview = newText;
localStorage.setItem('user', JSON.stringify(user));
}
this.removeEventListener('click', saveHandler);
});
});
// function to add a new review element
function addReview(title, content, user) {
var newReview = document.createElement('div');
newReview.classList.add('review-item');
newReview.innerHTML = `
<div class="reviewer">
<span class="avatar">${user.username.charAt(0).toUpperCase()}</span>
<span class="name">${user.username}</span>
</div>
<div class="rating">★★★★★ 5.0</div>
<p class="review-text">${content}</p>
`;
document.getElementById('reviewList').appendChild(newReview);
}
// save reviews to localStorage (insecure, check the 'Reflection' section of the google doc for more info)
function saveReviews() {
var reviewItems = document.getElementById('reviewList').querySelectorAll('.review-item:not(#sampleReview)'); // exclude sample review in the html
var reviews = Array.from(reviewItems).map(function(reviewItem) { // map each review item to an object
var reviewerName = reviewItem.querySelector('.name').textContent;
var reviewContent = reviewItem.querySelector('.review-text').textContent;
return { user: { username: reviewerName }, content: reviewContent };
});
localStorage.setItem('reviews', JSON.stringify(reviews)); // save to localStorage
}
// Load reviews from localStorage
function loadReviews() {
var reviews = JSON.parse(localStorage.getItem('reviews'));
if (reviews) {
document.getElementById('reviewList').querySelectorAll('.review-item:not(#sampleReview)').forEach(el => el.remove()); // remove
reviews.forEach(function(review) {
addReview('', review.content, review.user); // add each review
});
}
}
// post review button
document.getElementById('postReview').addEventListener('click', function() {
var content = document.getElementById('reviewContent').value; // get review content
var user = JSON.parse(localStorage.getItem('user')) || { username: 'Sample User' }; // default to sample user
if (content.trim() !== '') { // check if content is not empty
addReview('', content, user);
document.getElementById('reviewContent').value = ''; // clear content
saveReviews();
}
});
// show user info
function showUserInfo() {
var userData = JSON.parse(localStorage.getItem('user')); // get user data
userInitial.textContent = userData.username.charAt(0).toUpperCase(); // set user initial
userName.textContent = userData.username; // set user name
accountButton.style.display = 'none'; // hide account button
userInfo.style.display = 'flex';
updateSampleReview();
}
// show account button
function showAccountButton() {
accountButton.style.display = 'block';
userInfo.style.display = 'none';
}
// load reviews on page load
loadReviews();
});
// log out when dropdown icon is clicked
document.querySelector('.dropdown-icon').addEventListener('click', function() {
localStorage.removeItem('user');
alert('You have logged out.');
window.location.reload(); // reload after logout
});