Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move fetch-logic to custom hook to separate responsibilities #187

Merged
merged 5 commits into from
Feb 28, 2023

Conversation

olavis
Copy link
Contributor

@olavis olavis commented Feb 27, 2023

Why is this pull request needed?

This pull request is needed because we don't want to break SOLID principles. (Single Responsibility Principle)

What does this pull request change?

  • Throws a more meaningful error if there's problems reading the version file
  • Adds type CommitInfo to get better documentation of the code
  • Separates responsibilities - rendering of the JSX component and fetching data
  • Increases testability of VersionText.tsx, reducing the need of conditional rendering (potentially slightly better performance), making the code adaptable to changes.

Ideally, I'd like the component to be even more independent of the fetch, but after I added the new error message, I noticed that the endpoint threw 404, and it'd be nice to see the what a successful response looked like. I think it makes sense to try to fix that first, and then continue the refactoring after. #188

Given that the response is expected to be successful, it could be easier to predict what VersionText.txt returned if commitInfo was undefined initially. That way, the fetch-function would bear the whole responsibility for handling the fetch-response. We could have confidence that the only reason for commitInfo to appear missing in the JSX component, is that it doesn't exist, and not because it fails to handle the fetch-response (it shouldn't be responsible for that). Ternary operators checking for '' could at least be replaced by nullish coalescing operators, resulting in one less option for what the output might be. Maybe it's possible to remove conditionals altogether, depending on what a typical response look like.

Copy link
Contributor

@ingeridhellen ingeridhellen 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!

@olavis olavis merged commit 925d5b4 into main Feb 28, 2023
@olavis olavis deleted the refactor/separate-fetch branch February 28, 2023 08:33
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.

2 participants