A Brief Tutorial on Building the UI for a Blog Comments Section

In this lesson, we'll add a UI for the comments section and a new comment form. We'll also display the comments that we get from the React hook.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Full Stack Comments with Hasura and React course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Full Stack Comments with Hasura and React, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course The newline Guide to Full Stack Comments with Hasura and React

In this lesson, we'll add a simple UI for the commands. We already fetch commands on the front end, and now it's time to show them nicely in the application. For the styles, we're going to use Tailwind. It's a CSS framework that provides many easy-to-use CSS utility classes. Our block example comes with Tailwind setup out of the box, so we're ready to start with the UI. First, we're going to alter the JSX in the post component right after the post body. That's where we'll add the comments list and the form. Let's do it step by step. We start with a section for the comments. I will add a section component and apply the same styles that post body has. I will use a utility class which stands for Max with 36 RAM and Max Margin X Auto. Right now, we fetch all the commands, so the number of the commands is an array land. In the next module, we'll be working on pagination and we'll have a separate count variable. Anyway, we'll add an H3 element with a bold text and text color as gray 500. Feel free to customize it and change. As the next step, we'll map through the comments and for each, we'll create an article element with background color gray 100. We'll also add rounded borders, vertical margin and padding. Inside, we'll have information about an author and comment creation date. This element will have a bold font and margin button. The last thing we need to do is add a paragraph element with the content. I'm going to make the text slightly lighter than the rest of the page. I'm also going to format the date so that it's in more readable form. I will use built-in function tool or call date string. The last step is handling loading state. I'm adding a ternary operator and whenever loading variable is true, I will show loading comments text. I'm going for super simple option here. Feel free to add a spinner or some animation to indicate that the comments are loading. In the meantime, I added a few entries to the database so we can open the page and see how it looks. Now let's move on to the form for adding new comments. We don't have the logic for adding the comments, but that doesn't stop us from handling it in the UI. It will actually be easier to test adding the comments when we have the UI ready beforehand. So let's start with state variables. We need to save the state of the form. We'll declare two state variables in the post component. One for the author's username and one for the comments message. Next, we'll add a form component. In the onSubmit function, we'll do two things. We'll use prevent default on the event, which will stop the form from automatically submitting when the sub-mid button is clicked. Thanks to that, we can submit the form data as inharnously and perform some logic like making it an API call. We'll also add a console lock to print the username and the comment. It's only for now and later we'll replace it with an API call to the tasura instance. Next, we'll add an input for the author's username. I will create a label and input component. For the label, I will provide the following styles. The first one is display block. The second one is setting the text color to gray 600. I will also use a smaller text size than the rest of the page and will use text -sm. I also want the font to be bold and a slight margin button. I'm also adding a rounded border to the input and set its width to 100%. Finally, let's wrap it into a div and set the bottom margin for it. Inputs uncharged function will set a corresponding state variable username. Now let's do something similar for the comments content. We'll use the same styles, so I will actually copy paste what we have and change a few details. Instead of input, we'll use the text area and we'll also use the set comment function. The last part of our form is a sub-mid button. I'll make its background color to purple 700 and purple 600 on hover. Text color will be white and bold. I will also set vertical and horizontal padding and make the button rounded. Now we have the whole form component. Let's see how it looks in the browser. Okay, nice! We have the whole UI part covered. In the next lesson, we'll go back to our hook. We'll implement the part responsible for adding new comments and instead of console logs, we'll have a PA course in our form. See you in the next lesson. And feel free to post here for a bit and customize the UI by adding your own styles.