In this lab, you'll build a community forum where users can make text-based posts which can then be upvoted or downvoted.
- Clone this repository using
git clone
cd
into the project foldernpm install
npm start
- If you haven't already, sign up for Firebase and follow the initial setup instructions.
- Run
npm install firebase
in your development environment. - Sign up for a Firebase account.
- Add a new Firebase project
- Integrate Firebase into your React app by making a
Firestore.js
Component. - Set up a new database and collection called
community-board
in Firestore
- Run
Note: consult the "Getting Started with Firestore" lesson notes if you need help.
- Input the data below to add an initial post to your
community-board
collection. This will be the basic structure of each post on the community board, but feel free to expand upon it with additional fields if you'd like.
// DocumentID is 1
"user":"hipster2357"
"post":"Farm-to-table drinking vinegar photo booth shaman. Food truck vexillologist skateboard flannel asymmetrical hell of portland gentrify. Blue bottle food truck affogato, aesthetic air plant succulents vice vaporware jean shorts fanny pack. +1 bushwick normcore intelligentsia edison bulb keffiyeh microdosing asymmetrical bespoke cardigan portland hell of banh mi."
"voteCount":0
Note: consult the "5. Set up a database and collection in Firestore" section in the "Getting Started with Firestore" lesson notes if you need help.
Source: hipster ipsum
- Add 3 additional post documents to your collection using the structure above.
Note: make sure to set the "voteCount" variable type to
number
; the others can be kept asstring
(which is the default).
- Replace the current message in
App.js
with the<Post />
Component, and include any necessaryimport
statements. - Complete the
<Post />
component in./Components/Post.js
to:- Connect to your database.
- Indicate the collection and doc to retrieve. For now, retrieve the doc with
{'id':1}
.
- Update the
<Post />
component to receive the postid
via a prop.
Note: Firestore expects the documentID to be a string; if you pass the prop as a number, consider using the
.toString()
method to convert the number to a string.
- Add a function to
App.js
that uses.map()
to efficiently show all 4 posts in the database. For example:
let posts = [1,2,3,4].map((value) => {
// post id's must be strings
let postID = value.toString();
return <Post postID={postID} />
});
- Rewrite the function to get all posts in the Firestore database and generate a
<Post />
for each document. - Update your function to show posts in reverse chronological order so the newest post is at the top of the list and the oldest is at the bottom.
- Add a new Component called
<NewPost />
at the top of the forum which captures two pieces of information:- A username
- The text of a post
Note: Consult the "Firestore in React" section of the lesson notes for a pattern to follow.
- Ensure that the
<NewPost/ >
component adds a new post to Firestore which:- Initially has a
voteCount
property equal to0
- (stretch) Has an
id
that is in sequence with all previous posts. e.g. If the last post has{'id':6}
, the new post should have{'id':7}
.
- Initially has a
- Ensure that the Community Board is updated with any new post that is submitted.
- What other information would you want to capture from users? e.g. URLs? imageURLs? date? etc.
- Add two new components to the
<Post />
Component:- A component to
<Upvote />
which will add 1 vote to the totalVoteCount
. - A component to
<Downvote />
which will subtract 1 vote from the totalVoteCount
.
- A component to
Note: Consider using HTML characters or emoji (see Emojipedia) as symbols for users to click.
- The components should update the value of
VoteCount
for that post both in Firestore and in the App (in state). The value on Firestore and the value in state should always be the same.