import { createAction , createSelector , createSlice , PayloadAction } from '@reduxjs/toolkit' ; import { put , select , takeLatest } from 'redux-saga/effects' ; import { Fiats } from '@config' ; import { LSKeys , TFiatTicker , TUuid } from '@types' ; import { equals , findIndex , uniq } from '@vendor' ; import { initialLegacyState } from './legacy.initialState' ; import { getAppState } from './selectors' ; const sliceName = LSKeys . SETTINGS ; export const initialState = initialLegacyState [ sliceName ] ; const slice = createSlice ( { name : sliceName , initialState , reducers : { addCurrent ( state , action ) { state . dashboardAccounts . push ( action . payload ) ; } , addCurrents ( state , action ) { state . dashboardAccounts = uniq ( [ ... state . dashboardAccounts , ... action . payload ] ) ; } , resetCurrentsTo ( state , action ) { state . dashboardAccounts = action . payload ; } , addExcludedAsset ( state , action ) { state . excludedAssets . push ( action . payload ) ; } , removeExcludedAsset ( state , action ) { const idx = findIndex ( equals ( action . payload ) , state . excludedAssets ) ; state . excludedAssets . splice ( idx , 1 ) ; } , setLanguage ( state , action ) { state . language = action . payload ; } , setFiat ( state , action ) { state . fiatCurrency = action . payload ; } , setDemoMode ( state , action ) { state . isDemoMode = action . payload ; } , setProductAnalyticsAuthorisation ( state , action ) { state . canTrackProductAnalytics = action . payload ; } , setAnalyticsUserID ( state , action ) { state . analyticsUserID = action . payload ; } } } ) ; export const { addCurrent , addCurrents , resetCurrentsTo , setLanguage , setFiat , addExcludedAsset , removeExcludedAsset , setDemoMode , setProductAnalyticsAuthorisation , setAnalyticsUserID } = slice . actions ; export default slice ; /**
 * Selectors
 */ export const getSettings = createSelector ( getAppState , ( s ) => s . settings ) ; export const getCurrents = createSelector ( getSettings , ( s ) => s . dashboardAccounts ) ; export const getLanguage = createSelector ( getSettings , ( s ) => s . language ) ; export const getFiat = createSelector ( getSettings , ( s ) => s . fiatCurrency ) ; export const getFiatInformation = createSelector ( getSettings , ( s ) => Fiats [ s . fiatCurrency ] ) ; export const getExcludedAssets = createSelector ( getSettings , ( s ) => s . excludedAssets ) ; export const getIsDemoMode = createSelector ( getSettings , ( s ) => s . isDemoMode ) ; export const canTrackProductAnalytics = createSelector ( getSettings , ( s ) => s . canTrackProductAnalytics ) ; export const getAnalyticsUserID = createSelector ( getSettings , ( s ) => s . analyticsUserID ) ; /**
 * Actions
 */ export const addAccountsToCurrents = createAction < TUuid [ ] > ( `${ slice . name }/addAccountsToCurrents` ) ; /**
 * Sagas
 */ export function * settingsSaga ( ) { yield takeLatest ( addAccountsToCurrents . type , handleAddAccountsToCurrents ) ; } export function * handleAddAccountsToCurrents ( { payload } ) { const isDemoMode = yield select ( getIsDemoMode ) ; if ( isDemoMode ) { yield put ( slice . actions . setDemoMode ( false ) ) ; yield put ( slice . actions . resetCurrentsTo ( payload ) ) ; } else { yield put ( slice . actions . addCurrents ( payload ) ) ; } }