import React, { useState, useEffect } from "react"; const Posts = (props) => { const [state, setState] = useState({ user: { username: "" }, posts: [ { id: 1, upvotes: 2, text: "Action for Climate Change", comments: 0, topics: ["climate change", "air pollution"], }, ], }); const requestOptions = { method: "GET", headers: { Authorization: `Bearer ${localStorage.getItem("token")}`, }, }; const callAPI = () => { fetch(`${process.env.REACT_APP_API_ENDPOINT}/posts/`, requestOptions) .then((response) => response.json()) .then((data) => { if (data !== undefined) { setState(data); } }); }; useEffect(() => { callAPI(); }, []); const maybePluralize = (count, noun, suffix = "s") => `${count} ${noun}${count !== 1 ? suffix : ""}`; return (

Posts

Create Post


Hello {state.user.username}!

{state.posts.map((post, el) => { let background; if (el % 2 == 0) { background = "#F1EAE8"; } else { background = "#FFFFFF"; } return (
{post.text}

+{post.upvotes}

{maybePluralize(post.comments, "comment")}

{post.topics.map((topic) => { return (

{topic}

); })}
); })}
); }; export default Posts;