This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement multiple account management
- Loading branch information
Showing
8 changed files
with
120 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
export const getSavedAccount = () => { | ||
export const getSavedAccounts = () => { | ||
const savedAccounts = localStorage.getItem('accounts'); | ||
let account; | ||
let accounts = []; | ||
if (savedAccounts) { | ||
account = JSON.parse(savedAccounts); | ||
accounts = JSON.parse(savedAccounts); | ||
} | ||
|
||
return account; | ||
return accounts; | ||
}; | ||
|
||
export const getLastActiveAccount = () => ( | ||
getSavedAccounts()[localStorage.getItem('lastActiveAccountIndex') || 0] | ||
); | ||
|
||
export const setLastActiveAccount = ({ publicKey, network, address }) => { | ||
const lastActiveAccountIndex = getSavedAccounts().findIndex(account => ( | ||
account.publicKey === publicKey && | ||
account.network === network && | ||
account.address === address | ||
)); | ||
if (lastActiveAccountIndex > -1) { | ||
localStorage.setItem('lastActiveAccountIndex', lastActiveAccountIndex); | ||
} | ||
}; | ||
|
||
export const setSavedAccount = ({ publicKey, network, address }) => { | ||
localStorage.setItem('accounts', JSON.stringify([{ | ||
publicKey, | ||
network, | ||
address, | ||
}])); | ||
localStorage.setItem('accounts', JSON.stringify([ | ||
...getSavedAccounts(), | ||
{ | ||
publicKey, | ||
network, | ||
address, | ||
}, | ||
])); | ||
}; | ||
|
||
export const removeSavedAccount = (publicKey) => { | ||
let accounts = localStorage.getItem('accounts'); | ||
accounts = JSON.parse(accounts); | ||
accounts = accounts.filter(account => account.publicKey !== publicKey); | ||
export const removeSavedAccount = ({ network, publicKey }) => { | ||
const accounts = getSavedAccounts().filter(account => | ||
!(account.publicKey === publicKey && account.network === network)); | ||
localStorage.setItem('accounts', JSON.stringify(accounts)); | ||
}; |