Use Vuex more correctly to load categories on budget details page

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2020-03-13 21:24:57 -05:00
parent 8c41c2c67d
commit a5e349e776
6 changed files with 103 additions and 26 deletions

View file

@ -1,26 +1,39 @@
<template> <template>
<div> <div v-if="budget">
<h1 v-if="budget" v-text="budget.name"></h1> <h1 v-text="budget.name"></h1>
<div class="card"> <div class="card">
<CategoryList v-bind:budget-id="budget.id" v-bind:expense="true"></CategoryList>
</div>
<div class="card">
<CategoryList v-bind:budget-id="budget.id" v-bind:expense="false"></CategoryList>
</div> </div>
</div> </div>
</template> </template>
<script> <script>
import { mapGetters } from "vuex"; import { mapGetters } from "vuex";
import CategoryList from "./CategoryList";
export default { export default {
name: "budget-details", name: "budget-details",
components: { components: {
CategoryList
}, },
computed: { computed: {
budget: function(state) { budget: function(state) {
const budgetId = this.$route.params.id const budgetId = this.$route.params.id;
const budget = this.$store.getters.budgets.find(budget => budget.id === budgetId); const budget = this.$store.getters.budgets.find(
budget => budget.id === budgetId
);
return budget; return budget;
} }
}, },
methods: { methods: {
load() {
this.$store.dispatch("budgetDetailsViewed", this.$route.params.id);
}
},
mounted() {
this.load()
} }
}; };
</script> </script>

View file

@ -1,6 +1,11 @@
<template> <template>
<div> <div>
<AppNavigationItem v-for="budget in budgets" :key="budget.id" :title="budget.name" v-on:click="view(budget.id)"></AppNavigationItem> <AppNavigationItem
v-for="budget in budgets"
:key="budget.id"
:title="budget.name"
v-on:click="view(budget.id)"
></AppNavigationItem>
<AppNavigationNew text="New Budget"></AppNavigationNew> <AppNavigationNew text="New Budget"></AppNavigationNew>
</div> </div>
</template> </template>
@ -13,18 +18,22 @@ export default {
name: "budget-list", name: "budget-list",
components: { components: {
AppNavigationItem, AppNavigationItem,
AppNavigationNew AppNavigationNew,
}, },
computed: { computed: {
...mapGetters(["budgets"]) ...mapGetters(["budgets"])
}, },
methods: { methods: {
view: function(id) { load: function() {
this.$router.push({ name: 'budgetDetails', params: { id: id } }) this.$store.dispatch('budgetListViewed')
}, },
new: function() { view: function(id) {
this.$router.push({ name: "budgetDetails", params: { id: id } })
} },
new: function() {}
},
mounted() {
this.load()
} }
}; };
</script> </script>

View file

@ -0,0 +1,29 @@
<template>
<ul>
<li v-for="category in categories" :key="category.id">
<p>{{ category.name }}</p>
</li>
</ul>
</template>
<script>
import { mapGetters } from "vuex";
export default {
name: "category-list",
components: {},
props: {
budgetId: Number,
expense: Boolean
},
computed: {
categories: function(state) {
const categories = this.$store.getters.categories(this.budgetId)
if (categories) {
return categories.filter(category => category.expense === this.expense)
} else {
return [];
}
}
}
};
</script>

View file

@ -11,5 +11,5 @@ Vue.prototype.OCA = window.OCA
new Vue({ new Vue({
router, router,
store, store,
render: h => h(App) render: h => h(App),
}).$mount('#twigs-app') }).$mount('#twigs-app')

View file

@ -2,13 +2,13 @@ import VueRouter from 'vue-router'
import Vue from 'vue' import Vue from 'vue'
import BudgetDetails from '../components/BudgetDetails' import BudgetDetails from '../components/BudgetDetails'
Vue.use(VueRouter); Vue.use(VueRouter)
const routes = [ const routes = [
{ {
path: '/budgets/:id', path: '/budgets/:id',
name: 'budgetDetails', name: 'budgetDetails',
component: BudgetDetails component: BudgetDetails,
} }
] ]

View file

@ -8,23 +8,49 @@ export default new Vuex.Store({
state: { state: {
budgets: [], budgets: [],
currentBudget: 0, currentBudget: 0,
categories: [], categories: {},
currentCategory: 0, currentCategory: 0,
transactions: [], transactions: [],
currentTransaction: 0 currentTransaction: 0,
}, },
getters: { getters: {
budgets: (state) => { budgets: (state) => {
if (state.budgets.length === 0) {
axios.get(OC.generateUrl("/apps/twigs/api/v1.0/budgets"))
.then(function (response) {
state.budgets = response.data;
})
}
return state.budgets return state.budgets
}, },
budget: (state) => (id) => { budget: (state) => (id) => {
return state.budgets.find(budget => budget.id === id) return state.budgets.find(budget => budget.id === id)
},
categories: (state) => (budgetId) => {
return state.categories[budgetId]
},
},
actions: {
budgetListViewed({ commit }) {
axios.get(OC.generateUrl('/apps/twigs/api/v1.0/budgets'))
.then(function (response) {
commit('setBudgets', response.data)
})
},
budgetDetailsViewed({ commit }, budgetId) {
axios.get(OC.generateUrl(`/apps/twigs/api/v1.0/categories?budgetId=${budgetId}`))
.then(function (response) {
commit({
type: 'setCategories',
budgetId: budgetId,
categories: response.data
})
})
}
},
mutations: {
setBudgets(state, budgets) {
state.budgets = budgets
},
setCategories(state, data) {
state.categories = {
...state.categories,
[data.budgetId]: data.categories
}
} }
} }
}) })