From 3ebf029c04b15599f571a19524474df9680efa7d Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 3 Jan 2017 10:39:34 -0800 Subject: [PATCH 1/5] Update account list after adding account Fixed by finally making a function generator for a pattern we use frequently, communicating to the background process. Fixes #961 --- ui/app/actions.js | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/ui/app/actions.js b/ui/app/actions.js index 606460314..018baff73 100644 --- a/ui/app/actions.js +++ b/ui/app/actions.js @@ -153,7 +153,7 @@ var actions = { SHOW_NEW_KEYCHAIN: 'SHOW_NEW_KEYCHAIN', showNewKeychain: showNewKeychain, - + callBackgroundThenUpdate, } module.exports = actions @@ -269,15 +269,7 @@ function addNewKeyring (type, opts) { } function addNewAccount (ringNumber = 0) { - return (dispatch) => { - dispatch(actions.showLoadingIndication()) - background.addNewAccount(ringNumber, (err) => { - dispatch(this.hideLoadingIndication()) - if (err) { - return dispatch(actions.showWarning(err)) - } - }) - } + return callBackgroundThenUpdate(background.addNewAccount, ringNumber) } function showInfoPage () { @@ -476,6 +468,7 @@ function updateMetamaskState (newState) { function lockMetamask () { return (dispatch) => { + dispatch(actions.showLoadingIndication()) background.setLocked((err, newState) => { dispatch(actions.hideLoadingIndication()) if (err) { @@ -857,3 +850,24 @@ function shapeShiftRequest (query, options, cb) { return shapShiftReq.send() } } + +// Call Background Then Update +// +// A function generator for a common pattern wherein: +// We show loading indication. +// We call a background method. +// We hide loading indication. +// If it errored, we show a warning. +// If it didn't, we update the state. +function callBackgroundThenUpdate (method, ...args) { + return (dispatch) => { + dispatch(actions.showLoadingIndication()) + method.call(background, ...args, (err, newState) => { + dispatch(actions.hideLoadingIndication()) + if (err) { + return dispatch(actions.displayWarning(err.message)) + } + dispatch(actions.updateMetamaskState(newState)) + }) + } +} From 8b7b097034f274631917b2df8617c24feb09f69c Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 3 Jan 2017 10:42:09 -0800 Subject: [PATCH 2/5] Apply new pattern to repetitive functions --- ui/app/actions.js | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/ui/app/actions.js b/ui/app/actions.js index 018baff73..bc9b4a092 100644 --- a/ui/app/actions.js +++ b/ui/app/actions.js @@ -231,7 +231,6 @@ function createNewVaultAndKeychain (password) { if (err) { return dispatch(actions.showWarning(err.message)) } - dispatch(actions.updateMetamaskState(newState)) }) } } @@ -467,16 +466,7 @@ function updateMetamaskState (newState) { } function lockMetamask () { - return (dispatch) => { - dispatch(actions.showLoadingIndication()) - background.setLocked((err, newState) => { - dispatch(actions.hideLoadingIndication()) - if (err) { - return dispatch(actions.displayWarning(err.message)) - } - dispatch(actions.updateMetamaskState(newState)) - }) - } + return callBackgroundThenUpdate(background.setLocked) } function showAccountDetail (address) { @@ -586,14 +576,7 @@ function clearNotices () { } function markAccountsFound() { - return (dispatch) => { - dispatch(this.showLoadingIndication()) - background.markAccountsFound((err, newState) => { - dispatch(this.hideLoadingIndication()) - if (err) return dispatch(this.showWarning(err.message)) - dispatch(actions.updateMetamaskState(newState)) - }) - } + return callBackgroundThenUpdate(background.markAccountsFound) } // From 013e6a608f55c7a81e17a171d681cb21e041c9d9 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 3 Jan 2017 11:03:25 -0800 Subject: [PATCH 3/5] Corrected instances of showWarning to displayWarning --- ui/app/actions.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ui/app/actions.js b/ui/app/actions.js index bc9b4a092..96d76a50a 100644 --- a/ui/app/actions.js +++ b/ui/app/actions.js @@ -229,7 +229,7 @@ function createNewVaultAndKeychain (password) { return (dispatch) => { background.createNewVaultAndKeychain(password, (err, newState) => { if (err) { - return dispatch(actions.showWarning(err.message)) + return dispatch(actions.displayWarning(err.message)) } }) } @@ -261,7 +261,7 @@ function addNewKeyring (type, opts) { background.addNewKeyring(type, opts, (err) => { dispatch(this.hideLoadingIndication()) if (err) { - return dispatch(actions.showWarning(err)) + return dispatch(actions.displayWarning(err)) } }) } @@ -548,7 +548,7 @@ function markNoticeRead (notice) { background.markNoticeRead(notice, (err, notice) => { dispatch(this.hideLoadingIndication()) if (err) { - return dispatch(actions.showWarning(err)) + return dispatch(actions.displayWarning(err)) } if (notice) { return dispatch(actions.showNotice(notice)) From 10c818abac0901724cf7d98bf63728bdeadcdbed Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 3 Jan 2017 11:30:27 -0800 Subject: [PATCH 4/5] Restore missing line --- ui/app/actions.js | 1 + 1 file changed, 1 insertion(+) diff --git a/ui/app/actions.js b/ui/app/actions.js index 96d76a50a..2ffa352a4 100644 --- a/ui/app/actions.js +++ b/ui/app/actions.js @@ -231,6 +231,7 @@ function createNewVaultAndKeychain (password) { if (err) { return dispatch(actions.displayWarning(err.message)) } + dispatch(actions.updateMetamaskState(newState)) }) } } From 2c2cdc4475b0ac8a6a52186a470a49c6a085ca08 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Tue, 3 Jan 2017 11:33:15 -0800 Subject: [PATCH 5/5] Convert more actions to new simple pattern --- ui/app/actions.js | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/ui/app/actions.js b/ui/app/actions.js index 2ffa352a4..d63d36f19 100644 --- a/ui/app/actions.js +++ b/ui/app/actions.js @@ -226,14 +226,7 @@ function createNewVaultAndRestore (password, seed) { } function createNewVaultAndKeychain (password) { - return (dispatch) => { - background.createNewVaultAndKeychain(password, (err, newState) => { - if (err) { - return dispatch(actions.displayWarning(err.message)) - } - dispatch(actions.updateMetamaskState(newState)) - }) - } + return callBackgroundThenUpdate(background.createNewVaultAndKeychain, password) } function revealSeedConfirmation () { @@ -255,17 +248,8 @@ function requestRevealSeed (password) { } } - function addNewKeyring (type, opts) { - return (dispatch) => { - dispatch(actions.showLoadingIndication()) - background.addNewKeyring(type, opts, (err) => { - dispatch(this.hideLoadingIndication()) - if (err) { - return dispatch(actions.displayWarning(err)) - } - }) - } + return callBackgroundThenUpdate(background.addNewKeyring, type, opts) } function addNewAccount (ringNumber = 0) {