Permalink
Browse files

refactor code that processes IndexedDB format to app usable format ou…

…t into its own utility function & perform conversion in API layer
  • Loading branch information...
matthiaswh committed Feb 21, 2017
1 parent 324fbe9 commit 23182d133116f7266508a7362a0739c0f81d8589
Showing with 13 additions and 4 deletions.
  1. +2 −1 src/app/accounts/api.js
  2. +1 −3 src/app/accounts/vuex/actions.js
  3. +10 −0 src/utils.js
@@ -1,10 +1,11 @@
import localforage from 'localforage';
import { processAPIData } from '../../utils';
const ACCOUNT_NAMESPACE = 'ACCOUNT-';
export const fetchAccounts = () => {
return localforage.startsWith(ACCOUNT_NAMESPACE).then((res) => {
return res;
return processAPIData(res);
});
};
@@ -24,9 +24,7 @@ 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);
state.commit('LOAD_ACCOUNTS', res);
});
}
};
@@ -8,3 +8,13 @@ export const guid = function () {
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
};
export const processAPIData = function (data) {
/*
Converts the data formatted for IndexedDB / API into the format
our application uses.
*/
let res = {};
Object.keys(data).forEach((key) => { res[data[key].id] = data[key]; });
return res;
};

0 comments on commit 23182d1

Please sign in to comment.