Files
firefly-iii/frontend/src/components/transactions/TransactionAccount.vue

195 lines
6.2 KiB
Vue
Raw Normal View History

<!--
- TransactionAccount.vue
- Copyright (c) 2020 james@firefly-iii.org
-
- This file is part of Firefly III (https://github.com/firefly-iii).
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<template>
<div class="form-group">
2021-02-10 06:55:56 +01:00
<div class="text-xs d-none d-lg-block d-xl-block" v-if="visible">
<span v-if="0 === this.index">{{ $t('firefly.' + this.direction + '_account') }}</span>
<span class="text-warning" v-if="this.index > 0">{{ $t('firefly.first_split_overrules_' + this.direction) }}</span>
</div>
<div class="text-xs d-none d-lg-block d-xl-block" v-if="!visible">
&nbsp;
</div>
<vue-typeahead-bootstrap
2021-02-10 06:55:56 +01:00
v-if="visible"
2021-02-14 19:13:42 +01:00
v-model="accountName"
:data="accounts"
2020-12-30 18:43:16 +01:00
:showOnFocus=true
2021-01-31 07:26:52 +01:00
:inputClass="errors.length > 0 ? 'is-invalid' : ''"
2020-12-30 18:43:16 +01:00
:inputName="direction + '[]'"
:serializer="item => item.name_with_balance"
:minMatchingChars="3"
2021-02-10 06:55:56 +01:00
:placeholder="$t('firefly.' + direction + '_account')"
@input="lookupAccount"
2021-01-17 19:52:53 +01:00
@hit="selectedAccount = $event"
>
<template slot="append">
<div class="input-group-append">
2021-02-10 06:55:56 +01:00
<button tabindex="-1" class="btn btn-outline-secondary" v-on:click="clearAccount" type="button"><i class="far fa-trash-alt"></i></button>
</div>
</template>
</vue-typeahead-bootstrap>
2021-02-10 06:55:56 +01:00
<div class="form-control-static" v-if="!visible">
<span class="small text-muted"><em>{{ $t('firefly.first_split_decides') }}</em></span>
</div>
2021-01-31 07:26:52 +01:00
<span v-if="errors.length > 0">
<span v-for="error in errors" class="text-danger small">{{ error }}<br/></span>
</span>
</div>
</template>
<script>
import VueTypeaheadBootstrap from 'vue-typeahead-bootstrap';
import {debounce} from 'lodash';
import {createNamespacedHelpers} from "vuex";
const {mapState, mapGetters, mapActions, mapMutations} = createNamespacedHelpers('transactions/create')
export default {
name: "TransactionAccount",
components: {VueTypeaheadBootstrap},
2021-01-31 07:26:52 +01:00
props: ['index', 'direction', 'value', 'errors'],
data() {
return {
query: '',
accounts: [],
2020-12-30 18:43:16 +01:00
accountTypes: [],
2021-01-17 19:52:53 +01:00
initialSet: [],
2021-02-14 19:13:42 +01:00
selectedAccount: {},
account: this.value,
accountName: ''
}
},
2020-12-30 18:43:16 +01:00
created() {
this.createInitialSet();
},
methods: {
...mapMutations(
[
'updateField',
2020-12-30 18:43:16 +01:00
'setDestinationAllowedTypes',
'setSourceAllowedTypes'
],
),
2020-12-30 18:43:16 +01:00
...mapActions(
[
'calcTransactionType'
]
),
getACURL: function (types, query) {
2021-02-17 06:46:26 +01:00
let URL = './api/v1/autocomplete/accounts?types=' + types.join(',') + '&query=' + query;
//console.log('AC URL is ' + URL);
return URL;
2020-12-30 18:43:16 +01:00
},
clearAccount: function () {
this.accounts = this.initialSet;
2021-02-14 19:13:42 +01:00
this.account = {name: ''};
this.accountName = '';
2020-12-30 18:43:16 +01:00
},
lookupAccount: debounce(function () {
2021-02-17 06:46:26 +01:00
//console.log('In lookupAccount()');
2020-12-30 18:43:16 +01:00
if (0 === this.accountTypes.length) {
// set the types from the default types for this direction:
2020-12-30 18:43:16 +01:00
this.accountTypes = 'source' === this.direction ? this.sourceAllowedTypes : this.destinationAllowedTypes;
}
2020-12-30 18:43:16 +01:00
// update autocomplete URL:
2021-02-17 06:46:26 +01:00
axios.get(this.getACURL(this.accountTypes, this.accountName))
.then(response => {
2021-02-17 06:46:26 +01:00
//console.log('Got a response!');
this.accounts = response.data;
2021-02-17 06:46:26 +01:00
//console.log(response.data);
})
2020-12-30 18:43:16 +01:00
}, 300),
2021-02-14 19:13:42 +01:00
2020-12-30 18:43:16 +01:00
createInitialSet: function () {
let types = this.sourceAllowedTypes;
if ('destination' === this.direction) {
types = this.destinationAllowedTypes;
}
axios.get(this.getACURL(types, ''))
.then(response => {
this.accounts = response.data;
this.initialSet = response.data;
});
}
},
watch: {
selectedAccount: function (value) {
2021-02-17 06:46:26 +01:00
//console.log('Now in selectedAccount');
//console.log(value);
2021-02-14 19:13:42 +01:00
this.account = value;
2021-02-17 06:46:26 +01:00
this.accountName = this.account.name_with_balance;
2021-01-17 19:52:53 +01:00
},
2021-02-14 19:13:42 +01:00
account: function (value) {
2021-01-17 19:52:53 +01:00
this.updateField({field: this.accountKey, index: this.index, value: value});
2020-12-30 18:43:16 +01:00
// set the opposing account allowed set.
let opposingAccounts = [];
let type = value.type ? value.type : 'no_type';
if ('undefined' !== typeof this.allowedOpposingTypes[this.direction]) {
if ('undefined' !== typeof this.allowedOpposingTypes[this.direction][type]) {
opposingAccounts = this.allowedOpposingTypes[this.direction][type];
}
}
if ('source' === this.direction) {
this.setDestinationAllowedTypes(opposingAccounts);
}
if ('destination' === this.direction) {
this.setSourceAllowedTypes(opposingAccounts);
}
2021-01-17 19:52:53 +01:00
this.calcTransactionType();
2020-12-30 18:43:16 +01:00
},
},
computed: {
...mapGetters([
'transactionType',
2020-12-30 18:43:16 +01:00
'sourceAllowedTypes',
'destinationAllowedTypes',
'allowedOpposingTypes'
]),
2020-12-30 18:43:16 +01:00
accountKey: {
get() {
return 'source' === this.direction ? 'source_account' : 'destination_account';
}
},
2021-02-10 06:55:56 +01:00
visible: {
get() {
// index 0 is always visible:
if (0 === this.index) {
return true;
}
if ('source' === this.direction) {
return 'any' === this.transactionType || 'Deposit' === this.transactionType
}
if ('destination' === this.direction) {
return 'any' === this.transactionType || 'Withdrawal' === this.transactionType;
}
return false;
}
}
}
}
</script>