import { combineReducers } from 'redux' import { omit } from 'lodash' import { reducerWithInitialState } from 'typescript-fsa-reducers' import * as actions from './actions' const playbookList = reducerWithInitialState([]) .case(actions.searchPlaybooksSuccess, (state, playbookList) => playbookList) const searchPlaybooksStatus = reducerWithInitialState(actions.searchPlaybooks.toString()) .casesWithAction([ actions.searchPlaybooks, actions.searchPlaybooksSuccess, actions.searchPlaybooksPending, actions.searchPlaybooksFailed, ], (state, action) => action.type) const emailsByTabId = reducerWithInitialState({} as any) .caseWithAction(actions.setSFEmails, (state, action: any) => { state[action._sender.tab.id] = action.payload.emails return state }) .case(actions.clearState, (state, { tabId }) => omit(state, [tabId])) const activeTabId = reducerWithInitialState(null) .caseWithAction(actions.setSFEmails, (state, action: any) => action._sender.tab.id) const enrolledSequenceCount = reducerWithInitialState(0) .case(actions.fetchEnrolledSequenceCountSuccess, (state, count) => count) .case(actions.enrollSuccess, state => state + 1) const fetchEnrolledSequenceCountStatus = reducerWithInitialState(actions.fetchEnrolledSequenceCount.toString()) .casesWithAction([ actions.fetchEnrolledSequenceCountSuccess, actions.fetchEnrolledSequenceCount, actions.fetchEnrolledSequenceCountPending, actions.fetchEnrolledSequenceCountFailed, ], (state, action) => action.type) const enrollingPlaybookIdByTabId = reducerWithInitialState({} as any) .case(actions.enroll, (state, { tabId, playbookId }) => { state[tabId] = playbookId return state }) .cases([ actions.enrollFailed, actions.enrollSuccess, actions.clearState, ], (state, { tabId }) => omit(state, [tabId])) const editingPlaybookIdByTabId = reducerWithInitialState({} as any) .case(actions.fetchPlaybookForEndUser, (state, { tabId, playbookId }) => { state[tabId] = playbookId return state }) .cases([ actions.fetchPlaybookForEndUserSuccess, actions.fetchPlaybookForEndUserFailed, actions.clearState, ], (state, { tabId }) => omit(state, [tabId])) const playbookForEndUserByTabId = reducerWithInitialState({} as any) .case(actions.fetchPlaybookForEndUserSuccess, (state, { tabId, playbook }) => { state[tabId] = playbook return state }) .case(actions.setPlaybookForEndUserSubject, (state, { tabId, index, subject }) => { const playbook = state[tabId] playbook.actions[0].action.emails[index].title = subject return state }) .case(actions.setPlaybookForEndUserMessage, (state, { tabId, index, message }) => { const playbook = state[tabId] playbook.actions[0].action.emails[index].message = message return state }) .case(actions.clearState, (state, { tabId }) => omit(state, [tabId])) const enrollStatusByEmail = reducerWithInitialState({} as any) .casesWithAction([ actions.enrollFailed, actions.enrollSuccess, actions.enrollPending, ], (state, action) => { state[action.payload.email] = action.type return state }) .case(actions.clearState, (state, { emails }) => omit(state, emails)) const enrollErrorsByEmail = reducerWithInitialState({} as any) .case(actions.enrollFailed, (state, { email, error }) => { state[email] = error return state }) .case(actions.clearState, (state, { emails }) => omit(state, emails)) const fetchPlaybookForEndUserStatusByTabId = reducerWithInitialState({} as any) .casesWithAction([ actions.fetchPlaybookForEndUser, actions.fetchPlaybookForEndUserSuccess, actions.fetchPlaybookForEndUserPending, actions.fetchPlaybookForEndUserFailed, ], (state, action) => { state[action.payload.tabId] = action.type return state }) export default combineReducers({ playbookList, searchPlaybooksStatus, emailsByTabId, enrolledSequenceCount, fetchEnrolledSequenceCountStatus, playbookForEndUserByTabId, fetchPlaybookForEndUserStatusByTabId, enrollErrorsByEmail, enrollStatusByEmail, enrollingPlaybookIdByTabId, editingPlaybookIdByTabId, activeTabId, })