Permalink
Browse files

add base vuex/api code for handling categories

  • Loading branch information...
matthiaswh committed Mar 1, 2017
1 parent da59844 commit 9ed4b9982c6f81da4a56c1b8ad9332e9ee87e4d7
Showing with 48 additions and 2 deletions.
  1. +18 −0 src/app/budgets/api.js
  2. +16 −1 src/app/budgets/vuex/actions.js
  3. +2 −1 src/app/budgets/vuex/index.js
  4. +12 −0 src/app/budgets/vuex/mutations.js
@@ -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
@@ -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);
});
};
@@ -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
@@ -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,7 +3,8 @@ import mutations from './mutations';
import getters from './getters';
const state = {
budgets: {}
budgets: {},
categories: {}
};
export default {
@@ -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.