Skip to content

Commit

Permalink
refactor code that processes IndexedDB format to app usable format ou…
Browse files Browse the repository at this point in the history
…t into its own utility function & perform conversion in API layer
  • Loading branch information
matthiaswh committed Feb 21, 2017
1 parent 324fbe9 commit 23182d1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/app/accounts/api.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);
});
};

Expand Down
4 changes: 1 addition & 3 deletions src/app/accounts/vuex/actions.js
Expand Up @@ -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);
});
}
};
10 changes: 10 additions & 0 deletions src/utils.js
Expand Up @@ -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.