I created a reddit clone
Tags: react, node, javascript, css
Published: 2021-02-09 20:02:00

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/16viblq6yoxsgi6muay6.PNG)

[Live Demo](https://readit-nine.vercel.app/)
[Frontend] (https://github.com/thomasjk0831/readFront)
[Backend] (https://github.com/thomasjk0831/readBackend)

This is a clone of reddit, a website where users can comment and vote on user-created content. It was built using React, Node.js, and CSS. First, I approached this project by brainstorming user stories so that I could create a description of what the site does from an end-user perspective.
<div>

**User Stories:**

As a new user, I can register and/or login.
As a user, I can create a new post on a sub.
As a user, I can create a new sub.
As a user, I can upvote a post.
As a user, I comment on a post.
As a user, I can search for posts based on title.
As a user, I can view all posts on a sub.
As a poster, I can delete my own posts or comments.

**User Flow Diagram:**

I created this visual diagram from the user stories to understand how users interact with this website.
[User Flow](https://whimsical.com/readit-user-flow-VoGrw2opQUb8zDbmKU5KmL)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i839erkdccle4ul4xlt9.PNG)


**Database Schema:**

I also created a DB schema to represent the backend. I found it useful as a visual and logical representation of the API as I was building the endpoints

[Schema](https://whimsical.com/readit-db-schema-5irc8UjE8v6dJvPfEQDtr3)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ip9dprcnb1pu8szx53u3.PNG)

**API Contracts:**

This is the documentation of the API endpoints. 
[API Documentation](https://documenter.getpostman.com/view/12353028/TW76C48m)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t5cco7hvfw1a027iscts.PNG)

<h2>Some Engineering Tasks:</h2>

 <h3>"As a new user, I can register. As a user, I can log in"</h3>
 **(backend: register/login)**
We will be using JWT as the authentication method. Here we are validating that the user's new password is valid and then we are using the 'bcryptjs' library to hash the password before creating a new user.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xvznj8u3imlbdfxq7ieu.PNG)
<div>
When users are logging in, we are checking that they are registered already and then we will compare the hash password with the password inputted. If the user is registered and the password is correct, we will send the token to the frontend.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m315wer0ckndd0fa02a9.PNG)
<div>

**(Frontend: login):** 
The local storage is updated with the token as well as the user's id.  Users will need a token to do certain tasks(i.e creating posts, comments, etc). If users do not have a token, they will not be able to reach some restricted endpoints from the frontend.

We are also updating the log-in state with the redux function: 'logUserIn'. Finally, the user will be routed to the main page after successful login.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1d19irvxc88marslbxcw.PNG)
<div>
<div>

<h3 color=red>"As a user, I can create a new post on a sub"</h3>
**(Backend: make a new post request):**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/040xzthfnb6ta0jvqi07.PNG)
Access to this endpoint is 'restricted' to logged-in users only. This is accomplished using a middleware function using the 'jsonwebtoken' library. We will use this library to verify if the token is valid. If the token turns out to be valid, the 'next' function allows the post request to continue.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zwy0kvqf9syz6pjukrjx.PNG)
Once the token is verified, the endpoint checks if the new post is valid.(It needs a valid title, body, etc) 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jiipaxehmvigqddno92s.PNG)
Once the new post is verified, the function 'add' is called which queries the database and inserts a new post.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ptr3sobz3pqfg2p5vprk.PNG)
<div>
<div>
**(Frontend: When users make a new post request):**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1bvp7gnrfw2kq13wokih.PNG)

Here, 'tempPost' is an object that represents the new post from a user. The API call 'axiosWithAuth' is restricted to only logged-in users. This means that frontend needs to add a header with a valid token in order for backend to accept the request.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t41kpsfu2j2bkhb4hg6b.PNG)

A function handles the submit request for a new post. It checks that the post is valid as well as checking that a sub exists for that post. If the post is not valid, it will display an error message to the user.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7iocygcfb8ll8m6jxv22.PNG)
<div> 
<div>
<h3>"As a user, I can search for posts based on title."</h3>
**(frontend:search posts):**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/y0bvsk2w2iw0yauxdpvj.PNG)
Users will be able to filter posts by title. The function 'submitHandler' is run when the user presses the submit button. The redux function: 'searchPosts' is then called and the 'searchString' is passed as an argument.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vln04xl4ditavrhnmtcg.PNG)
The state of the posts is then handled inside the reducer. The searchString is represented by 'action.payload' and it is used to filter the existing post state. The magic of React happens when it displays the filtered post dynamically.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ad4pkg0c06fryeveg85o.PNG)
<div>
<div>

**Final Thoughts**
 I loved that I was able to work on both the backend and frontend.
Being able to build a whole web app by myself was very satisfying.
 The biggest challenge for me was fleshing out the user experience on the front end. Creating a userflow diagram really helped me understand the flow of the site as well as when to load certain components and states. Some of the things I can improve on in the future are: a real time chat option, improved modularity, ability to follow users, and user profile pictures.

Finally, this was a really fun project and I had a blast building it.