/
actions.js
60 lines (51 loc) · 1.92 KB
/
actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import moment from 'moment';
import { guid } from '../../../utils';
import { saveBudget, fetchBudgets, saveCategory, fetchCategories } from '../api';
const verifyUniqueMonth = (budgets, budget) => {
// accepts a list of budgets, and the budget being updated
// returns true if there is no date collision
// returns false if a budget already exists in budgets with the same month as budget
let month = moment(budget.month);
return !Object.values(budgets).find((o) => {
if (o.id === budget.id) return false; // it's the budget we're examining, let's not check if the months are the same
return month.isSame(o.month, 'month');
});
};
export const createBudget = ({ commit, state }, data) => {
if (!verifyUniqueMonth(state.budgets, data)) {
return Promise.reject(new Error('A budget already exists for this month.'));
}
let id = guid();
let budget = Object.assign({ id: id }, data);
commit('CREATE_BUDGET', { budget: budget });
saveBudget(budget).then((value) => {
// we saved the budget, what's next?
});
};
export const updateBudget = ({ commit, state }, data) => {
if (!verifyUniqueMonth(state.budgets, data)) {
return Promise.reject(new Error('A budget already exists for this month.'));
}
commit('UPDATE_BUDGET', { budget: data });
saveBudget(data);
};
export const loadBudgets = ({ state, commit }) => {
if (!state.budgets || Object.keys(state.budgets).length === 0) {
return fetchBudgets().then((res) => {
commit('LOAD_BUDGETS', res);
});
}
};
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);
});
}
};