Skip to content

Commit 9ed4b99

Browse files
committed
add base vuex/api code for handling categories
1 parent da59844 commit 9ed4b99

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

src/app/budgets/api.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import localforage from 'localforage';
22
import { processAPIData } from '../../utils';
33

44
const BUDGET_NAMESPACE = 'BUDGET-';
5+
const CATEGORY_NAMESPACE = 'CATEGORY-';
56

67
export const saveBudget = (budget) => {
78
budget = Object.assign({}, budget); // clone our object so we can manipulate it before saving
@@ -27,3 +28,20 @@ export const fetchBudgets = () => {
2728
return budgets;
2829
});
2930
};
31+
32+
export const saveCategory = (category) => {
33+
return localforage.setItem(
34+
CATEGORY_NAMESPACE + category.id,
35+
category
36+
).then((value) => {
37+
return value;
38+
}).catch((err) => {
39+
console.log('category problems abound! ', err);
40+
});
41+
};
42+
43+
export const fetchCategories = () => {
44+
return localforage.startsWith(CATEGORY_NAMESPACE).then((res) => {
45+
return processAPIData(res);
46+
});
47+
};

src/app/budgets/vuex/actions.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import moment from 'moment';
22
import { guid } from '../../../utils';
3-
import { saveBudget, fetchBudgets } from '../api';
3+
import { saveBudget, fetchBudgets, saveCategory, fetchCategories } from '../api';
44

55
const verifyUniqueMonth = (budgets, budget) => {
66
// accepts a list of budgets, and the budget being updated
@@ -43,3 +43,18 @@ export const loadBudgets = ({ state, commit }) => {
4343
});
4444
}
4545
};
46+
47+
export const createCategory = ({ commit, state }, data) => {
48+
let id = guid();
49+
let category = Object.assign({ id: id }, data);
50+
commit('CREATE_CATEGORY', { category: category });
51+
saveCategory(category);
52+
};
53+
54+
export const loadCategories = ({ state, commit }) => {
55+
if (!state.categories || Object.keys(state.categories).length === 0) {
56+
return fetchCategories().then((res) => {
57+
commit('LOAD_CATEGORIES', res);
58+
});
59+
}
60+
};

src/app/budgets/vuex/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import mutations from './mutations';
33
import getters from './getters';
44

55
const state = {
6-
budgets: {}
6+
budgets: {},
7+
categories: {}
78
};
89

910
export default {

src/app/budgets/vuex/mutations.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,17 @@ export default {
99

1010
LOAD_BUDGETS (state, payload) {
1111
state.budgets = payload;
12+
},
13+
14+
CREATE_CATEGORY (state, payload) {
15+
state.categories[payload.category.id] = payload.category;
16+
},
17+
18+
UPDATE_CATEGORY (state, payload) {
19+
state.categories[payload.category.id] = payload.category;
20+
},
21+
22+
LOAD_CATEGORIES (state, payload) {
23+
state.categories = payload;
1224
}
1325
};

0 commit comments

Comments
 (0)