Search code examples
reduxflux

Where should I put the request status in a flux standard action?


If I want to standardized action types across multiple statuses into a single type and put the status as a property, where in the flux standard action should I set it? In the payload or in the meta?

For example, rather than having 3 action types:

export const LOGIN_REQUEST = 'LOGIN_REQUEST';
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS';
export const LOGIN_FAILURE = 'LOGIN_FAILURE';

I would just have one action type

export const LOGIN = 'LOGIN';

If the action.error !== null, I can assume failure, else it would be success. However, where should I put the status that indicates that it is awaiting results or that it has begun an async request, i.e. BEGIN, or REQUESTED status.

Is there an accepted standard for this? Should I just set a property on meta called "isFetching" or something similar?

I suppose there are many ways to do this - but I'm hoping for a standard in the Flux Standard Action.


Solution

  • Your payload can have 3 "shapes" so to speak... With a ".error" property, a ".response" property, and with neither ".error" nor ".response". At the beginning of your async fetch, you'll have no .response and no .error (equivalent of LOGIN_REQUEST). If an error occurs, payload will have a non-null .error (equivalent of LOGIN_FAILURE). If request succeeded, payload will have non-null .response property (equivalent of LOGIN_SUCCESS)