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

Is not sent (pristine) indicator/flag/state #329

Closed
ghost opened this issue Mar 30, 2020 · 7 comments
Closed

Is not sent (pristine) indicator/flag/state #329

ghost opened this issue Mar 30, 2020 · 7 comments

Comments

@ghost
Copy link

ghost commented Mar 30, 2020

Hello. Thank you for your library, it's great!

In some cases, we need to know that the request hasn't been sent yet at all. But getQuery always returns the default structure:

https://github.com/klis87/redux-saga-requests/blob/master/packages/redux-saga-requests/src/selectors/get-query.js#L121

const defaultQuery = {
  data: null,
  loading: false,
  error: null,
};

Just checking over data is null won't work as the backend may return, for example, either null or a model (yes, that happens ¯_(ツ)_/¯).


Is there a way to extend defaultQuery (not default data) or to understand, that the query is "pristine"?

It looks like, just adding pristine flag to defaultQuery should solve the issue as the default query is returned any time when there is no data for the query (it's not initialized/sent/etc.) in requests` state.

@ghost ghost changed the title Is not sent indicator/flag/state Is not sent (pristine) indicator/flag/state Mar 30, 2020
@klis87
Copy link
Owner

klis87 commented Mar 30, 2020

Hi, I am glad you like this library!

Actually in roadmap #306 there is a task:
add loaded prop to query object, as without it it is not possible to know whether data is empty as request was not dispatched yet or because it is truly empty, this will also prevent flickering if requests is dispatched for the 1st time after component is rendered`

Regarding the idea, I think this actually be solved above getQuery, probably in reducer. Reducer could store loaded: false initially and on 1st success response it would be true and stay true. But resetRequests action could still make it going back to false.

For now I think you can solve it in 2 ways:

  1. use action.meta.getData to transform server response and replace null with sth else, like 'empty' or whatever, then you will be able to dinstinguish 2 scenarios
  2. If you use React, you could use useQuery or Query and wrap them with containing hook or component, with additional state flag, false initially, which you could change to true in useEffect hook

@klis87
Copy link
Owner

klis87 commented Mar 30, 2020

  1. you could store those flags yourself in your own reducer, and wrapping getQuery with your selector, using reselect, reading query and your flags reducer, adding extra pristine flag to response

@ghost
Copy link
Author

ghost commented Mar 31, 2020

Hello! Thank you for your reply.

For now, it's resolved at the request level. An Axios interceptor runs a casting function if it's passed in the config. It just throws a simulated not found error.

The issue is not for a particular problem. It's very handy in many cases to have an ability to understand that the request is "pristine". I'm glad to know there is the roadmap with the feature coming!

Looking forward to new versions. This issue can be closed. :)

@ghost ghost closed this as completed Mar 31, 2020
@cluk3
Copy link

cluk3 commented Apr 3, 2020

Hey @klis87 , It's not clear to me what the task in the roadmap is about as the query has already a loading prop. I think a good solution would be to replace the loading prop with a status one, everything is explained very well in this blog post: https://kentcdodds.com/blog/stop-using-isloading-booleans

@klis87
Copy link
Owner

klis87 commented Apr 3, 2020

@cluk3 In reducers there is even no loading prop, there is just pending counter, which is just integer, number of current pending requests of a given type. Boolean would be indeed buggy, as for instance sequence 1stREQUEST > 2ndREQUEST > 1stRESPONSE > 2ndRESPONSE would cause 1stRESPONSE to set loading to false despite the fact there is still 2ndREQUEST pending.

getQuery transforms pending integer as loading: pending > 0

This task is to give 1 extra information, to distinguish between:

  1. I dont have any pending requests of a given type, and I did not even fire any request yet
  2. I already fired request of this type, but response.data from server was null and I really want to know that I tried, but data was empty

This flag wont be necessary for response.data like empty, or [], but for null it is as by default data is null if nothing was fetched or a query was reset.

Regarding the attached article, I agree that booleans can be abused if you have many states, and state machines are better then, but in our case 90% people will need only loading flag, pristine will be just extra flag for 10% cases. I dont want all people to do if(query.loading === 'loading', just if(query.loading)

@cluk3
Copy link

cluk3 commented Apr 4, 2020

I understand now, thanks a lot for the answer and for the awesome work with the library!

@klis87
Copy link
Owner

klis87 commented May 17, 2020

@umbrella-kirill-karpov @cluk3 pristine added to queries in @redux-requests/[email protected]

also resetRequests sets pristine back to true

This issue was closed.
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

No branches or pull requests

2 participants