Skip to content

[Issue 143] Refactor /resources page to use React Query #162

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

Merged
merged 16 commits into from
Oct 3, 2020

Conversation

lpatmo
Copy link
Member

@lpatmo lpatmo commented Sep 26, 2020

What type of PR is this? (check all applicable)

  • ♻️ Refactor
  • ✨ Feature
  • 🐛 Bug Fix
  • 🎨 Enhancement
  • 📝 Documentation Update
  • 🔖 Release
  • 🚩 Other

Context

Closes #143. This PR updates the /resources page to use React Query instead of useEffect.

Screenshots/Recordings (if there are UI changes)

image

Implementation Details - what was your thought process as you changed the code?

[x] Removed useEffect in search in favor of React Query
[x] Added new getResources function in utils/queries
[x] Removed the avatar part of the cardheader

Related Tickets & Documents (Optional)

Placeholder example: This relates to issue #23.

Filed issue #43 as a next step to do after this PR is merged.

Added tests?

A basic test already exists for the /resources page.

  • 👍 yes
  • 🙅 no, because they aren't needed
  • 🙋 no, because I need help

Added to documentation (readme.md or contributing.md)?

  • 📜 readme.md
  • 📜 contributing.md
  • 🙅 no documentation needed
  • 🙋 I'd like someone to help write documentation, and will file a new issue for it

Copy link
Contributor

@angelocordon angelocordon left a comment

Choose a reason for hiding this comment

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

Overall, looks good. There's one thing I'd prefer us to handle differently, but the rest is fine 👍🏽

</Typography>
)}
<br />
{loading && !errorMessage ? (
{isLoading && !isError ? (
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's handle the error and loading state like how it's handled above:

 if (isLoading) {
    return <p>Loading...</p>;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops, I just realized that the isLoading && !isError is leftover logic, but since we already have an isLoading on the page we don't need two. So this is the change I made: b5e0335 Is that acceptable?

@@ -7,4 +7,13 @@ const getResource = async (_key, id) => {
return data;
};

export { getResource };
const getResources = async searchTerm => {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to have a test for this, mainly for documentation or what we expect to get in return.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 How does this look? 7845982

const getResources = async searchTerm => {
const url =
searchTerm !== ''
? `${API_URL}/resources/?search=${searchTerm}` // Empty '' search term will return 0 results
Copy link
Contributor

Choose a reason for hiding this comment

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

It makes sense that an empty search term will return 0 results, but it's a bit of an over reach to query the /resources/ endpoint instead, which would return all or a paginated set of resources IIRC, right? If we're doing this, then the conditional in line 46 of the resource/index page will never get hit
https://github.com/codebuddies/frontend/pull/162/files#diff-cec2f6afae4583cc82d24feda5d79ad6R46

Copy link
Member Author

Choose a reason for hiding this comment

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

When the component is initially loaded, I have searchValue initialized to equal '' an empty string but if I search for an empty string, I'll get 0 results:

image

However, if I type to search '' explicitly in the searchbox or a search term that isn't found, I'll get the expected 0 results:
image
image

I'm not sure if this answers your question? Was a bit confused by the question, since the conditional in line 46 in resources/index.js does get hit from what I understand...

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'm mainly finding it odd the way that we're validating the searchTerm in the query method itself. Maybe we should be validating in the frontend before it gets passed to here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm... how would we validate in the frontend?

@tgrrr
Copy link

tgrrr commented Sep 27, 2020

Great work overall @lpatmo!

@lpatmo
Copy link
Member Author

lpatmo commented Sep 29, 2020

@tgrrr Worked w/ @gauravchl to make 5fc473c and b2ad22f which responds to the two suggestions you made!

@tgrrr
Copy link

tgrrr commented Sep 29, 2020

I've made a couple more comments.

One thing I'd like to add handling for is a timeout. Is that something we get for free with React Query?

If so, should we add some Toast notifications as another ticket?

@lpatmo
Copy link
Member Author

lpatmo commented Sep 29, 2020

One thing I'd like to add handling for is a timeout. Is that something we get for free with React Query?

Hmm... not sure if that's something we get from React Query, but yeah, let's revisit in a new ticket!

],
};

axios.get.mockImplementationOnce(() => Promise.resolve({ data }));
Copy link
Contributor

Choose a reason for hiding this comment

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

I find this to be a little weird but unsure how to really fix it. Just leaving this as a comment and perhaps something that we'll need to figure out how to do better.

We're asserting on the response object that we implemented. If the API breaks and that the response object is different, we'll never know in this case.

const getResources = async searchTerm => {
const url =
searchTerm !== ''
? `${API_URL}/resources/?search=${searchTerm}` // Empty '' search term will return 0 results
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'm mainly finding it odd the way that we're validating the searchTerm in the query method itself. Maybe we should be validating in the frontend before it gets passed to here?

@angelocordon angelocordon merged commit 806f8c8 into main Oct 3, 2020
@angelocordon angelocordon deleted the issue-143-refactor branch October 3, 2020 20:38
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.

[Refactor] Refactor the /resources page to use React Query
4 participants