import { reducerWithInitialState } from 'typescript-fsa-reducers' import { pull, concat, get, map, filter } from 'lodash' import { Settings, IInboxSetting } from './interfaces' import * as UserPropertiesActions from '../userProperties/actions' import * as actions from './actions' export default reducerWithInitialState({ bccStatus: false, bccEmailAddress: '', inboxesToRunExtensionOn: {}, notificationSentMessage: true, notificationVisitedSite: true, notificationClickedEmailLink: true, notificationOpenedEmail: true, notificationMuteList: [], domainMuteList: [], }) .case(actions.setSettingStatus, (state, setting) => ({ ...state, ...setting, })) .case(UserPropertiesActions.fetchUserPropertiesSuccess, (state, userProperties) => { const settingsFromUserProps = get(userProperties, 'extensionSettings', {}) as Settings const domainMuteList = map(settingsFromUserProps.domainMuteList, (domain: any) => typeof domain === 'string' ? ({ domain, date: Date.now() }) : domain) return { ...settingsFromUserProps, domainMuteList } || state }) .case(actions.addEmailToMuteList, (state, email) => { const muteList = state.notificationMuteList const newMuteList = concat(muteList, email) return { ...state, notificationMuteList: newMuteList } }) .case(actions.removeEmailFromMuteList, (state, email) => { const muteList = state.notificationMuteList const newMuteList = pull(muteList, email) return { ...state, notificationMuteList: newMuteList } }) .case(actions.addDomainToMuteList, (state, domain) => { const existingDomainMuteList = state.domainMuteList || [] return { ...state, domainMuteList: [...existingDomainMuteList, { domain, date: Date.now() }], } }) .case(actions.removeDomainFromMuteList, (state, domain) => { const existingDomainMuteList = state.domainMuteList || [] return { ...state, domainMuteList: filter(existingDomainMuteList, domainObj => domainObj.domain !== domain), } }) .case(actions.setInboxesToRunExtensionOn, (state, inboxSetting: IInboxSetting) => { const currentInboxList = state.inboxesToRunExtensionOn return { ...state, inboxesToRunExtensionOn: { ...currentInboxList, ...inboxSetting }, } })