Split AddEditTransaction into multiple components

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2020-03-15 21:15:43 -06:00
parent 604fe5da33
commit 12936c72ab
10 changed files with 68 additions and 26 deletions

View file

@ -41,6 +41,6 @@ class Transaction extends Entity implements JsonSerializable {
if (!$timestamp) return null; if (!$timestamp) return null;
$datetime = new DateTime(); $datetime = new DateTime();
$datetime->setTimestamp($timestamp); $datetime->setTimestamp($timestamp);
return $datetime->format(DateTime::ATOM); return $datetime->format(DateTime::RFC3339_EXTENDED);
} }
} }

View file

@ -37,7 +37,7 @@ import { mapGetters, mapState } from "vuex";
import { Actions } from "@nextcloud/vue/dist/Components/Actions"; import { Actions } from "@nextcloud/vue/dist/Components/Actions";
import { ActionButton } from "@nextcloud/vue/dist/Components/ActionButton"; import { ActionButton } from "@nextcloud/vue/dist/Components/ActionButton";
import CategoryList from "./CategoryList"; import CategoryList from "./CategoryList";
import TransactionList from "./TransactionList"; import TransactionList from "./transaction/TransactionList";
export default { export default {
name: "budget-details", name: "budget-details",

View file

@ -8,7 +8,7 @@
</template> </template>
<script> <script>
import { mapGetters, mapState } from "vuex"; import { mapGetters, mapState } from "vuex";
import TransactionList from './TransactionList' import TransactionList from './transaction/TransactionList'
export default { export default {
name: "category-details", name: "category-details",

View file

@ -0,0 +1,20 @@
<template>
<TransactionForm :transaction="transaction" />
</template>
<script>
import { mapGetters } from 'vuex'
import TransactionForm from './TransactionForm'
export default {
name: 'edit-transaction',
components: {
TransactionForm
},
computed: {
...mapGetters(['transaction'])
},
mounted() {
this.$store.dispatch("editTransactionViewed", this.$route.params.id);
}
}
</script>

View file

@ -0,0 +1,23 @@
<template>
<TransactionForm :transaction="transaction" />
</template>
<script>
import TransactionForm from "./TransactionForm";
export default {
name: "new-transaction",
components: {
TransactionForm
},
data: function() {
return {
transaction: {
date: new Date(),
expense: true,
budgetId: this.$store.state.currentBudget,
categoryId: this.$store.state.currentCategory
}
};
}
};
</script>

View file

@ -1,6 +1,6 @@
<template> <template>
<div> <div>
<div v-if="!loading && transaction" class="add-edit-transaction"> <div v-if="!loading" class="add-edit-transaction">
<h2>{{ transaction.id ? 'Edit' : 'Add' }} Transaction</h2> <h2>{{ transaction.id ? 'Edit' : 'Add' }} Transaction</h2>
<input v-model="transaction.name" type="text" placeholder="Name" title="Name" /> <input v-model="transaction.name" type="text" placeholder="Name" title="Name" />
<textarea v-model="transaction.description" placeholder="Description" title="Description"></textarea> <textarea v-model="transaction.description" placeholder="Description" title="Description"></textarea>
@ -40,42 +40,39 @@ export default {
}, },
data: function() { data: function() {
return { return {
transaction: Object, saving: false
loading: true
}; };
}, },
props: {
transaction: Object
},
computed: { computed: {
...mapGetters(["budgets"]), ...mapGetters(["budgets"]),
filteredCategories: function(state) { filteredCategories: function(state) {
return this.$store.getters.categories.filter(function(category) { return this.$store.getters.categories.filter(function(category) {
return category.expense === state.transaction.expense; return category.expense === state.transaction.expense;
}); });
} },
loading: state => state.transaction === undefined || state.saving
}, },
methods: { methods: {
updateCategories() { updateCategories() {
if (!this.transaction) return;
this.$store.dispatch( this.$store.dispatch(
"addEditTransactionBudgetSelected", "addEditTransactionBudgetSelected",
this.transaction.budgetId this.transaction.budgetId
); );
}, },
saveTransaction() { saveTransaction() {
this.loading = true this.saving = true;
this.$store.dispatch('addEditTransactionSaveClicked', this.transaction) this.$store.dispatch("addEditTransactionSaveClicked", this.transaction);
} }
}, },
mounted() { mounted() {
if (this.$route.params.id) { let transactionId;
this.transaction = this.$store.getters.transaction(this.$route.params.id); if (this.transaction) {
} else { transactionId = this.transaction.id;
this.transaction = {
date: new Date(),
expense: true,
budgetId: this.$store.state.currentBudget,
categoryId: this.$store.state.currentCategory
};
} }
this.loading = false;
this.updateCategories(); this.updateCategories();
} }
}; };
@ -95,6 +92,6 @@ export default {
margin-right: 1em; margin-right: 1em;
} }
.icon-loading { .icon-loading {
margin-top: 16px; margin-top: 16px;
} }
</style> </style>

View file

@ -2,8 +2,9 @@ import VueRouter from 'vue-router'
import Vue from 'vue' import Vue from 'vue'
import BudgetDetails from '../components/BudgetDetails' import BudgetDetails from '../components/BudgetDetails'
import CategoryDetails from '../components/CategoryDetails' import CategoryDetails from '../components/CategoryDetails'
import AddEditTransaction from '../components/AddEditTransaction' import NewTransaction from '../components/transaction/NewTransaction'
import TransactionDetails from '../components/TransactionDetails' import EditTransaction from '../components/transaction/EditTransaction'
import TransactionDetails from '../components/transaction/TransactionDetails'
Vue.use(VueRouter) Vue.use(VueRouter)
@ -21,7 +22,7 @@ const routes = [
{ {
path: '/transactions/new', path: '/transactions/new',
name: 'newTransaction', name: 'newTransaction',
component: AddEditTransaction, component: NewTransaction,
}, },
{ {
path: '/transactions/:id', path: '/transactions/:id',
@ -31,7 +32,7 @@ const routes = [
{ {
path: '/transactions/:id/edit', path: '/transactions/:id/edit',
name: 'editTransaction', name: 'editTransaction',
component: AddEditTransaction, component: EditTransaction,
}, },
] ]

View file

@ -91,8 +91,9 @@ export default new Vuex.Store({
addTransactionClicked({ commit }) { addTransactionClicked({ commit }) {
router.push({ name: "newTransaction" }) router.push({ name: "newTransaction" })
}, },
addEditTransactionViewed({ commit, state, getters }, transactionId) { editTransactionViewed({ commit, state, getters }, transactionId) {
if (transactionId && getters.transaction(transactionId) === undefined) { commit('setCurrentTransaction', transactionId)
if (transactionId !== undefined && getters.transaction === undefined) {
axios.get(OC.generateUrl(`/apps/twigs/api/v1.0/transactions/${transactionId}`)) axios.get(OC.generateUrl(`/apps/twigs/api/v1.0/transactions/${transactionId}`))
.then((response) => { .then((response) => {
commit('setTransactions', [response.data]) commit('setTransactions', [response.data])