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

Refs #29992 - pass stopInterval as callback also on API success #7842

Merged

Conversation

Ron-Lavi
Copy link
Member

There are many scenarios where we would like to stop the API interval after receiving some information from the server,
in order to do it easily, the stopInterval for the specific key would be passed as a second param in the handleSuccess callback.

dispatch(
  get({
    key: MY_SPECIAL_KEY,
    url,
    handleSuccess: (response, stopInterval) => {
       if (response.taskFinished) stopInterval()
    }
  })
);

@theforeman-bot
Copy link
Member

Issues: #29992

Copy link
Member

@amirfefer amirfefer left a comment

Choose a reason for hiding this comment

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

Thanks @LaViro! 👍
I left a few inline comments :)

@@ -131,3 +131,6 @@ dispatch(
```

The success handling function will receive the response object from the API request.

In case you are also using the interval middleware with the same `KEY`,
Copy link
Member

Choose a reason for hiding this comment

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

can you also add a mention in the Interval Middleware story?

@@ -36,6 +36,21 @@ Array [
`;

exports[`API get should dispatch request and success actions on resolve 1`] = `
Array [
Copy link
Member

Choose a reason for hiding this comment

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

what this extra snap gives us?

Copy link
Member Author

Choose a reason for hiding this comment

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

it captures the handleSuccess callback args as the API response,
and the second arg is a Function - the stopInterval / warning callbacks

@@ -22,6 +22,10 @@ export const apiRequest = async (
) => {
const { REQUEST, SUCCESS, FAILURE } = actionTypeGenerator(key, actionTypes);
const modifiedPayload = { ...payload, url };
const stopIntervalCallback = selectDoesIntervalExist(getState(), key)
? () => dispatch(stopInterval(key))
: noop;
Copy link
Member

Choose a reason for hiding this comment

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

how about making a function that prints to the console a warning if a user consumes it?

Copy link
Member Author

Choose a reason for hiding this comment

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

great idea, added :)

@@ -25,6 +26,9 @@ export const apiRequest = async (
) => {
const { REQUEST, SUCCESS, FAILURE } = actionTypeGenerator(key, actionTypes);
const modifiedPayload = { ...payload, url };
const stopIntervalCallback = selectDoesIntervalExist(getState(), key)
? () => dispatch(stopInterval(key))
: () => console.warn(`There's no interval API request for the key: ${key}`);
Copy link
Member

Choose a reason for hiding this comment

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

is this warning needed in production?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I think so, it will be easier for debugging

Copy link
Member

@amirfefer amirfefer left a comment

Choose a reason for hiding this comment

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

Thanks, @LaViro 👍
We do have console warnings in other places that trigger in production, though the deprecate function triggers only in development which makes sense.

Katello failures aren't related

@amirfefer amirfefer merged commit a58aa42 into theforeman:develop Sep 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants