Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
now saving and retrieving accounts from localstorage!
- Loading branch information
1 parent
6c3e206
commit f144a6e
Showing
7 changed files
with
85 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import localforage from 'localforage'; | ||
|
||
const ACCOUNT_NAMESPACE = 'ACCOUNT-'; | ||
|
||
export const fetchAccounts = () => { | ||
return localforage.startsWith(ACCOUNT_NAMESPACE).then((res) => { | ||
return res; | ||
}); | ||
}; | ||
|
||
export const saveAccount = (account) => { | ||
return localforage.setItem( | ||
ACCOUNT_NAMESPACE + account.id, | ||
account | ||
).then((value) => { | ||
return value; | ||
}).catch((err) => { | ||
console.log('oops! the account was too far gone, there was nothing we could do to save him ', err); | ||
}); | ||
}; | ||
|
||
export const removeAccount = (account) => { | ||
return localforage.removeItem( | ||
ACCOUNT_NAMESPACE + account.id | ||
).then(() => { | ||
return true; | ||
}).catch((err) => { | ||
console.log(err); | ||
return false; | ||
}); | ||
}; |
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,11 +1,32 @@ | ||
import { guid } from '../../../utils'; | ||
import { removeAccount, saveAccount, fetchAccounts } from '../api'; | ||
|
||
export const addAccount = ({ commit }, data) => { | ||
commit('ADD_ACCOUNT', { account: data }); | ||
let id = guid(); | ||
let account = Object.assign({ id: id }, data); | ||
commit('ADD_ACCOUNT', {account: account}); | ||
saveAccount(account).then((value) => { | ||
// we've saved the account, what now | ||
}); | ||
}; | ||
|
||
export const updateAccount = ({ commit }, data) => { | ||
commit('UPDATE_ACCOUNT', { account: data }); | ||
commit('UPDATE_ACCOUNT', {account: data}); | ||
saveAccount(data); | ||
}; | ||
|
||
export const deleteAccount = ({ commit }, data) => { | ||
commit('DELETE_ACCOUNT', { account: data }); | ||
removeAccount(data); | ||
}; | ||
|
||
export const loadAccounts = (state) => { | ||
// loads accounts only if they are not already loaded | ||
if (!state.accounts || Object.keys(state.accounts).length === 0) { | ||
return fetchAccounts().then((res) => { | ||
let accounts = {}; | ||
Object.keys(res).forEach((key) => { accounts[res[key].id] = res[key]; }); | ||
state.commit('LOAD_ACCOUNTS', accounts); | ||
}); | ||
} | ||
}; |
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