blog

Creating a Blogging App Using React, Part 5: Profile Page

In the previous part of this tutorial series, you saw how to implement the update and delete post feature for our React blog application. In this tutorial, you'll implement the profile page for the blog application.

Getting Started

Let's get started by cloning the source code from the last part of the series.

https://github.com/royagasthyan/ReactBlogApp-EditDelete

Once the directory has been cloned, navigate to the project directory and install the required dependencies.

Save the above changes and restart the server. Point your browser to http://localhost:7777/ and sign in to the application. Once logged in, you will be able to view the menu list with the profile link.

For the profile menu link to work, you need to add a new route to the existing routes in the home.jsx file.

In the home.jsx file, create a new component ShowProfile. Add some state variables for name, password, email, and Id. Inside the render method of the ShowProfile component, add the HTML for rendering the profile details. Here is how the ShowProfile component looks:

When the profile page is loaded, you need to fetch the details from the database and populate it in the form. Add the code in the getProfile method inside the ShowProfile component to make the AJAX call to get details about the user.

Inside the app.js file, create a method called getProfile which will handle the POST method call from the ShowProfile's getProfile method. The getProfile method inside the app.js file will instead make a call to user.js to get details from the database. Here is how it looks:

As seen in the above code, you make a call to the MongoDB using the MongoClient to query the user collection based on the email address. Once the result is received, it's returned back to the callback function.

Save the above changes and restart the Node.js server. Point your browser to http://localhost:7777/#/ and log in to the application. Click the profile link in the menu and you will be able to view the profile details populated on the page.

Updating the User Profile

To handle the name and password change, you need to define two methods called handleNameChange and handlePasswordChange in the ShowProfile component. These methods will set the state variables on text change. Here is how it looks:

Define a method called updateProfile which will be called when the user clicks on the Update button to update the user details. Inside the updateProfile method, make a POST call to the updateProfile method in the app.js file along with the changed name and password. Here is how the updateProfile method in the ShowProfile component looks:

As seen in the above code, once the parameters are parsed in the updateProfile method in the app.js file, the user.updateProfile method is called with changed name, password, and username.

Let's define the user.updateProfile method inside the user.js file, which will make a call to the MongoDB database and update the name and password based on the username. Here is how the updateProfile method in the user.js file looks:

In the above code, you updated the user details based on the email address using the updateOne method.

Save the above changes and restart the server. Log in to the application and click on the Profile link. Change the name and password and click the Update button. Try to sign in, and you will be able to log in using the new password.

Wrapping It Up

In this tutorial, you implemented the profile page for the blog application. You learnt how to fetch details from a database and populate it on the page using React. You also implemented the functionality to update profile details.

Source code from this tutorial is available on GitHub. Do let me know your thoughts or any suggestions in the comments below.