-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptJournal.js
More file actions
317 lines (268 loc) · 11 KB
/
Copy pathscriptJournal.js
File metadata and controls
317 lines (268 loc) · 11 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//Note: need to change all the index.html
const enterButton = document.querySelector('#createJournal');
const clearButton = document.querySelector('#journalClear');
let prevJournalEntry = document.querySelector('#journalEntries');
let input = document.querySelector('#journaltext');
//Login
let loginButton = document.querySelector("#loginButton");
let popUp = document.querySelector("#login");
//To Close Modal
let closeButton = document.querySelector("#close");
//Text that Shows What User is Logged In
let userText = document.querySelector("#listedUser");
//Modal Pops Up When You Press Log In
loginButton.addEventListener("click", () => {
popUp.style.display = "block";
});
//Modal Closes When You Click Close
closeButton.addEventListener("click", () => {
popUp.style.display = "none";
});
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == popUp) {
popUp.style.display = "none";
}
};
//Show Current User
if ((JSON.parse(localStorage.getItem('currentUser'))!=undefined) && (JSON.parse(localStorage.getItem('currentUser'))!=null)){
userText.innerHTML = `<p class = "subtitle is-size-5 has-text-left"> Current User: ${JSON.parse(localStorage.getItem('currentUser')).name}</p><button class="logOutBtn" id="LogOut" type="submit" onclick="LogOut()">Log Out</button>`;
} else {
userText.innerHTML = `<p class = "subtitle is-size-5 has-text-left"> Nobody is Logged In Currently </p>`;
};
//If window is in the journal page
if (window.location.href == "https://google-cssi-final-project-matter-of-mind.sabrinado.repl.co/journal.html") {
//If somebody is logged in, set up the account (meaning log the previous entries)
if (JSON.parse(localStorage.getItem('validLogin')) == true) {
setUpAccount();
};
//Basically resets and logs past journal entries
function setUpAccount() {
//Randomizes Color
let color = randomColor();
//Initializes to find correct current user info
var currentUserInfoString = localStorage.getItem('all_users');
var currentUserInfo = JSON.parse(currentUserInfoString);
var currentUser = JSON.parse(localStorage.getItem('currentUser')).name;
let index = 0;
for (var i = 0; i<currentUserInfo.length; i++){
if (currentUserInfo[i].name == currentUser){
index = i;
break;
};
};
prevJournalEntry.innerHTML = "";
currentUserJournal = currentUserInfo[index].journal;
//Circles through all journal entries of the current users, grabs date and entry values and adds them to the html
for (var j = 0; j< currentUserJournal.length; j++) {
dateValue = currentUserJournal[j].date;
entryValue = currentUserJournal[j].entry;
if (j == 0){
prevJournalEntry.innerHTML = `<div class = "entryOuter">
<div class = "entry" style="background-color: ${color};">
<h3>${dateValue}</h3>
<p>${entryValue}</p>
</div>
</div>`
} else {
prevJournalEntry.innerHTML += `<div class = "entryOuter">
<div class = "entry" style="background-color: ${color};">
<h3>${dateValue}</h3>
<p>${entryValue}</p>
</div>
</div>`
};
};
};
//Clear Button Functionality
clearButton.addEventListener('click', () => {
input.value = "";
});
//Count for those who are not logged in
let nonLoggedCount = 0;
//Enter Journal entry
enterButton.addEventListener("click", () => {
//Current Date Information
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
var journalDate = today;
let color = randomColor(); //Randomize Color
let index = 0;
if (input.value!="") { //Cannot submit entry unless entry has string text
if ((JSON.parse(localStorage.getItem('validLogin')) == true)) { //If user is logged in
//Get current user info
var currentUserInfoString = localStorage.getItem('all_users');
var currentUserInfo = JSON.parse(currentUserInfoString);
var currentUser = JSON.parse(localStorage.getItem('currentUser')).name;
for (let i = 0; i<currentUserInfo.length; i++) {
if (currentUserInfo[i].name === currentUser) {
index = i;
break;
};
};
if (currentUserInfo[index].journal.length == 0) {
prevJournalEntry.innerHTML = `<div class = "entryOuter">
<div class = "entry" style="background-color: ${color};">
<h3>${today}</h3>
<p>${input.value}</p>
</div>
</div>`;
currentUserInfo[index].count++;
} else {
prevJournalEntry.innerHTML += `<div class = "entryOuter">
<div class = "entry" style="background-color: ${color};">
<h3>${today}</h3>
<p>${input.value}</p>
</div>
</div>`;
currentUserInfo[index].count++;
};
currentUserInfo[index].journal.push({date: today, entry: input.value});
localStorage.setItem('all_users', JSON.stringify(currentUserInfo));
input.value = "";
} else {
//for those who do not log in
if(nonLoggedCount==0) {
prevJournalEntry.innerHTML = `<div class = "entryOuter">
<div class = "entry" style="background-color: ${color};">
<h3>${today}</h3>
<p>${input.value}</p>
</div>
</div>`;
input.value = "";
nonLoggedCount++;
} else {
prevJournalEntry.innerHTML += `<div class = "entryOuter">
<div class = "entry" style="background-color: ${color};">
<h3>${today}</h3>
<p>${input.value}</p>
</div>
</div>`;
};
};
};
});
};
//Generate Random Color for Sticky Notes
function randomColor() {
var colors = ['#E0BBE4', '#957DAD', '#D291BC', '#FEC8D8', '#FFDFD3'];
var random_color = colors[Math.floor(Math.random() * colors.length)];
return random_color;
};
//When You Login
function Login() {
if ((JSON.parse(localStorage.getItem('all_users')) == null) || (JSON.parse(localStorage.getItem('all_users')) == undefined)){
var a = [
{name: "abc@gmail.com", password: "abc",journal: [], count:0}
];
localStorage.setItem('all_users', JSON.stringify(a));
};
console.log("Login successful");
var currentUserInfoString = localStorage.getItem('all_users');
var currentUserInfo = JSON.parse(currentUserInfoString);
var username = document.querySelector('#uname').value;
var password = document.querySelector('#psw').value;
localStorage.setItem('validLogin', JSON.stringify(false));
//Check if username and password are correct
for (let user of currentUserInfo)
{
if(user.name === username && user.password === password) {
localStorage.setItem('validLogin', JSON.stringify(true));
localStorage.setItem('currentUser', JSON.stringify(user));
popUp.style.display = "none";
userText.innerHTML = `<p class = "subtitle is-size-5 has-text-left"> Current User: ${JSON.parse(localStorage.getItem('currentUser')).name} </p><button class="logOutBtn" id="LogOut" type="submit" onclick="LogOut()">Log Out</button>`;
//Only in Journal Page
if (window.location.href == "https://journalpage.sabrinado.repl.co/index.html") {
setUpAccount();
};
break;
};
};
//If the username is not in the storage, it will return an alert
if (JSON.parse(localStorage.getItem('validLogin')) == false) {
alert("Wrong Username/Password");
};
};
//Create Account
function CreateAccount() {
var username = document.querySelector('#newEmail').value;
var pass = document.querySelector('#newPsw').value;
if ((JSON.parse(localStorage.getItem('all_users')) == null) || (JSON.parse(localStorage.getItem('all_users')) == undefined)){
var a = [
{name: "abc@gmail.com", password: "abc",journal: [], count:0}
];
localStorage.setItem('all_users', JSON.stringify(a));
};
var currentUserInfoString = localStorage.getItem('all_users');
var currentUserInfo = JSON.parse(currentUserInfoString);
currentUserInfo.push({name: username, //Push new object with new information to allUsers
password: pass,
journal: [],
count: 0,
});
localStorage.setItem('all_users', JSON.stringify(currentUserInfo)); //Store in Local Storage
console.log(JSON.parse(localStorage.getItem('all_users'))); //Console
};
//Reset Password
function ResetPassword() {
if ((JSON.parse(localStorage.getItem('all_users')) == null) || (JSON.parse(localStorage.getItem('all_users')) == undefined)){
var a = [
{name: "abc@gmail.com", password: "abc",journal: [], count:0}
];
localStorage.setItem('all_users', JSON.stringify(a));
};
var currentUserInfoString = localStorage.getItem('all_users');
var currentUserInfo = JSON.parse(currentUserInfoString);
var username = document.querySelector('#user').value;
var resetPass = document.querySelector('#newPass').value;
let index = 0;
let isTrue = false;
for (var i = 0; i<currentUserInfo.length; i++) { //Iterate through all current Users
if (currentUserInfo[i].name == username) { //if username is in previous users, return true and log the index
index = i;
isTrue = true;
break;
};
};
//if username is not in previous users, alert
if (isTrue == false) {
alert("Your username does not exist.");
} else {
currentUserInfo[index].password = resetPass;
localStorage.setItem('all_users', JSON.stringify(currentUserInfo));
};
};
//Log Out Function
function LogOut() {
console.log("logging out...");
var currentUserInfoString = localStorage.getItem('all_users');
var currentUserInfo = JSON.parse(currentUserInfoString);
var username = JSON.parse(localStorage.getItem('currentUser')).name;
for (var i = 0; i<currentUserInfo.length; i++) { //Iterate through all current Users
if (currentUserInfo[i].name == username) { //if username is in previous users, return true and log the index
index = i;
isTrue = true;
break;
};
};
localStorage.setItem('validLogin', JSON.stringify(false));
localStorage.setItem('currentUser', JSON.stringify(null));
userText.innerHTML = `<p class = "subtitle is-size-5 has-text-left"> Nobody is Logged In Currently </p>`;
prevJournalEntry.innerHTML = `<p class = "hero is-size-4 has-text-centered "> <br>You have no entries yet! Please submit one.<br> </p>`;
};
/*
async function blackOut(){
page.classList.add("has-background-black");
let myKey = "iCEG4C90b5qOuYiDntZjWtztw0vRR6XC";
let myQuery = `https://api.giphy.com/v1/gifs/search?api_key=${myKey}&q=failure`;
const response = await fetch(myQuery);
const myjson = await response.json();
console.log(myjson);
const random = getRandomInt(myjson.data.length);
const image_url = myjson.data[random].images.downsized.url;
console.log(image_url);
body.innerHTML = `<img src = "${image_url}"/>`;
};*/