Compare commits

...

10 Commits

@ -54,16 +54,16 @@ class StoryList {
// instance method? // instance method?
// query the /stories endpoint (no auth required) // query the /stories endpoint (no auth required)
const response = await axios({ const response = axios({
url: `${BASE_URL}/stories`, url: `${BASE_URL}/stories`,
method: "GET", method: "GET",
}); });
// turn plain old story objects from API into instances of Story class // turn plain old story objects from API into instances of Story class
const stories = response.data.stories.map(story => new Story(story)); const stories = response.data.stories.map(story => new Story(story));
console.log("stories instances here: ", stories);
// build an instance of our own class using the new array of stories // build an instance of our own class using the new array of stories
return new StoryList(stories); return new StoryList(response);
} }
/** Adds story data to API, makes a Story instance, adds it to story list. /** Adds story data to API, makes a Story instance, adds it to story list.
@ -74,18 +74,14 @@ class StoryList {
*/ */
async addStory(user, newStory) { async addStory(user, newStory) {
console.log("addStory called");
console.log("user.loginToken = ", user.loginToken);
const response = await axios.post( const response = await axios.post(
`${BASE_URL}/stories`, `${BASE_URL}/stories`,
{ token: user.logintoken, story: newStory }
{ token: user.loginToken, story: newStory }
); );
console.log("the response is : ", response);
return new Story(response.data.story);
const story = new Story(response.data.story);
stories.unshift(story);
return story;
} }
} }
@ -104,7 +100,7 @@ class User {
username, username,
name, name,
createdAt, createdAt,
favorites = [], favorites,
ownStories = [] ownStories = []
}, },
token) { token) {
@ -175,6 +171,52 @@ class User {
); );
} }
/** first method - take a story instance and insert into our array
* of favorites for a partiuclar user
*/
async insertStoryIntoFavorites(story) {
const response = await axios({
url: `${BASE_URL}/users/${currentUser.username}/favorites/${story.storyId}`,
method: "POST",
data: { token: `${currentUser.loginToken}` }
});
return currentUser.favorites.push(story);
}
/** second method - take a story instance and remove it from our array of
* favorited articles
*/
async removedStoryFromFavorites(story) {
const response = await axios({
url: `${BASE_URL}/users/${currentUser.username}/favorites/${story.storyId}`,
method: "DELETE",
data: { token: `${currentUser.loginToken}` }
});
currentUser.favorites = response.data.user.favorites;
console.log("response.data.user.favorites = ", response.data.user.favorites);
console.log("CU Faves ", currentUser.favorites);
// return currentUser;
}
/*****/
/** Stopping point of sprint. instertStoryIntoFavorites and removedStoryFromFavorites
* are working functions.
* Building out UI for these fucntions is next step.
*/
/*****/
/** When we already have credentials (token & username) for a user, /** When we already have credentials (token & username) for a user,
* we can log them in automatically. This function does that. * we can log them in automatically. This function does that.
*/ */

@ -34,7 +34,7 @@ function updateNavOnLogin() {
$(".main-nav-links").show(); $(".main-nav-links").show();
$navLogin.hide(); $navLogin.hide();
$navLogOut.show(); $navLogOut.show();
$navUserProfile.text(`${currentUser.username}`).show(); $navUserProfile.text(`${currentUser.user}`).show();
} }
/** Show story submission form on click on "submit" */ /** Show story submission form on click on "submit" */

@ -52,16 +52,12 @@ function putStoriesOnPage() {
} }
/** handles story form submission: takes in values from story submission /** handles story form submission: extracts values from story submission
* form, calls addStory, generates story markup to append to page * form, calls addStory, generates story markup to append to page
*/ */
async function getNewStoryAndSubmit(evt) { async function getNewStoryAndSubmit(evt) {
console.debug("getNewStoryAndSubmit", evt);
// clear page then display form
evt.preventDefault(); evt.preventDefault();
hidePageComponents();
$addStoryForm.show();
// get values from form // get values from form
const title = $("#story-title").val(); const title = $("#story-title").val();
@ -78,11 +74,13 @@ async function getNewStoryAndSubmit(evt) {
// instantiate a Story with obj and display on page // instantiate a Story with obj and display on page
const resultOfAddStoryCall = await storyList.addStory(currentUser, newStory); const resultOfAddStoryCall = await storyList.addStory(currentUser, newStory);
console.log("resultOfAddStoryCall: ", resultOfAddStoryCall); console.log("resultOfAddStoryCall: ", resultOfAddStoryCall);
hidePageComponents(); const newStoryMarkup = generateStoryMarkup(resultOfAddStoryCall);
await getAndShowStoriesOnStart(); $allStoriesList.prepend(newStoryMarkup);
//resets form with empty values //resets form with empty values
$addStoryForm.trigger("reset"); $addStoryForm.trigger("reset");
hidePageComponents();
$allStoriesList.show();
} }
//event listener for add story form submit //event listener for add story form submit

@ -35,7 +35,7 @@ async function signup(evt) {
console.debug("signup", evt); console.debug("signup", evt);
evt.preventDefault(); evt.preventDefault();
const name = $("#signup-name").val(); const name = $("#signup-name").text();
const username = $("#signup-username").val(); const username = $("#signup-username").val();
const password = $("#signup-password").val(); const password = $("#signup-password").val();
@ -44,7 +44,7 @@ async function signup(evt) {
currentUser = await User.signup(username, password, name); currentUser = await User.signup(username, password, name);
saveUserCredentialsInLocalStorage(); saveUserCredentialsInLocalStorage();
updateUIOnUserLogin(); updateUiOnUserLogin();
$signupForm.trigger("reset"); $signupForm.trigger("reset");
} }
@ -57,7 +57,6 @@ $signupForm.on("submit", signup);
*/ */
function logout(evt) { function logout(evt) {
console.debug("logout", evt);
localStorage.clear(); localStorage.clear();
location.reload(); location.reload();
} }
@ -73,7 +72,6 @@ $navLogOut.on("click", logout);
*/ */
async function checkForRememberedUser() { async function checkForRememberedUser() {
console.debug("checkForRememberedUser");
const token = localStorage.getItem("token"); const token = localStorage.getItem("token");
const username = localStorage.getItem("username"); const username = localStorage.getItem("username");
if (!token || !username) return false; if (!token || !username) return false;

Loading…
Cancel
Save