Skip to content

Instantly share code, notes, and snippets.

@rafaelrozon
Last active April 7, 2018 02:21
Show Gist options
  • Save rafaelrozon/baaeee77e3b36d860e86309f231ddb16 to your computer and use it in GitHub Desktop.
Save rafaelrozon/baaeee77e3b36d860e86309f231ddb16 to your computer and use it in GitHub Desktop.
/*
Redux State
Generic structure:
module: {
data: {
resource: {
byId: {},
allIds: {},
current: 0
}
},
error: undefined,
isLoading: false
}
Example of implementation:
packageA: {
data: {
users: {
byId: {
1: { name: 'John Doe'},
2: { name: 'John Doe II' }
},
allIds: [1, 2],
current: 1
}
},
error: undefined,
isLoading: false
}
*/
// Actions - API CRUD
// create data: const post<Resource> = (data) => {/*...*/};
const postUser = data => dispatch => {/*...*/};
// read single data record: const get<Resource singular> = (id) => {/*...*/};
const getUser = userId => dispatch => {/*...*/};
// read multiple data records: const get<Resource plural> = () => {/*...*/};
const getUsers = () => dispatch => {/*...*/};
// udpate data: const update<Resource> = (id, data) => {/*...*/};
const updateUser = (userId, data) => dispatch => {/*...*/};
// delete data: const delete<Resource> = (id) => {/*...*/};
const deleteUser = userId => dispatch => {/*...*/};
// Actions - state changes
// update field in the state: const set<Field> = (data) => {/*...*/};
const setIsLoading = data => {/*...*/};
const setError = data => {/*...*/};
/*--------------------------------------------------------------------------------------*/
// Reducers
// const update<Field> = (state, action) => {/*...*/};
const updateIsLoading = (state, action) => {/*...*/};
const updateError = (state, action) => {/*...*/};
/*--------------------------------------------------------------------------------------*/
// Others
// functions to parse data: const parse<Resource>Data = data => {/*...*/};
const parseUserData = data => {/*...*/};
/*--------------------------------------------------------------------------------------*/
// Selectors
// Data selectors, i.e., selectors that don't return state for components
// selector for a collection of objects: const get<Resource in plural>
const getUsers = createSelector(/*...*/); // returns module.data.resource.byId
const getUsersIds = createSelector(/*...*/); // returns module.data.resource.allIds
// selector for a single data record: const get<Resource in singular>
const getUser = userId => createSelector(/*...*/); // returns module.data.resource.byId[someId]
const getCurrentUser = () => createSelector(/*...*/); // returns module.data.resource.current
// selector for state status: const is<StateStatus> = createSelector(/*...*/);
const isLoading = createSelector(/*...*/);
// Selectors that return data for components
// const get<ComponentName>State = createSelector(/*...*/);
const getUserAvatarState = createSelector(/*...*/);
/*--------------------------------------------------------------------------------------*/
// functions that get data from api and load into redux state
// functions fired in response to events
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment