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

feat: Create API Layer and custom Hook to use the API #1434

Closed
wants to merge 16 commits into from

Conversation

rodrigoea
Copy link
Contributor

Description

Currently, we have API calls on the client sprinkled around, and where/how we make requests is not always obvious.

This PR creates an API Layer to try to reduce data calls and use them in the top-level containers. The goal is to centralize all of our web requests to a single layer.

This new API layer is a generic layer that uses axios to manage the calls.

Also, a generic custom hook useApi was created to consume the new API layer. The intention with the hook is to have a UI handler separated from the API layer itself, where we'll be able to handle each call better, having its status like isLoading, isError and the data per call. This hook can be reusable, even extendable.

useApi -- new custom hook

Basic example

import React from ‘react’;
import useApi from ‘viewers_path/src/customHooks/useApi’;

const Component = () => {
  const { state, api } = useApi();
  const fetchData = () => {
    api.GET(‘fetchUrl’);
  };
  const shouldRenderData = !state.isLoading && !state.isError;
  return (
    <>
      {state.isLoading && ‘Loading...}
      {state.isError && state.errorMessage}
      {shouldRenderData && JSON.stringify(state.data)}
      <button onClick={fetchData}>Click to fetch</button>
    </>
  );
};

export default Component;

@codecov
Copy link

codecov bot commented Feb 10, 2020

Codecov Report

❗ No coverage uploaded for pull request base (master@4fd0c6d). Click here to learn what that means.
The diff coverage is 72.34%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #1434   +/-   ##
=========================================
  Coverage          ?   12.43%           
=========================================
  Files             ?      274           
  Lines             ?     6990           
  Branches          ?     1318           
=========================================
  Hits              ?      869           
  Misses            ?     4955           
  Partials          ?     1166
Flag Coverage Δ
#core 17.03% <ø> (?)
#viewer 1.96% <72.34%> (?)
Impacted Files Coverage Δ
platform/viewer/src/customHooks/useApi.js 72% <72%> (ø)
platform/viewer/src/api/api.js 72.72% <72.72%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4fd0c6d...a0c57d7. Read the comment docs.

@dannyrb dannyrb added this to In Review in Official v2 Release Feb 13, 2020
Copy link
Contributor

@galelis galelis left a comment

Choose a reason for hiding this comment

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

Great changes @rodrigolabs, this would be a very good feature once we start to use it. 👍

Why do we have it implemented on Viewer level?
Maybe we could add it as an OHIF/core so we can use in other extensions, packages as well.

Code changes looks good to me, with some minimal questions.

Great job! 🎊

"lodash": "4.17.15",
"lodash.clonedeep": "4.5.0"
"lodash.clonedeep": "4.5.0",
"react-test-renderer": "^16.12.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we using this package?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, because of @testing-library/react-hooks. It uses react-test-renderer but it doesn't come bundled together to allow us to install a specific version of react-test-renderer.

@@ -0,0 +1,3 @@
export default {
studies: 'url',
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to add this data here? Can it be empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is just an initial file for us to keep in mind that we should centralize API URLs there.
I'll remove the studies key/value and keep this file empty for future use :)
Thanks!

@rodrigoea
Copy link
Contributor Author

@galelis Thanks for reviewing this PR :)

Why do we have it implemented on Viewer level?
Maybe we could add it as an OHIF/core so we can use in other extensions, packages as well.

Initially, It will live in the viewer project folder for now, as requested by @dannyrb -- what's your opinion on that?

@dannyrb dannyrb self-requested a review February 20, 2020 21:03
@stale
Copy link

stale bot commented Mar 12, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Official v2 Release
  
In Review
Development

Successfully merging this pull request may close these issues.

None yet

2 participants