Skip to content

Instantly share code, notes, and snippets.

@8lane
Created October 5, 2018 12:38
Show Gist options
  • Save 8lane/21f51552a7bd99ae3bbdfc6230e41d0d to your computer and use it in GitHub Desktop.
Save 8lane/21f51552a7bd99ae3bbdfc6230e41d0d to your computer and use it in GitHub Desktop.
import Immutable from 'seamless-immutable'
import { createReducer } from 'reduxsauce'
import { Types } from './actions'
export const INITIAL_STATE = Immutable({
agentsOnline: 0,
estimatedWaitTime: null,
estimatedWaitTimeConnecting: false,
estimatedWaitTimeAttempts: 0,
attributes: {
Channel: 'Chat',
Department: 'Argos_Service',
InteractionType: 'Ser_General', // should default to 'General'
Tier: ''
},
creatingContext: false,
createContextAttempts: 0,
contextId: '',
chatHostCheckAttempt: false,
chatHostOnline: true,
chatHostCheckRetryAttempts: 0,
contactCenterConnecting: false,
contactCenterOnline: true,
contactCenterRetryAttempts: 0,
orderNumber: '',
error: ''
})
const calculateChatQueue = (state, { orderNumber }) =>
state.merge({ orderNumber })
const saveChatQueue = (state, { chatQueue }) =>
state.merge({
attributes: {
...state.attributes,
InteractionType: chatQueue,
Tier: `${chatQueue}_T1`
}
})
const resetApi = (state) => state.merge(INITIAL_STATE)
const clearAgentsOnline = (state) => state.merge({ agentsOnline: 0 })
const chatHostCheckAttempt = (state) =>
state.merge({ chatHostCheckAttempt: true })
const chatHostCheckSuccess = (state) =>
state.merge({ chatHostCheckAttempt: false, chatHostOnline: true, maxApiRetryAttempts: 0 })
const chatHostCheckFailure = (state, { error }) =>
state.merge({ chatHostCheckAttempt: false, chatHostOnline: false, error })
const chatHostCheckRetry = (state) =>
state.merge({ chatHostCheckRetryAttempts: state.chatHostCheckRetryAttempts + 1 })
const contactCenterAttempt = (state) =>
state.merge({ contactCenterConnecting: true })
const contactCenterSuccess = (state, action) =>
state.merge({ contactCenterConnecting: false, contactCenterOnline: action.online })
const contactCenterFailure = (state) =>
state.merge({ contactCenterConnecting: false })
const contactCenterRetry = (state) =>
state.merge({ contactCenterRetryAttempts: state.contactCenterRetryAttempts + 1 })
const createContextAttempt = (state) =>
state.merge({ creatingContext: true })
const createContextSuccess = (state, { contextId }) =>
state.merge({ creatingContext: false, contextId, maxApiRetryAttempts: 0 })
const createContextFailure = (state, action) =>
state.merge({ creatingContext: false, error: action.error })
const createContextRetry = (state) =>
state.merge({ createContextAttempts: state.createContextAttempts + 1 })
const estimatedWaitTimeAttempt = (state) =>
state.merge({ estimatedWaitTimeConnecting: true })
const estimatedWaitTimeSuccess = (state, { estimatedWaitTime, agentsOnline }) =>
state.merge({ estimatedWaitTimeConnecting: false, estimatedWaitTime, agentsOnline, maxApiRetryAttempts: 0 })
const estimatedWaitTimeFailure = (state, action) =>
state.merge({ estimatedWaitTimeConnecting: false, error: action.error })
const estimatedWaitTimeRetry = (state) =>
state.merge({ estimatedWaitTimeAttempts: state.estimatedWaitTimeAttempts + 1 })
const ACTION_HANDLERS = {
[Types.CALCULATE_CHAT_QUEUE]: calculateChatQueue,
[Types.SAVE_CHAT_QUEUE]: saveChatQueue,
[Types.RESET_API]: resetApi,
[Types.CLEAR_AGENTS_ONLINE]: clearAgentsOnline,
[Types.CREATE_CONTEXT_ATTEMPT]: createContextAttempt,
[Types.CREATE_CONTEXT_SUCCESS]: createContextSuccess,
[Types.CREATE_CONTEXT_FAILURE]: createContextFailure,
[Types.CREATE_CONTEXT_RETRY]: createContextRetry,
[Types.ESTIMATED_WAIT_TIME_ATTEMPT]: estimatedWaitTimeAttempt,
[Types.ESTIMATED_WAIT_TIME_SUCCESS]: estimatedWaitTimeSuccess,
[Types.ESTIMATED_WAIT_TIME_FAILURE]: estimatedWaitTimeFailure,
[Types.ESTIMATED_WAIT_TIME_RETRY]: estimatedWaitTimeRetry,
[Types.CHAT_HOST_CHECK_ATTEMPT]: chatHostCheckAttempt,
[Types.CHAT_HOST_CHECK_SUCCESS]: chatHostCheckSuccess,
[Types.CHAT_HOST_CHECK_FAILURE]: chatHostCheckFailure,
[Types.CHAT_HOST_CHECK_RETRY]: chatHostCheckRetry,
[Types.CONTACT_CENTER_ATTEMPT]: contactCenterAttempt,
[Types.CONTACT_CENTER_SUCCESS]: contactCenterSuccess,
[Types.CONTACT_CENTER_FAILURE]: contactCenterFailure,
[Types.CONTACT_CENTER_RETRY]: contactCenterRetry
}
export default createReducer(INITIAL_STATE, ACTION_HANDLERS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment