Skip to content

Instantly share code, notes, and snippets.

@Nicktho
Last active November 4, 2015 12:25
Show Gist options
  • Save Nicktho/fb2030b65483cea40881 to your computer and use it in GitHub Desktop.
Save Nicktho/fb2030b65483cea40881 to your computer and use it in GitHub Desktop.
Redux actions and app state design for api interaction
// ## FETCH USERS (read all)
// start
{ type: 'FETCH_USERS', payload: { loading: true } }
{
users: {
1: { user: { id: 1, name: 'Fred' } }
2: { user: { id: 2, name: 'Bob' } },
3: { user: { id: 3, name: 'Sandra' } },
loading: true,
error: null
}
}
// success
{ type: 'FETCH_USERS', payload: { users } }
{
users: {
1: { user: { id: 1, name: 'Fred' } }
2: { user: { id: 2, name: 'Bob' } },
3: { user: { id: 3, name: 'Sandra' } },
4: { user: { id: 4, name: 'Billy' } },
loading: false,
error: null
}
}
// error
{ type: 'FETCH_USERS', payload: new Error('Something went wrong'), error: true }
{
users: {
loading: false,
error: new Error('Something went wrong')
}
}
// ## FETCH USER (read)
// start
{ type: 'FETCH_USER', payload: { loading: true }, meta: { id } }
{
users: {
1: { user: { id: 1, name: 'Fred' }, loading: true }
2: { user: { id: 2, name: 'Bob' } },
3: { user: { id: 3, name: 'Sandra' } },
loading: false,
error: null
}
}
// success
{ type: 'FETCH_USER', payload: { user }, meta: { id } }
{
users: {
1: { user: { id: 1, name: 'Fred' } }
2: { user: { id: 2, name: 'Bob' } },
3: { user: { id: 3, name: 'Sandra' } },
loading: false,
error: null
}
}
// error
{ type: 'FETCH_USER', payload: new Error('not found'), error: true, meta: { id } }
{
users: {
1: { error: new Error('not found') }
2: { user: { id: 2, name: 'Bob' } },
3: { user: { id: 3, name: 'Sandra' } },
loading: false,
error: null
}
}
// POST TO USERS (create)
// Start
{ type: 'POST_USERS', payload: { loading: true } }
{
api: {
users: { root: { request: true } }
}
users: {
1: { user: { id: 1, name: 'Fred' } }
2: { user: { id: 2, name: 'Bob' } },
3: { user: { id: 3, name: 'Sandra' } },
4: { user: { id: 4, name: 'Billy' } },
loading: false,
error: null
}
}
// Success
{ type: 'POST_USERS', payload: { complete: true } }
{
api: {
users: { root: {} }
}
users: {
1: { user: { id: 1, name: 'Fred' } }
2: { user: { id: 2, name: 'Bob' } },
3: { user: { id: 3, name: 'Sandra' } },
4: { user: { id: 4, name: 'Billy' } },
5: { user: { id: 5, name: 'Jane' } },
loading: false,
error: null
}
}
// Error
{ type: 'POST_USERS', payload: new Error('not authorized'), error: true }
{
api: {
users: { root: { error: new Error('not authorized') } }
}
users: {
1: { user: { id: 1, name: 'Fred' } }
2: { user: { id: 2, name: 'Bob' } },
3: { user: { id: 3, name: 'Sandra' } },
4: { user: { id: 4, name: 'Billy' } },
loading: false,
error: null
}
}
// POST TO SPECIFIC USER (update/delete)
// Start
{ type: 'POST_USER', payload: { loading: true }, meta: { id } }
{
api: {
users: { root: {}, 1: { request: true } }
}
users: {
1: { user: { id: 1, name: 'Fred' } }
2: { user: { id: 2, name: 'Bob' } },
3: { user: { id: 3, name: 'Sandra' } },
4: { user: { id: 4, name: 'Billy' } },
loading: false,
error: null
}
}
// Success
{ type: 'POST_USER', payload: { complete: true }, meta: { id } }
{
api: {
users: { root: {} }
}
users: {
1: { user: { id: 1, name: 'Fred' } }
2: { user: { id: 2, name: 'Bob' } },
3: { user: { id: 3, name: 'Sandra' } },
4: { user: { id: 4, name: 'Billy' } },
5: { user: { id: 5, name: 'Jane' } },
loading: false,
error: null
}
}
// Error
{ type: 'POST_USER', payload: new Error('not authorized'), error: true, meta: { id } }
{
api: {
users: { root: {}, 1: { error: new Error('not authorized') } }
}
users: {
1: { user: { id: 1, name: 'Fred' } }
2: { user: { id: 2, name: 'Bob' } },
3: { user: { id: 3, name: 'Sandra' } },
4: { user: { id: 4, name: 'Billy' } },
loading: false,
error: null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment