Skip to content

Commit

Permalink
add base vuex/api code for handling categories
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaswh committed Mar 1, 2017
1 parent da59844 commit 9ed4b99
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/app/budgets/api.js
Expand Up @@ -2,6 +2,7 @@ import localforage from 'localforage';
import { processAPIData } from '../../utils';

const BUDGET_NAMESPACE = 'BUDGET-';
const CATEGORY_NAMESPACE = 'CATEGORY-';

export const saveBudget = (budget) => {
budget = Object.assign({}, budget); // clone our object so we can manipulate it before saving
Expand All @@ -27,3 +28,20 @@ export const fetchBudgets = () => {
return budgets;
});
};

export const saveCategory = (category) => {
return localforage.setItem(
CATEGORY_NAMESPACE + category.id,
category
).then((value) => {
return value;
}).catch((err) => {
console.log('category problems abound! ', err);
});
};

export const fetchCategories = () => {
return localforage.startsWith(CATEGORY_NAMESPACE).then((res) => {
return processAPIData(res);
});
};
17 changes: 16 additions & 1 deletion src/app/budgets/vuex/actions.js
@@ -1,6 +1,6 @@
import moment from 'moment';
import { guid } from '../../../utils';
import { saveBudget, fetchBudgets } from '../api';
import { saveBudget, fetchBudgets, saveCategory, fetchCategories } from '../api';

const verifyUniqueMonth = (budgets, budget) => {
// accepts a list of budgets, and the budget being updated
Expand Down Expand Up @@ -43,3 +43,18 @@ export const loadBudgets = ({ state, commit }) => {
});
}
};

export const createCategory = ({ commit, state }, data) => {
let id = guid();
let category = Object.assign({ id: id }, data);
commit('CREATE_CATEGORY', { category: category });
saveCategory(category);
};

export const loadCategories = ({ state, commit }) => {
if (!state.categories || Object.keys(state.categories).length === 0) {
return fetchCategories().then((res) => {
commit('LOAD_CATEGORIES', res);
});
}
};
3 changes: 2 additions & 1 deletion src/app/budgets/vuex/index.js
Expand Up @@ -3,7 +3,8 @@ import mutations from './mutations';
import getters from './getters';

const state = {
budgets: {}
budgets: {},
categories: {}
};

export default {
Expand Down
12 changes: 12 additions & 0 deletions src/app/budgets/vuex/mutations.js
Expand Up @@ -9,5 +9,17 @@ export default {

LOAD_BUDGETS (state, payload) {
state.budgets = payload;
},

CREATE_CATEGORY (state, payload) {
state.categories[payload.category.id] = payload.category;
},

UPDATE_CATEGORY (state, payload) {
state.categories[payload.category.id] = payload.category;
},

LOAD_CATEGORIES (state, payload) {
state.categories = payload;
}
};

0 comments on commit 9ed4b99

Please sign in to comment.