-
Notifications
You must be signed in to change notification settings - Fork 194
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
RSAA action-creator factories #169
Comments
@ckreiling Could you provide an example or two of how you use this |
@agraboso Absolutely, see below. My app is an admin UI for managing an organization's messaging service, similar to Slack. I have an ApiConfig.js file in the root of my app that maintains all of the URL's requests are made to, and it looks like: const apiVersion = '/v1';
const BASE_URL = 'https://www.fakeurl.com/api' + apiVersion; // base URL for all requests
/**
* Makes a standard config object for a specific request. Just replace the member of the type
* array that corresponds to any action you might want to assign a payload to.
* @param url the URL to be tacked on to the {@code BASE_URL} string
*/
const standardPostConfigFactory = (url) => ({
endpoint: BASE_URL + url,
method: 'POST',
credentials: 'include'
});
// These are certain URLs for the API, we're writing the actual requests inside the actions in order
// to have a better form of verification. These are each paired with a method that defines what
// type of HTTP request is made.
export const apiRequestObjects = {
// Auth endpoints
LOGIN: standardPostConfigFactory('/auth/login'),
LOGOUT: standardPostConfigFactory('/auth/logout'),
AUTH_STATUS: standardPostConfigFactory('/auth/status'),
// System calls for getting big payloads for the user
GROUPS_GET_ALL: standardPostConfigFactory('/system/getgroups'), // gets all the user's Group objects
USERS_GET_ALL: standardPostConfigFactory('/system/getusers'), // gets all the user's User objects
MEMBERS_GET_ALL: standardPostConfigFactory('/system/getmembers'), // gets all the user's User objects
// Group endpoints
GROUPS_CLEAR: standardPostConfigFactory('/groups/clear'), // removes all users from the group
GROUPS_NUKE: standardPostConfigFactory('/groups/nuke'), // nuke a group
GROUPS_FIX: standardPostConfigFactory('/groups/fix'), // fix a group
GROUPS_CREATE: standardPostConfigFactory('/groups/create'), // create a new group
GROUPS_DESTROY: standardPostConfigFactory('/groups/destroy'), // destroy a group
GROUPS_ADD: standardPostConfigFactory('/groups/add'), // add a certain user to the group
GROUPS_EVAL: standardPostConfigFactory('/groups/eval'), // check for illegal members
GROUPS_SHOW: standardPostConfigFactory('/groups/show'),
GROUPS_ABOVE_CURRENT: standardPostConfigFactory('/groups/abovecurrent'),
GROUPS_MEMBERS: standardPostConfigFactory('/groups/members'), // get all the members for a group
// User endpoints
USER_GET: standardPostConfigFactory('/users/get'), // get the user by User ID
}; My Redux state is then cut into slices, stored in their own directories, with RSAA actions tied to each endpoint. For instance, the // Create the new action creator by binding it to the abstracted one
const groupEntityRsaaActionCreator = entityRsaaActionCreatorFactory.bind(this, 'group');
// Create another new action creator, again using bind()
const nuke = groupEntityRsaaActionCreator.bind(this,
apiRequestObjects.GROUPS_NUKE,
ActionTypes.GROUP_NUKE_FETCH,
ActionTypes.GROUP_NUKE_SUCCESS,
ActionTypes.GROUP_NUKE_FAILURE
);
const fix = groupEntityRsaaActionCreator.bind(this,
apiRequestObjects.GROUPS_FIX,
ActionTypes.GROUP_FIX_FETCH,
ActionTypes.GROUP_FIX_SUCCESS,
ActionTypes.GROUP_FIX_FAILURE
); I find this recipe to be very extensible. Please let me know what you think as I'd love to improve upon my existing implementation! |
@nason I'm really interested in helping with a "recipes" section, as I only started loving the library as I found ways to consolidate my code. I also think it should be made more evident in the docs that chaining RSAA dispatches using the |
@ckreiling awesome! I've been thinking about documentation in general, and think that some recipes or even example apps would be great additions. Do you want to make a PR to kick this off?
Great point. I agree this should be in the docs. |
is anything changing? |
@geminiyellow I'd like to add a recipes section to the documentation. It's been awhile since I had a use-case for this project, but I loved using it when I did. Let me know what you think. I want to create a recipes section demonstrating the use of higher-order functions in order to build action-creators for different entities. |
@ckreiling 👍 , or you can create a new doc who's title is i created one, redux-api-actions , but it is not very good. |
Hello,
The below example is a quick whip-up, but if you have any suggestions for improvement please let me know. In my case, and I'm guessing in many others', it's common to send one 'entity' to the backend in a request (like an object's ID), so I made this nice little RSAA action creator to abstract away that functionality. The 'apiRequestObject' can be any of the other valid RSAA action properties besides 'body' and 'types'. I then use Javascript's convenient
bind()
method to make more specific action creators.So what does everyone think about incorporating an action creator factory for RSAAs?
The text was updated successfully, but these errors were encountered: