Skip to content

Commit 23182d1

Browse files
committed
refactor code that processes IndexedDB format to app usable format out into its own utility function & perform conversion in API layer
1 parent 324fbe9 commit 23182d1

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/app/accounts/api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import localforage from 'localforage';
2+
import { processAPIData } from '../../utils';
23

34
const ACCOUNT_NAMESPACE = 'ACCOUNT-';
45

56
export const fetchAccounts = () => {
67
return localforage.startsWith(ACCOUNT_NAMESPACE).then((res) => {
7-
return res;
8+
return processAPIData(res);
89
});
910
};
1011

src/app/accounts/vuex/actions.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ export const loadAccounts = (state) => {
2424
// loads accounts only if they are not already loaded
2525
if (!state.accounts || Object.keys(state.accounts).length === 0) {
2626
return fetchAccounts().then((res) => {
27-
let accounts = {};
28-
Object.keys(res).forEach((key) => { accounts[res[key].id] = res[key]; });
29-
state.commit('LOAD_ACCOUNTS', accounts);
27+
state.commit('LOAD_ACCOUNTS', res);
3028
});
3129
}
3230
};

src/utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@ export const guid = function () {
88
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
99
s4() + '-' + s4() + s4() + s4();
1010
};
11+
12+
export const processAPIData = function (data) {
13+
/*
14+
Converts the data formatted for IndexedDB / API into the format
15+
our application uses.
16+
*/
17+
let res = {};
18+
Object.keys(data).forEach((key) => { res[data[key].id] = data[key]; });
19+
return res;
20+
};

0 commit comments

Comments
 (0)