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

replace axiosInstance with getFetchClient inside the admin #15245

Merged
merged 2 commits into from
Feb 3, 2023

Conversation

simotae14
Copy link
Contributor

@simotae14 simotae14 commented Dec 22, 2022

What does it do?

Replace every instance of axiosInstance with the new useFetchClient and getFetchClient in the Admin

We have already implemented the new constructs useFetchClient and getFetchClient in this PR

Now we need to replace every place where we use axiosInstance with the new constructs, test the possible regression errors and still provide the old implementation of the axiosInstance into the helper plugin with a message of the removal in v5

Why is it needed?

Inside the Strapi project we have a lot of different implementations of the same code to use axios and its axiosInstance. In this branch we replace axiosInstance in the admin

How to test it?

You need to use the new construct inside every axiosInstance call and check the possible regression errors

List of the API calls in the Admin where we use the new construct and how to test them:

@simotae14 simotae14 added source: core:admin Source is core/admin package pr: enhancement This PR adds or updates some part of the codebase or features labels Dec 22, 2022
@simotae14 simotae14 self-assigned this Dec 22, 2022
@simotae14 simotae14 marked this pull request as draft December 22, 2022 10:56
@codecov
Copy link

codecov bot commented Dec 22, 2022

Codecov Report

Base: 59.11% // Head: 59.07% // Decreases project coverage by -0.04% ⚠️

Coverage data is based on head (f9ad7ad) compared to base (61f3ef2).
Patch coverage: 40.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15245      +/-   ##
==========================================
- Coverage   59.11%   59.07%   -0.04%     
==========================================
  Files        1502     1501       -1     
  Lines       38360    38365       +5     
  Branches     7385     7384       -1     
==========================================
- Hits        22677    22666      -11     
- Misses      13411    13427      +16     
  Partials     2272     2272              
