In this task, we will create the main components of the Posts app and set up the app layout.
Step 1: Create the App Layout
Create the main layout of the app with a sidebar and feed (and a placeholder for another sidebar). We will use the cn utility function to conditionally add classes based on a debug flag. The debug flag will help us visualize the layout during development.
Save the file and check the app layout in the browser.
Step 2: Add Sidebar Component
In this step, we will create a sidebar component with three buttons: Home, Search, and Make a Post. We will use the Button component from the @/components/ui/button module and the HomeIcon, MagnifyingGlassIcon, and PlusCircledIcon icons from the @radix-ui/react-icons package. The react-icons package was installed through shadcn-ui during the project setup.
Create a new file named sidebar.tsx in the components folder and add the following code:
Update the App component to include the Sidebar component:
Save the file and check the app layout in the browser.
Step 3: Add Feed Component
Next, we will create a feed component to display posts. We will use the useState hook to manage the posts state and render the posts in a list. The posts will be sorted by date in descending order.
Create a new file named feed.tsx in the components folder and add the following code:
Notice the PostType interface at the top of the file. This interface defines the shape of a post object. The posts contents are quotes from Edsger W. Dijkstra, a Dutch computer scientist known for his contributions to the field of computer science.
Update the App component to include the Feed component:
In the next steps, we weill break down the Feed component into smaller components to improve readability and maintainability.
Step 4: Move PostType to data/types.ts
Create a new file named types.ts in the src/data folder and move the PostType interface to this file:
This interface will be used in multiple components, so it’s better to define it in a separate file for reusability.
Step 5: Move Initial Posts to data/db.json
Create a new file named db.json in the src/data folder and move the initial posts data to this file:
We will use this file to initialize posts data in the Feed component.
Step 6: Add Header Component
Create a new file named header.tsx in the components folder and add the following code:
Notice that we are using the Button component and style it as a link. The disabled attribute is set to true for the “All Posts” button to indicate that it is not clickable. In this tutorial, we focus on CRUD operations for “My Posts” only.
Step 7: Add Posts Component
Create a new file named posts.tsx in the components folder and add the following code:
Notice that we are using the PostType interface and the initial posts data from the db.json file. In particular, pay attention to how we can import JSON files in Vite without any additional configuration.
With this component, we have separated the rendering of individual posts into a separate component. This separation of concerns makes the code more modular and easier to maintain.
Step 8: Update Feed Component
Now, we will update the Feed component to include the Header and Posts components:
This separation of components makes the code more readable and maintainable. The Header component is responsible for rendering the header buttons, while the Posts component is responsible for rendering the list of posts.
Step 9: Add Post Component
Let’s refine how each post is displayed by creating a new post.tsx component:
In this component, we display the post content, date, and icons for editing and deleting the post. Now, update the Posts component to use the Post component:
Save the files and check the app layout in the browser.
Step 10: Add PostActions Component
We can further refactor the Post component by extracting the post actions (edit and delete buttons) into a separate component. Create a new file named post-actions.tsx in the components folder and add the following code:
Now, update the Post component to use the PostActions component:
Save the files and check the app layout in the browser to make sure everything is working as expected.
Conclusion
In this task, we created the main components of the Posts app and set up the app layout. We created the Sidebar, Feed, Header, Posts, Post, and PostActions components. We also moved the PostType interface to a separate file and the initial posts data to a JSON file. This separation of concerns makes the code more modular and easier to maintain. In the next task, we will add functionality to create, read, update, and delete posts.