|
|
|
@ -54,16 +54,16 @@ class StoryList {
|
|
|
|
|
// instance method?
|
|
|
|
|
|
|
|
|
|
// query the /stories endpoint (no auth required)
|
|
|
|
|
const response = await axios({
|
|
|
|
|
const response = axios({
|
|
|
|
|
url: `${BASE_URL}/stories`,
|
|
|
|
|
method: "GET",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// turn plain old story objects from API into instances of Story class
|
|
|
|
|
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
|
|
|
|
|
return new StoryList(stories);
|
|
|
|
|
return new StoryList(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Adds story data to API, makes a Story instance, adds it to story list.
|
|
|
|
@ -74,18 +74,14 @@ class StoryList {
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async addStory(user, newStory) {
|
|
|
|
|
console.log("addStory called");
|
|
|
|
|
console.log("user.loginToken = ", user.loginToken);
|
|
|
|
|
|
|
|
|
|
const response = await axios.post(
|
|
|
|
|
`${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,
|
|
|
|
|
name,
|
|
|
|
|
createdAt,
|
|
|
|
|
favorites = [],
|
|
|
|
|
favorites,
|
|
|
|
|
ownStories = []
|
|
|
|
|
},
|
|
|
|
|
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,
|
|
|
|
|
* we can log them in automatically. This function does that.
|
|
|
|
|
*/
|
|
|
|
|