From 02947db565519ddee7da77fd3e0c52f9cd365dc2 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Tue, 17 Mar 2020 21:47:53 -0600 Subject: [PATCH] Implement budget & category creation & updating Signed-off-by: William Brawner --- src/App.vue | 4 +- src/components/BudgetDetails.vue | 113 -------------------- src/components/budget/BudgetDetails.vue | 112 +++++++++++++++++++ src/components/budget/BudgetForm.vue | 61 +++++++++++ src/components/{ => budget}/BudgetList.vue | 6 +- src/components/budget/EditBudget.vue | 20 ++++ src/components/budget/NewBudget.vue | 18 ++++ src/components/category/CategoryDetails.vue | 83 ++++++++------ src/router/index.js | 14 ++- src/store/index.js | 42 +++++++- 10 files changed, 319 insertions(+), 154 deletions(-) delete mode 100644 src/components/BudgetDetails.vue create mode 100644 src/components/budget/BudgetDetails.vue create mode 100644 src/components/budget/BudgetForm.vue rename src/components/{ => budget}/BudgetList.vue (81%) create mode 100644 src/components/budget/EditBudget.vue create mode 100644 src/components/budget/NewBudget.vue diff --git a/src/App.vue b/src/App.vue index 35d5e5b..b718a96 100644 --- a/src/App.vue +++ b/src/App.vue @@ -13,7 +13,7 @@ import AppNavigation from "@nextcloud/vue/dist/Components/AppNavigation"; import AppNavigationSettings from "@nextcloud/vue/dist/Components/AppNavigationSettings"; import AppContent from "@nextcloud/vue/dist/Components/AppContent"; -import BudgetList from "./components/BudgetList"; +import BudgetList from "./components/budget/BudgetList"; import axios from "@nextcloud/axios"; export default { @@ -34,4 +34,4 @@ export default { width: 100%; } - \ No newline at end of file + diff --git a/src/components/BudgetDetails.vue b/src/components/BudgetDetails.vue deleted file mode 100644 index a201456..0000000 --- a/src/components/BudgetDetails.vue +++ /dev/null @@ -1,113 +0,0 @@ - - - \ No newline at end of file diff --git a/src/components/budget/BudgetDetails.vue b/src/components/budget/BudgetDetails.vue new file mode 100644 index 0000000..09f89dc --- /dev/null +++ b/src/components/budget/BudgetDetails.vue @@ -0,0 +1,112 @@ + + + diff --git a/src/components/budget/BudgetForm.vue b/src/components/budget/BudgetForm.vue new file mode 100644 index 0000000..b3c4f35 --- /dev/null +++ b/src/components/budget/BudgetForm.vue @@ -0,0 +1,61 @@ + + + diff --git a/src/components/BudgetList.vue b/src/components/budget/BudgetList.vue similarity index 81% rename from src/components/BudgetList.vue rename to src/components/budget/BudgetList.vue index 91e6ada..8656d7d 100644 --- a/src/components/BudgetList.vue +++ b/src/components/budget/BudgetList.vue @@ -1,6 +1,6 @@ + diff --git a/src/components/budget/NewBudget.vue b/src/components/budget/NewBudget.vue new file mode 100644 index 0000000..406ed80 --- /dev/null +++ b/src/components/budget/NewBudget.vue @@ -0,0 +1,18 @@ + + diff --git a/src/components/category/CategoryDetails.vue b/src/components/category/CategoryDetails.vue index e29b7cb..2a261e3 100644 --- a/src/components/category/CategoryDetails.vue +++ b/src/components/category/CategoryDetails.vue @@ -1,43 +1,62 @@ diff --git a/src/router/index.js b/src/router/index.js index 212c698..829f0cf 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,6 +1,8 @@ import VueRouter from 'vue-router' import Vue from 'vue' -import BudgetDetails from '../components/BudgetDetails' +import NewBudget from '../components/budget/NewBudget' +import EditBudget from '../components/budget/EditBudget' +import BudgetDetails from '../components/budget/BudgetDetails' import NewCategory from '../components/category/NewCategory' import EditCategory from '../components/category/EditCategory' import CategoryDetails from '../components/category/CategoryDetails' @@ -11,11 +13,21 @@ import TransactionDetails from '../components/transaction/TransactionDetails' Vue.use(VueRouter) const routes = [ + { + path: '/budgets/new', + name: 'newBudget', + component: NewBudget, + }, { path: '/budgets/:id', name: 'budgetDetails', component: BudgetDetails, }, + { + path: '/budgets/:id/edit', + name: 'editBudget', + component: EditBudget, + }, { path: '/categories/new', name: 'newCategory', diff --git a/src/store/index.js b/src/store/index.js index 0d51f9a..0d4a7ef 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -18,12 +18,10 @@ export default new Vuex.Store({ }, getters: { budgets: (state) => state.budgets, - budget: (state) => (id) => state.budgets.find(budget => budget.id === id), + budget: (state) => state.budgets.find(budget => budget.id === state.currentBudget), budgetBalance: (state) => (id) => state.budgetBalances[id], categories: (state) => state.categories, - category: (state) => (id) => { - return state.categories.find(category => category.id === id) - }, + category: (state) => state.categories.find(category => category.id === state.currentCategory), categoryBalance: (state) => (categoryId) => { return state.categoryBalances[categoryId]; }, @@ -35,6 +33,9 @@ export default new Vuex.Store({ transaction: (state) => state.transactions.find(transaction => transaction.id === state.currentTransaction), }, actions: { + addBudgetClicked({ commit }) { + router.push({ name: "newBudget" }) + }, budgetListViewed({ commit }) { axios.get(OC.generateUrl('/apps/twigs/api/v1.0/budgets')) .then(function (response) { @@ -53,6 +54,27 @@ export default new Vuex.Store({ budgetClicked({ commit }, budgetId) { router.push({ name: "budgetDetails", params: { id: budgetId } }) }, + editBudgetViewed({ commit, state, getters }, budgetId) { + commit('setCurrentBudget', budgetId) + if (budgetId !== undefined && getters.budget === undefined) { + axios.get(OC.generateUrl(`/apps/twigs/api/v1.0/budgets/${budgetId}`)) + .then((response) => { + commit('setBudgets', [response.data]) + }) + } + }, + budgetFormSaveClicked({ commit }, budget) { + let request; + if (budget.id) { + request = axios.put(OC.generateUrl(`/apps/twigs/api/v1.0/budgets/${budget.id}`), budget) + } else { + request = axios.post(OC.generateUrl(`/apps/twigs/api/v1.0/budgets`), budget) + } + request.then(response => { + commit('addBudget', response.data) + router.push({ name: "budgetDetails", params: { id: response.data.id } }) + }) + }, budgetDetailsViewed({ commit }, budgetId) { commit('setCurrentBudget', budgetId) commit('setCategories', []) @@ -74,12 +96,18 @@ export default new Vuex.Store({ axios.get(OC.generateUrl(`/apps/twigs/api/v1.0/transactions?budgetId=${budgetId}?count=10`)) .then((response) => commit('setTransactions', response.data)) }, + editBudgetClicked({ commit }, budgetId) { + router.push({ name: "editBudget" , params: { id: budgetId } }) + }, categoryClicked({ commit }, categoryId) { router.push({ name: "categoryDetails", params: { id: categoryId } }) }, addCategoryClicked({ commit }) { router.push({ name: "newCategory" }) }, + editCategoryClicked({ commit }, categoryId) { + router.push({ name: "editCategory" , params: { id: categoryId } }) + }, editCategoryViewed({ commit, state, getters }, categoryId) { commit('setCurrentCategory', categoryId) if (categoryId !== undefined && getters.category === undefined) { @@ -174,6 +202,12 @@ export default new Vuex.Store({ }, }, mutations: { + addBudget(state, budget) { + state.budgets = [ + ...state.budgets.filter(b => b.id !== budget.id), + budget + ] + }, setCurrentBudget(state, budgetId) { state.currentBudget = Number.parseInt(budgetId) },