Skip to content

Conversation

@bogy0
Copy link
Collaborator

@bogy0 bogy0 commented Nov 20, 2025

This PR adds a simple end-to-end example of how to fetch data. The goal is to give developers a real example how to do this.
(FYI: This PR is a rework of #87. The original branch was created about three months ago and diverged significantly from main, so I recreated the feature on a fresh branch based on the latest main.)

This PR also gives example how to access params/queries in client component

What’s included

PostComponent example

Added a small React component that loads a "blog post" + its "comments". It shows:

  • how to call the API,
  • and how to keep the UI clean by not mixing fetch logic into the component.

Clear separation between layers

  • UI layer: React component that just renders stuff
    src/pages/welcome/post/PostComponent.jsx
  • API layer: tiny client-side wrappers around the backend routes
    src/api/message.js
  • Service layer: server-side handlers that talk to an external API (mocked here)
    src/service/postHandlers.js, src/service/externalHandlers.js

This should make the overall flow easier to follow and copy/paste for future features, also testing can be easier.

New endpoints

  • GET /api/post/:id
  • GET /api/comments?postId=:id
  • GET /api/external/post/:id
  • GET /api/external/comments?postId=:id

These external endpoints are mocked
— they’re only here to demonstrate how the service layer would normally proxy to some real third-party service.

Dashboard extended with handling url params/queries

  • /dashboard/1234?q=xxx&name=Anne

This is how this new section looks in the welcome page:

Screenshot 2025-11-20 at 14 25 38

Modified dashboard page, to demonstrate how to handle params/queries client-side:

Screenshot 2025-11-20 at 15 48 14

- Added externalHandlers.js to simulate external API responses for posts and comments.
- Updated routes to include new external API mock routes.
- Refactored postHandlers to call the new external API endpoints.
- Adjusted server.js to export base URL for easier configuration.
- Enhanced test setup to mock external API calls correctly.

Resolves: no-ticket
Signed-off-by: [Balint Lendvai] <[email protected]>
- Moved server configuration constants (port and base) from server.js to a new config/server-config.js file for better organization and testability.
- Updated imports in postHandlers.js and test files to reflect the new configuration module.
- Adjusted response handling in tests to use HttpResponse from msw.

Resolves: no-ticket
…outing and parameter handling

- Added a utility function to determine active menu items based on current path, supporting dynamic routes.
- Updated NavHeaderItems to utilize the new active path logic.
- Enhanced Dashboard component to access and display URL path and query parameters.
- Configured routing to support dynamic dashboard paths with parameters.

Resolves: no-ticket
rodet
rodet previously requested changes Nov 21, 2025
Copy link
Contributor

@rodet rodet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall! Please review my comments. Later on we should add more test cases but this could be a separate PR.

@bogy0 bogy0 requested a review from rodet November 21, 2025 12:56
Copy link
Member

@lee-chase lee-chase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bogy0 made a small number of changes to fix preview:prod.

Turns out that at run time Express insists on the whole object being selected.

I was also a bit confused about the posts/comments.

The id and postId always seemed to be one.
In addition it would be nice to prevent invalid IDs. Suggest 400 for invalid ID, 404 for out of range/not found.

@bogy0
Copy link
Collaborator Author

bogy0 commented Nov 24, 2025

@lee-chase Thanks for your changes to make the preview:prod command work again.

For your other comments:

I was also a bit confused about the posts/comment
The id and postId always seemed to be one.

The Welcome page's <PostComponent> file was written in a way that it loads the first post:

const loadPost = async () => {
try {
const post = await getPost(1);
setPost(post);
} catch {
setPost('Failed to load message');
}
};

Again this is just an example usage, if you have better idea how you want to demonstrate this in the Welcome page, let me know, but based on your comment im more like confused, then confident whats the goal. If you want to i can write a random loadPost logic so we load a random post from our service. Thats what you meant?


The id and postId always seemed to be one. In addition it would be nice to prevent invalid IDs. Suggest 400 for invalid ID, 404 for out of range/not found.

The src/service/externalHandlers.js file already handles the case when invalid post id is passed, and it gives back 400 status code with message Post not found.

if (!post) {
return res.status(404).json({ message: 'Post not found' });
}

Screenshot 2025-11-24 at 13 43 56

@lee-chase
Copy link
Member

Hi @bogy0, perhaps my understanding of what we are demonstrating is incorrect. I expected postId and id to match some back end identifier and thought by changing the params I might see that.

As for the 404: "Post not found" that is correct, but if the wrong ID format is supplied it may be more appropriate to return 400: Bad request.

Happy for you to reject both these thoughts and approve.

@bogy0
Copy link
Collaborator Author

bogy0 commented Nov 24, 2025

As for the 404: "Post not found" that is correct, but if the wrong ID format is supplied it may be more appropriate to return 400: Bad request.

Happy for you to reject both these thoughts and approve.

The case if post id format is invalid is also covered, and responds with 400 http status, message:
Screenshot 2025-11-24 at 14 15 49

also added JSDoc for PostComponent

Resolves: no-ticket
@bogy0
Copy link
Collaborator Author

bogy0 commented Nov 24, 2025

Hi @bogy0, perhaps my understanding of what we are demonstrating is incorrect. I expected postId and id to match some back end identifier and thought by changing the params I might see

@lee-chase now i understand, sorry for my confusion, so what i did to address this in my latest commit (9a3fb40) is that i added a postId property to the <PostComponent /> and also added JSDoc, so at the Welcome page now its controllable which post we want to load.

@lee-chase lee-chase dismissed rodet’s stale review November 24, 2025 15:13

Issue being raised to deal with test coverage.

@lee-chase lee-chase merged commit ddc77db into main Nov 24, 2025
5 checks passed
@lee-chase lee-chase deleted the feat/network-query-examples-redo branch November 24, 2025 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants