-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.js
287 lines (238 loc) · 7.88 KB
/
ui.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
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
$(async function() {
// cache some selectors we'll be using quite a bit
const $allStoriesList = $("#all-articles-list");
const $submitForm = $("#submit-form");
const $filteredArticles = $("#filtered-articles");
const $loginForm = $("#login-form");
const $createAccountForm = $("#create-account-form");
const $ownStories = $("#my-articles");
const $navLogin = $("#nav-login");
const $navLogOut = $("#nav-logout");
const $navWelcome = $("#nav-welcome");
const $navSubmit = $("#nav-submit");
// global storyList variable
let storyList = null;
// global currentUser variable
let currentUser = null;
await checkIfLoggedIn();
/**
* Event listener for logging in.
* If successfully we will setup the user instance
*/
$loginForm.on("submit", async function(evt) {
evt.preventDefault(); // no page-refresh on submit
// grab the username and password
const username = $("#login-username").val();
const password = $("#login-password").val();
// call the login static method to build a user instance
const userInstance = await User.login(username, password);
// set the global user to the user instance
currentUser = userInstance;
syncCurrentUserToLocalStorage();
loginAndSubmitForm();
});
/**
* Event listener for signing up.
* If successfully we will setup a new user instance
*/
$createAccountForm.on("submit", async function(evt) {
evt.preventDefault(); // no page refresh
// grab the required fields
let name = $("#create-account-name").val();
let username = $("#create-account-username").val();
let password = $("#create-account-password").val();
// call the create method, which calls the API and then builds a new user instance
const newUser = await User.create(username, password, name);
currentUser = newUser;
syncCurrentUserToLocalStorage();
loginAndSubmitForm();
});
/**
* Log Out Functionality
*/
$navLogOut.on("click", function() {
// empty out local storage
localStorage.clear();
// refresh the page, clearing memory
location.reload();
});
/**
* Event Handler for Clicking Login
*/
$navLogin.on("click", function() {
// Show the Login and Create Account Forms
$loginForm.slideToggle();
$createAccountForm.slideToggle();
$allStoriesList.toggle();
});
/**
* Event handler for Navigation to Homepage
*/
$("body").on("click", "#nav-all", async function() {
hideElements();
await generateStories();
$allStoriesList.show();
});
/**
* Event handler for clicking Submit nav button
*/
$navSubmit.on("click", function(){
$submitForm.slideToggle();
});
/**
* Event handler for submitting story form
*/
$submitForm.on("submit", async function(evt){
//story object to be passed to StoryList.addStory
//Grabs submit form values
let story = {
author: $("#author").val(),
title: $("#title").val(),
url: $("#url").val()
};
//POST request to StoryList
let storySubmit = await StoryList.addStory(currentUser, story);
//Update HTML on top of the list
$("#all-articles-list").prepend(generateStoryHTML(storySubmit));
});
/**
* On page load, checks local storage to see if the user is already logged in.
* Renders page information accordingly.
*/
async function checkIfLoggedIn() {
// let's see if we're logged in
const token = localStorage.getItem("token");
const username = localStorage.getItem("username");
// if there is a token in localStorage, call User.getLoggedInUser
// to get an instance of User with the right details
// this is designed to run once, on page load
currentUser = await User.getLoggedInUser(token, username);
await generateStories();
if (currentUser) {
showNavForLoggedInUser();
}
}
/**
* A rendering function to run to reset the forms and hide the login info
*/
function loginAndSubmitForm() {
// hide the forms for logging in and signing up
$loginForm.hide();
$createAccountForm.hide();
// reset those forms
$loginForm.trigger("reset");
$createAccountForm.trigger("reset");
// show the stories
$allStoriesList.show();
// update the navigation bar
showNavForLoggedInUser();
}
/**
* A rendering function to call the StoryList.getStories static method,
* which will generate a storyListInstance. Then render it.
*/
async function generateStories() {
// get an instance of StoryList
const storyListInstance = await StoryList.getStories();
// update our global variable
storyList = storyListInstance;
// empty out that part of the page
$allStoriesList.empty();
// loop through all of our stories and generate HTML for them
for (let story of storyList.stories) {
const result = generateStoryHTML(story);
$allStoriesList.append(result);
}
}
/**
* A function to render HTML for an individual Story instance
*/
function generateStoryHTML(story) {
let hostName = getHostName(story.url);
let starClass = checkForFavorites(story) ? "fas" : "far";
// render story markup
const storyMarkup = $(`
<li id="${story.storyId}">
<span class="star"><i class="${starClass} fa-star"></i></span>
<a class="article-link" href="${story.url}" target="a_blank">
<strong>${story.title}</strong>
</a>
<small class="article-author">by ${story.author}</small>
<small class="article-hostname ${hostName}">(${hostName})</small>
<small class="article-username">posted by ${story.username}</small>
</li>
`);
return storyMarkup;
}
/* hide all elements in elementsArr */
function hideElements() {
const elementsArr = [
$submitForm,
$allStoriesList,
$filteredArticles,
$ownStories,
$loginForm,
$createAccountForm
];
elementsArr.forEach($elem => $elem.hide());
}
function showNavForLoggedInUser() {
$navLogin.hide();
$navLogOut.show();
$navWelcome.show();
}
/* simple function to pull the hostname from a URL */
function getHostName(url) {
let hostName;
if (url.indexOf("://") > -1) {
hostName = url.split("/")[2];
} else {
hostName = url.split("/")[0];
}
if (hostName.slice(0, 4) === "www.") {
hostName = hostName.slice(4);
}
return hostName;
}
/* sync current user information to localStorage */
function syncCurrentUserToLocalStorage() {
if (currentUser) {
localStorage.setItem("token", currentUser.loginToken);
localStorage.setItem("username", currentUser.username);
}
}
$("body").on("click", ".fa-star", async function(){
//toggle star color
$(this).toggleClass("fas");
let selectedStoryID = $(this).parent().parent().attr("id");
//If star has class "fas", add to user favorites array
if ($(this).hasClass("fas")){
await User.addToFavorites(currentUser.username,selectedStoryID);
}
});
$("#nav-favorites").on("click", function(){
$("#all-articles-list").hide();
$("#favorited-articles").show();
showFavorites();
})
async function showFavorites(){
let favoriteStories = await User.getUserFavoriteStories(currentUser.username);
$("#favorited-articles").empty();
for(let i = 0; i < favoriteStories.length; i++) {
let storyHTML = await generateStoryHTML(favoriteStories[i]);
$("#favorited-articles").append(storyHTML);
}
$('.fa-star').addClass('fas');
}
// Loop through user's favorite stories and check if their ID's
// match current stories being generated, then add 'fas' class
function checkForFavorites(story){
for (let i = 0; i < currentUser.favorites.length; i++){
if(story.storyId === currentUser.favorites[i].storyId){
//console.log("checkForFavorites true", story.title, story.storyId === currentUser.favorites[i].storyId);
return true;
}
}
return false;
}
});