In this task, we will add navigation functionality to the Posts app. We will add navigation links to view comments for a post and navigate back to the home page.
Add an action button to view comments
We will add a button to view comments for a post. When the button is clicked, the user will be navigated to the comments view for the post.
Open the web/src/components/post/post-actions.tsx file and update the PostActions component as follows:
Notice the navigateToCommentsView function that uses the openPage function to navigate to the comments view for the post. We pass the postId as a parameter to the openPage function to identify the post.
Update the home button
We will update the home button in the sidebar to navigate back to the home page when clicked. Open the web/src/components/layout/sidebar.tsx file and update the Sidebar component as follows:
Notice the navigateHome function that uses the openPage function to navigate back to the home page. We pass the "home" route as a parameter to the openPage function to navigate to the home page.
We also updated the βMake a Commentβ button to use the variant="default" style to match the other buttons in the sidebar. The button now displays a chat bubble icon to indicate making a comment.
Update the header
We will update the header component to display the appropriate title based on the current page. Open the web/src/components/layout/header.tsx file and update the Header component as follows:
Notice the page store that we use to determine the current page. We display different titles based on the current page. If the current page is the βpostβ page, we display βMy Commentsβ and βAll Commentsβ buttons. If the current page is the βhomeβ page, we display βMy Postsβ and βAll Postsβ buttons.
Test the navigation
You can now test the navigation functionality by clicking the buttons to view comments for a post and navigate back to the home page.
The navigation should work as expected, and you should see the appropriate title displayed in the header based on the current page.
Conclusion
In this task, we added navigation functionality to the Posts app. We added buttons to view comments for a post and navigate back to the home page. We updated the sidebar, header, and post actions components to enable navigation between different views in the app. You can now navigate between the home page and the comments view for a post.