Skip to content

Commit 91820c8

Browse files
committed
user can now duplicate one budget object onto another
1 parent 0e8972c commit 91820c8

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

src/app/budgets/components/CreateUpdateBudget.vue

+41-5
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,20 @@
7676
</tbody>
7777
<tfoot>
7878
<tr>
79-
<td></td>
79+
<td>
80+
Copy entire budget from:
81+
<select
82+
class="select"
83+
@change="processDuplicateBudget($event.target.value)"
84+
>
85+
<option
86+
v-for="value, key in budgets"
87+
:value="key"
88+
>
89+
{{ value.month | moment }}
90+
</option>
91+
</select>
92+
</td>
8093
<td>${{ selectedBudget.budgeted }}</td>
8194
<td>${{ selectedBudget.spent }}</td>
8295
<td>${{ selectedBudget.budgeted - selectedBudget.spent }}</td>
@@ -92,11 +105,12 @@
92105
</template>
93106

94107
<script>
95-
import { mapActions, mapGetters } from 'vuex';
108+
import { mapActions, mapGetters, mapState } from 'vuex';
96109
import Datepicker from 'vuejs-datepicker';
97110
98111
import CreateUpdateBudgetCategory from './CreateUpdateBudgetCategory';
99112
import BudgetCategory from './BudgetCategory';
113+
import { moment } from '../../../filters';
100114
101115
export default {
102116
name: 'budget-create-edit-view',
@@ -111,10 +125,15 @@ export default {
111125
return {
112126
selectedBudget: {},
113127
editing: false,
114-
activeBudgetCategory: null
128+
activeBudgetCategory: null,
129+
lastBudget: null
115130
};
116131
},
117132
133+
filters: {
134+
moment
135+
},
136+
118137
mounted () {
119138
if ('budgetId' in this.$route.params) {
120139
this.loadBudgets().then(() => {
@@ -125,6 +144,7 @@ export default {
125144
}
126145
});
127146
}
147+
this.lastBudget = this.getBudgetById('d0862d23-e433-bb30-6088-09f0e2f9f221');
128148
},
129149
130150
methods: {
@@ -133,7 +153,8 @@ export default {
133153
'updateBudget',
134154
'loadBudgets',
135155
'createBudgetCategory',
136-
'updateBudgetCategory'
156+
'updateBudgetCategory',
157+
'duplicateBudget'
137158
]),
138159
139160
resetAndGo () {
@@ -189,14 +210,29 @@ export default {
189210
190211
budgetCategoryComponent (budgetCategory) {
191212
return this.activeBudgetCategory && this.activeBudgetCategory === budgetCategory ? 'create-update-budget-category' : 'budget-category';
213+
},
214+
215+
processDuplicateBudget (budgetId) {
216+
if (confirm('Are you sure you want to duplicate this budget? Doing this will overwrite all existing data for this month (transaction data will NOT be erased).')) {
217+
this.duplicateBudget({
218+
budget: this.selectedBudget,
219+
baseBudget: this.getBudgetById(budgetId)
220+
}).then((budget) => {
221+
this.selectedBudget = budget;
222+
});
223+
}
192224
}
193225
},
194226
195227
computed: {
196228
...mapGetters([
197229
'getBudgetById',
198230
'getCategoryById'
199-
])
231+
]),
232+
233+
...mapState({
234+
'budgets': state => state.budgets.budgets
235+
})
200236
}
201237
};
202238
</script>

src/app/budgets/vuex/actions.js

+38
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,44 @@ export const updateBudget = ({ commit, state }, data) => {
4040
saveBudget(data);
4141
};
4242

43+
export const duplicateBudget = ({ commit, dispatch, getters, state }, data) => {
44+
/*
45+
* Expects an existing budget object, budget, and an budget to be copied, baseBudget
46+
* Duplicates all budget categories and budgeted amounts to the new budget
47+
*/
48+
if (!(data.budget && data.baseBudget)) return Promise.reject(new Error('Incorrect data sent to duplicateBudget'));
49+
50+
// clone our object in case we received something from the store
51+
let budget = Object.assign({}, data.budget);
52+
53+
// let's reset some information first
54+
budget.budgeted = 0;
55+
budget.budgetCategories = null;
56+
// note that we don't reset the spent or income because we aren't
57+
// changing any transactions, which are what determine those values
58+
59+
commit('UPDATE_BUDGET', { budget: budget });
60+
61+
budget = getters.getBudgetById(budget.id);
62+
63+
if ('budgetCategories' in data.baseBudget) {
64+
Object.keys(data.baseBudget.budgetCategories).forEach((key) => {
65+
dispatch('createBudgetCategory', {
66+
budget: budget,
67+
budgetCategory: {
68+
category: data.baseBudget.budgetCategories[key].category,
69+
budgeted: data.baseBudget.budgetCategories[key].budgeted,
70+
spent: 0 // TODO: grab this value when we have transactions!
71+
}
72+
});
73+
});
74+
}
75+
76+
saveBudget(budget);
77+
78+
return budget;
79+
};
80+
4381
export const loadBudgets = ({ state, commit }) => {
4482
if (!state.budgets || Object.keys(state.budgets).length === 0) {
4583
return fetchBudgets().then((res) => {

0 commit comments

Comments
 (0)