girlcodeakl / girlcode2018-term2

Girl Code project
0 stars 0 forks source link

Instead of a feed, show just one post at a time (like Tinder) #27

Closed mgatland closed 6 years ago

mgatland commented 6 years ago

We're going to make a new page on our site that shows one post at a time, instead of the whole feed.

(We'll keep original feed as well, because seeing all the posts at once is useful for testing. We might remove it from the final site.)

Feed2 now asks the server for a random post.

Our server doesn't know how to answer that request yet but we are going to show it how. That means moving to index.js

In index.js, add this code, borrowed from our TV Show Idea generator back in week 1.

You can add this anywhere, as long as it's not inside another function:

//pick and return a random element from the given list
function pickRandomFrom(list) {
  return list[Math.floor(Math.random()*list.length)];
};

And then this code sets up the response to the request for "/random":

//give the client a random post
function getRandomPost(request, response) {
  let randomPost = pickRandomFrom(posts);
  let list = [randomPost]; //we put it inside a list, just because it makes our existing feed code work
  response.send(list);
}

app.get('/random', getRandomPost);

You should get a randomly selected post each time you refresh the page.

'Next' button

Can you add a link on the page that makes a new post appear?

Does it work? Is there a weird side effect? Can you figure out why it's happening and how you might fix it?

mgatland commented 6 years ago

$(".message-list").empty();

nextpost();

natalievillard commented 6 years ago

we did it!