Flag Coverage Δ
back 47.38% <ø> (ø)
front 67.08% <40.00%> (-0.07%) ⬇️
unit_back 47.38% <ø> (ø)
unit_front 67.08% <40.00%> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...admin/src/components/AuthenticatedApp/utils/api.js 0.00% <0.00%> (ø)
...ager/components/CollectionTypeFormWrapper/index.js 10.24% <0.00%> (-0.74%) ⬇️
...-manager/components/SingleTypeFormWrapper/index.js 10.88% <0.00%> (-0.76%) ⬇️
...n/admin/src/content-manager/pages/App/useModels.js 0.00% <0.00%> (ø)
...ent-manager/pages/App/utils/getContentTypeLinks.js 0.00% <0.00%> (ø)
.../admin/src/content-manager/pages/ListView/index.js 27.48% <0.00%> (-0.99%) ⬇️
packages/core/admin/admin/src/core/utils/index.js 100.00% <ø> (ø)
...dmin/src/hooks/useFetchEnabledPlugins/utils/api.js 0.00% <0.00%> (ø)
packages/core/admin/admin/src/index.js 0.00% <0.00%> (ø)
...ges/SettingsPage/pages/Users/EditPage/utils/api.js 0.00% <0.00%> (ø)
... and 19 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@@ -38,7 +39,7 @@ const fetchStrapiLatestRelease = async (toggleNotification) => {

const fetchAppInfo = async () => {
try {
const { data, headers } = await axiosInstance.get('/admin/information');
const { data, headers } = await get('/admin/information');
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please make sure all calls prefixed with /admin take process.env.STRAPI_ADMIN_BACKEND_URL into account?

Copy link
Member

Choose a reason for hiding this comment

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

We can't use just the env var, we should be using the internal API method to fetch the admin config, eg in both these examples your env var wouldn't work

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks guys, this activity will be part of another ticket just to focus on the axiosInstance replacement on this one. This issue #13889 will be fixed in another ticket with some activities in the BE and in the FE if needed

@simotae14 simotae14 marked this pull request as ready for review January 23, 2023 13:19
gu-stav
gu-stav previously approved these changes Jan 24, 2023
Copy link
Contributor

@gu-stav gu-stav left a comment

Choose a reason for hiding this comment

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

LGTM

@derrickmehaffy
Copy link
Member

Added to my list to review as I want to check to make sure reverse proxying isn't impacted negatively.

@simotae14
Copy link
Contributor Author

Thanks @derrickmehaffy, please let me know the results of your review. Thanks

@gu-stav
Copy link
Contributor

gu-stav commented Jan 25, 2023

@simotae14 I don't know if you have discussed that already internally but I'd prefer if we start to release your branches as soon as they are ready instead of batching them into another branch. The reason would be that we should start to get this on the road brick by brick instead of a big bang release to: 1) limit the impact and therefore bugs 2) catch early on in case this becomes a problem for plugins developers (which I don't think).

We could start with the one from users & permissions right after today's 4.6. release and get it into 4.6.1 - this allows us to have it in main for two weeks.

What do you think?

@simotae14
Copy link
Contributor Author

simotae14 commented Jan 25, 2023

Yes, @gu-stav I agree with your idea, at the beginning I thought was useful to have a main branch (enhancement/axios-migration-step-2) with all the code of the others branches just to remind me to refactor the code, for example, to remove the useCallback and useMemo...but honestly is better to merge piece by piece every branch in main just to double check, as you said, the behavior before the releases

@simotae14 simotae14 changed the base branch from enhancement/axios-migration-step-2 to main January 25, 2023 14:21
@simotae14 simotae14 dismissed gu-stav’s stale review January 25, 2023 14:21

The base branch was changed.

Bassel17
Bassel17 previously approved these changes Jan 26, 2023
Copy link
Member

@Bassel17 Bassel17 left a comment

Choose a reason for hiding this comment

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

LGTM! we need to test everything still

christiancp100
christiancp100 previously approved these changes Jan 26, 2023
[fetchData, params, slug, toggleNotification]
);

const handleConfirmDeleteData = useCallback(
async (idToDelete) => {
try {
await axiosInstance.delete(getRequestUrl(`collection-types/${slug}/${idToDelete}`));
await fetchClient.del(getRequestUrl(`collection-types/${slug}/${idToDelete}`));
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here
const {del} = useFetchClient()

[formatMessage, getData, getDataSucceeded, notifyStatus, push, toggleNotification]
);

const handleConfirmDeleteAllData = useCallback(
async (ids) => {
try {
await axiosInstance.post(getRequestUrl(`collection-types/${slug}/actions/bulkDelete`), {
await fetchClient.post(getRequestUrl(`collection-types/${slug}/actions/bulkDelete`), {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't you use the object destructuring in the del method like in the other ones? It's nothing important, feel free to ignore it 😂

const { post } = useFetchClient()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I needed the fetchClient variable to execute the get method because we also imported the lodash get method

@@ -26,7 +27,7 @@ const useRolesList = (shouldFetchData = true) => {

const {
data: { data },
} = await axiosInstance.get('/admin/roles');
} = await fetchClient.get('/admin/roles');
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I needed the fetchClient variable to execute the get method because we also imported the lodash get method

@joshuaellis joshuaellis self-requested a review January 26, 2023 09:37
Copy link
Member

@joshuaellis joshuaellis left a comment

Choose a reason for hiding this comment

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

The eslint disabling isn't great for me to be honest, it's a quick win and it'll lead to bugs if someone works on the codebase (which we will) before the problem is addressed. It shouldn't be too hard to make the hook have a stable function identity.

I stopped highlighting them all after i realised how many there were.

@@ -186,6 +187,7 @@ const CollectionTypeFormWrapper = ({ allLayoutData, children, slug, id, origin }
return () => {
source.cancel('Operation canceled by the user.');
};
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Member

Choose a reason for hiding this comment

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

Please don't disable this it causes more problems down the line when people edit the function but don't change their deps, if you can't safely put the fetchclient as a dep you need to change the code in the hook to reflect this.

@@ -237,6 +237,8 @@ const CollectionTypeFormWrapper = ({ allLayoutData, children, slug, id, origin }
return Promise.reject(err);
}
},
// TODO: remove when we evaluate the need of the useCallback
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Member

Choose a reason for hiding this comment

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

this again, im not a fan of slapping eslint rules as an easy win it's really bad practice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok I understand but as I said in the comment, I will remove it in this activity https://strapi-inc.atlassian.net/browse/DX-570
if you are not ok with it I need to use again the getFetchClient

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why you don't think you can include it in the deps? It's a stable function because it lives outside of react no?

Copy link
Member

Choose a reason for hiding this comment

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

Looking at the hook quickly if anything you need to wrap the object you return in a useMemo? But we can't have this many eslint disables around hook deps, we'll be shooting ourselves in the foot.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am fine with your comment, I can try to solve my problem in a different way. Just to keep in mind, there are other places, into the code, where we are using the eslint comment

Copy link
Member

Choose a reason for hiding this comment

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

Just to keep in mind, there are other places, into the code, where we are using the eslint comment

Then we should as a collective try get rid of them 🤝

Copy link
Member

Choose a reason for hiding this comment

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

could be an interesting discussion for the Frontend sync @simotae14 @joshuaellis
we should work on enforcing some general rules from now and forward

@Bassel17
Copy link
Member

I am missing the point here why did you got back to getFetchClient ?

derrickmehaffy
derrickmehaffy previously approved these changes Jan 27, 2023
Copy link
Member

@derrickmehaffy derrickmehaffy left a comment

Choose a reason for hiding this comment

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

Tested 2 of the 3 proxying systems and works fine. I'm gonna submit a PR to the docs to remove the 3rd because our proxying configs are wrong (my fault) and they are too technically complex, nor do we recommend that method anymore anyway.

LGTM

christiancp100
christiancp100 previously approved these changes Jan 31, 2023
gu-stav
gu-stav previously approved these changes Jan 31, 2023
@gu-stav
Copy link
Contributor

gu-stav commented Jan 31, 2023

@simotae14 Could you try and delete the back-and-forth commits please before merging this PR? Otherwise you could also squash it, but I think it doesn't make sense to have all of it in the git history.

joshuaellis
joshuaellis previously approved these changes Jan 31, 2023
@simotae14
Copy link
Contributor Author

Thanks Gustav, yes there are too many commits, then I prefer to do a Squash and merge on this case

const { result } = await setup();

expect(result.current).toHaveProperty('get');
expect(result.current).toHaveProperty('post');
expect(result.current).toHaveProperty('put');
expect(result.current).toHaveProperty('del');

expect(result.current.get).toHaveBeenCalledTimes(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Mh, I think if you want to make that test useful you'd have to re-render and confirm it still has only be called once, because right now this is expected no?

Copy link
Contributor Author

@simotae14 simotae14 Jan 31, 2023

Choose a reason for hiding this comment

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

yes sorry, I push the first draft of the test, now I have added the rerendering and if the get is called once even after the rerendering

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this test is reliable or correct. You're testing against the renderHook instance of useFetchClient, you're not testing against the instance of TestComponent. Because wrapper just wraps adds a component around the internal test component of @testing-library/react-hooks...

You should just be using @testing-library/react's render method to render the component with the hook inside and assert against that by spying/mocking the hook.

joshuaellis
joshuaellis previously approved these changes Jan 31, 2023
@simotae14 simotae14 added this to the 4.6.1 milestone Feb 1, 2023
…seFetchClient and getFetchClient in the admin
@simotae14 simotae14 merged commit 8383e33 into main Feb 3, 2023
@simotae14 simotae14 deleted the enhancement/axios-migration-admin branch February 3, 2023 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: enhancement This PR adds or updates some part of the codebase or features source: core:admin Source is core/admin package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants