Last active
February 17, 2017 06:37
-
-
Save ElmoJones3/50533889bc0eb66a45e34fa4aee3e4dd to your computer and use it in GitHub Desktop.
Immutable JS Reducer Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { combineReducers } from 'redux'; | |
import { List, fromJS } from 'immutable'; | |
import { | |
FETCH_USER_TEAM, | |
FETCH_USER_TEAM_FAILURE, | |
FETCH_USER_TEAM_SUCCESS, | |
FETCH_USER_THREADS, | |
FETCH_USER_THREADS_FAILURE, | |
FETCH_USER_THREADS_SUCCESS, | |
FETCH_THREAD_CONVERSATION, | |
FETCH_THREAD_CONVERSATION_FAILURE, | |
FETCH_THREAD_CONVERSATION_SUCCESS, | |
CREATE_USER_THREAD, | |
CREATE_USER_THREAD_FAILURE, | |
CREATE_USER_THREAD_SUCCESS, | |
ARCHIVE_USER_THREAD, | |
ARCHIVE_USER_THREAD_FAILURE, | |
ARCHIVE_USER_THREAD_SUCCESS, | |
ADD_MESSAGE_TO_THREAD, | |
ADD_MESSAGE_TO_THREAD_FAILURE, | |
ADD_MESSAGE_TO_THREAD_SUCCESS, | |
} from '../../constants'; | |
const handleLoading = (state = false, action) => { | |
switch (action.type) { | |
case FETCH_USER_TEAM: | |
case FETCH_USER_THREADS: | |
case FETCH_THREAD_CONVERSATION: | |
case CREATE_USER_THREAD: | |
case ADD_MESSAGE_TO_THREAD: | |
case ARCHIVE_USER_THREAD: | |
return true; | |
case FETCH_USER_TEAM_FAILURE: | |
case FETCH_USER_TEAM_SUCCESS: | |
case FETCH_USER_THREADS_FAILURE: | |
case FETCH_USER_THREADS_SUCCESS: | |
case FETCH_THREAD_CONVERSATION_FAILURE: | |
case FETCH_THREAD_CONVERSATION_SUCCESS: | |
case CREATE_USER_THREAD_FAILURE: | |
case CREATE_USER_THREAD_SUCCESS: | |
case ARCHIVE_USER_THREAD_FAILURE: | |
case ARCHIVE_USER_THREAD_SUCCESS: | |
case ADD_MESSAGE_TO_THREAD_FAILURE: | |
case ADD_MESSAGE_TO_THREAD_SUCCESS: | |
return false; | |
default: | |
return state; | |
} | |
}; | |
const handleErrors = (state = null, action) => { | |
switch (action.type) { | |
case FETCH_USER_TEAM_FAILURE: | |
case FETCH_USER_THREADS_FAILURE: | |
case FETCH_THREAD_CONVERSATION_FAILURE: | |
case CREATE_USER_THREAD_FAILURE: | |
case ARCHIVE_USER_THREAD_FAILURE: | |
return action.errors; | |
default: | |
return state; | |
} | |
}; | |
const initialThreadState = List().toJS(); | |
const handleThreads = (state = initialThreadState, action) => { | |
switch (action.type) { | |
case FETCH_USER_THREADS_SUCCESS: | |
return fromJS(state).mergeDeepWith( | |
(prev, next) => next, | |
fromJS(action.threads), | |
).toJS(); | |
case CREATE_USER_THREAD_SUCCESS: | |
return fromJS(state).push(fromJS(action.thread)).toJS(); | |
case FETCH_THREAD_CONVERSATION_SUCCESS: | |
return fromJS(state).update( | |
fromJS(state).findIndex(item => item.get('id') === action.id), | |
item => item.set('messages', fromJS(action.messages)) | |
).toJS(); | |
case ADD_MESSAGE_TO_THREAD_SUCCESS: | |
return fromJS(state).update( | |
fromJS(state).findIndex(item => item.get('id') === action.id), | |
item => item.set( | |
'messages', | |
item.messages.push( | |
fromJS(action.message) | |
) | |
) | |
).toJS(); | |
case ARCHIVE_USER_THREAD_SUCCESS: | |
return fromJS(state).delete(fromJS(state).findIndex(item => item.get('id') === action.id)).toJS(); | |
default: | |
return state; | |
} | |
}; | |
export const threadsReducer = combineReducers({ | |
isLoading: handleLoading, | |
errors: handleErrors, | |
threads: handleThreads, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment