In this episode, we wire up our message fetching logic to our Pinia store. Now that our messages endpoint is available, we build a new action in the store that handles fetching those messages from the backend. We make sure to pass in the room's slug and the desired page (for pagination!) whenever we call this action.
Using Axios, we call our messages API, then update our store state with the current page, and cleverly merge the new messages with any that are already loaded instead of just overwriting our state each time. This way, loading the next page simply adds the additional messages to what’s already there.
To make use of our fresh messages store, we create a new ChatMessages
component that pulls messages from the store and renders them. We drop this new component into the room view and make sure everything lines up visually—with just a quick style tweak for spacing.
At the end, we try everything out and confirm it's working: our messages are fetched when the room loads, stored in the state, and displayed in the chat UI. Pretty satisfying to see the real data flowing into the app!