Skip to content

Commit da59844

Browse files
committed
dates are stored in proper json format and re-objectified when fetched; reject the user if they try to save two budgets with the same month
1 parent e2c8ae5 commit da59844

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

src/app/budgets/api.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { processAPIData } from '../../utils';
44
const BUDGET_NAMESPACE = 'BUDGET-';
55

66
export const saveBudget = (budget) => {
7+
budget = Object.assign({}, budget); // clone our object so we can manipulate it before saving
8+
budget.month = budget.month.toJSON();
9+
710
return localforage.setItem(
811
BUDGET_NAMESPACE + budget.id,
912
budget
@@ -16,6 +19,11 @@ export const saveBudget = (budget) => {
1619

1720
export const fetchBudgets = () => {
1821
return localforage.startsWith(BUDGET_NAMESPACE).then((res) => {
19-
return processAPIData(res);
22+
let budgets = processAPIData(res);
23+
Object.keys(budgets).forEach((o) => {
24+
budgets[o].month = new Date(budgets[o].month);
25+
});
26+
27+
return budgets;
2028
});
2129
};

src/app/budgets/components/BudgetsList.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<router-link :to="{ name: 'accountsList' }">View accounts</router-link>
77

88
<ul>
9-
<li v-for="budget, key in budgets">
9+
<li v-for="budget in sortedBudgets">
1010
{{ budget.month | moment }}
1111
${{ budget.budgeted }}
1212
${{ budget.spent }}
@@ -42,7 +42,17 @@ export default {
4242
computed: {
4343
...mapState({
4444
'budgets': state => state.budgets.budgets
45-
})
45+
}),
46+
47+
sortedBudgets () {
48+
let sortedKeys = Object.keys(this.budgets).sort((a, b) => {
49+
return this.budgets[b].month - this.budgets[a].month;
50+
});
51+
52+
return sortedKeys.map((key) => {
53+
return this.budgets[key];
54+
});
55+
}
4656
}
4757
};
4858
</script>

src/app/budgets/components/CreateUpdateBudget.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,16 @@ export default {
7474
saveNewBudget () {
7575
this.createBudget(this.selectedBudget).then(() => {
7676
this.resetAndGo();
77+
}).catch((err) => {
78+
alert(err);
7779
});
7880
},
7981
8082
saveBudget () {
8183
this.updateBudget(this.selectedBudget).then(() => {
8284
this.resetAndGo();
85+
}).catch((err) => {
86+
alert(err);
8387
});
8488
},
8589

src/app/budgets/vuex/actions.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1+
import moment from 'moment';
12
import { guid } from '../../../utils';
23
import { saveBudget, fetchBudgets } from '../api';
34

4-
export const createBudget = ({ commit }, data) => {
5+
const verifyUniqueMonth = (budgets, budget) => {
6+
// accepts a list of budgets, and the budget being updated
7+
// returns true if there is no date collision
8+
// returns false if a budget already exists in budgets with the same month as budget
9+
let month = moment(budget.month);
10+
return !Object.values(budgets).find((o) => {
11+
if (o.id === budget.id) return false; // it's the budget we're examining, let's not check if the months are the same
12+
return month.isSame(o.month, 'month');
13+
});
14+
};
15+
16+
export const createBudget = ({ commit, state }, data) => {
17+
if (!verifyUniqueMonth(state.budgets, data)) {
18+
return Promise.reject(new Error('A budget already exists for this month.'));
19+
}
20+
521
let id = guid();
622
let budget = Object.assign({ id: id }, data);
723

@@ -11,15 +27,19 @@ export const createBudget = ({ commit }, data) => {
1127
});
1228
};
1329

14-
export const updateBudget = ({ commit }, data) => {
30+
export const updateBudget = ({ commit, state }, data) => {
31+
if (!verifyUniqueMonth(state.budgets, data)) {
32+
return Promise.reject(new Error('A budget already exists for this month.'));
33+
}
34+
1535
commit('UPDATE_BUDGET', { budget: data });
1636
saveBudget(data);
1737
};
1838

19-
export const loadBudgets = (state) => {
39+
export const loadBudgets = ({ state, commit }) => {
2040
if (!state.budgets || Object.keys(state.budgets).length === 0) {
2141
return fetchBudgets().then((res) => {
22-
state.commit('LOAD_BUDGETS', res);
42+
commit('LOAD_BUDGETS', res);
2343
});
2444
}
2545
};

0 commit comments

Comments
 (0)