1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/ui/app/reducers/metamask.js

125 lines
3.1 KiB
JavaScript
Raw Normal View History

const extend = require('xtend')
const actions = require('../actions')
module.exports = reduceMetamask
2016-06-21 22:18:32 +02:00
function reduceMetamask (state, action) {
2016-06-21 22:56:04 +02:00
let newState
// clone + defaults
var metamaskState = extend({
isInitialized: false,
isUnlocked: false,
rpcTarget: 'https://rawtestrpc.metamask.io/',
identities: {},
unconfTxs: {},
currentFiat: 'USD',
conversionRate: 0,
conversionDate: 'N/A',
}, state.metamask)
switch (action.type) {
2016-06-21 22:18:32 +02:00
case actions.SHOW_ACCOUNTS_PAGE:
2016-06-21 22:56:04 +02:00
newState = extend(metamaskState)
delete newState.seedWords
return newState
2016-06-21 22:18:32 +02:00
case actions.UPDATE_METAMASK_STATE:
return extend(metamaskState, action.value)
case actions.AGREE_TO_DISCLAIMER:
return extend(metamaskState, {
isDisclaimerConfirmed: true,
2016-06-21 22:18:32 +02:00
})
case actions.UNLOCK_METAMASK:
return extend(metamaskState, {
isUnlocked: true,
isInitialized: true,
selectedAccount: action.value,
})
case actions.LOCK_METAMASK:
return extend(metamaskState, {
isUnlocked: false,
})
case actions.SET_RPC_TARGET:
return extend(metamaskState, {
provider: {
type: 'rpc',
rpcTarget: action.value,
},
})
case actions.SET_PROVIDER_TYPE:
return extend(metamaskState, {
provider: {
type: action.value,
},
})
case actions.COMPLETED_TX:
var stringId = String(action.id)
2016-06-21 22:56:04 +02:00
newState = extend(metamaskState, {
2016-06-21 22:18:32 +02:00
unconfTxs: {},
unconfMsgs: {},
})
2016-06-21 22:56:04 +02:00
for (const id in metamaskState.unconfTxs) {
2016-06-21 22:18:32 +02:00
if (id !== stringId) {
newState.unconfTxs[id] = metamaskState.unconfTxs[id]
}
}
2016-06-21 22:56:04 +02:00
for (const id in metamaskState.unconfMsgs) {
2016-06-21 22:18:32 +02:00
if (id !== stringId) {
newState.unconfMsgs[id] = metamaskState.unconfMsgs[id]
}
}
2016-06-21 22:18:32 +02:00
return newState
case actions.SHOW_NEW_VAULT_SEED:
return extend(metamaskState, {
isUnlocked: true,
isInitialized: false,
})
case actions.CLEAR_SEED_WORD_CACHE:
2016-06-21 22:56:04 +02:00
newState = extend(metamaskState, {
2016-06-21 22:18:32 +02:00
isUnlocked: true,
isInitialized: true,
selectedAccount: action.value,
})
delete newState.seedWords
return newState
case actions.SHOW_ACCOUNT_DETAIL:
2016-06-21 22:56:04 +02:00
newState = extend(metamaskState, {
2016-06-21 22:18:32 +02:00
isUnlocked: true,
isInitialized: true,
selectedAccount: action.value,
})
delete newState.seedWords
return newState
case actions.SAVE_ACCOUNT_LABEL:
const account = action.value.account
const name = action.value.label
var id = {}
id[account] = extend(metamaskState.identities[account], { name })
var identities = extend(metamaskState.identities, id)
return extend(metamaskState, { identities })
case actions.SET_CURRENT_FIAT:
2016-07-22 19:15:39 +02:00
return extend(metamaskState, {
currentFiat: action.value.currentFiat,
conversionRate: action.value.conversionRate,
conversionDate: action.value.conversionDate,
})
2016-06-21 22:18:32 +02:00
default:
return metamaskState
}
}