Files
firefly-iii/frontend/src/pages/accounts/Create.vue
2022-12-29 19:43:43 +01:00

220 lines
6.0 KiB
Vue

<!--
- Create.vue
- Copyright (c) 2022 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>
<q-page>
<div class="row q-mx-md">
<div class="col-12">
<q-banner v-if="'' !== errorMessage" class="bg-orange text-white" inline-actions rounded>
{{ errorMessage }}
<template v-slot:action>
<q-btn flat label="Dismiss" @click="dismissBanner"/>
</template>
</q-banner>
</div>
</div>
<div class="row q-mx-md q-mt-md">
<div class="col-12">
<q-card bordered>
<q-card-section>
<div class="text-h6">Info for {{ $route.params.type }} {{ index }}</div>
</q-card-section>
<q-card-section>
<div class="row">
<div class="col-12 q-mb-xs">
<q-input
v-model="name"
:disable="disabledInput"
:error="hasSubmissionErrors.name"
:error-message="submissionErrors.name"
:label="$t('form.name')" bottom-slots clearable outlined
type="text"/>
</div>
</div>
<div class="row">
<div class="col-12 q-mb-xs">
<q-input
v-model="iban"
:disable="disabledInput"
:error="hasSubmissionErrors.iban"
:error-message="submissionErrors.iban" :label="$t('form.iban')" bottom-slots clearable mask="AA## XXXX XXXX XXXX XXXX XXXX XXXX XXXX XX" outlined
type="text"/>
</div>
</div>
</q-card-section>
</q-card>
</div>
</div>
<div class="row q-mx-md">
<div class="col-12">
<q-card class="q-mt-xs">
<q-card-section>
<div class="row">
<div class="col-12 text-right">
<q-btn :disable="disabledInput" color="primary" label="Submit" @click="submitAccount"/>
</div>
</div>
<div class="row">
<div class="col-12 text-right">
<q-checkbox v-model="doReturnHere" :disable="disabledInput" label="Return here to create another one"
left-label/>
<br/>
<q-checkbox v-model="doResetForm" :disable="!doReturnHere || disabledInput" label="Reset form after submission"
left-label/>
</div>
</div>
</q-card-section>
</q-card>
</div>
</div>
</q-page>
</template>
<script>
import Post from "../../api/accounts/post";
export default {
name: "Create",
data() {
return {
submissionErrors: {},
hasSubmissionErrors: {},
submitting: false,
doReturnHere: false,
doResetForm: false,
errorMessage: '',
type: '',
// account fields:
name: '',
iban: '',
}
},
computed: {
disabledInput: function () {
return this.submitting;
}
},
created() {
this.resetForm();
this.type = this.$route.params.type;
},
methods: {
resetForm: function () {
this.name = '';
this.iban = '';
this.resetErrors();
},
resetErrors: function () {
this.submissionErrors =
{
name: '',
iban: '',
};
this.hasSubmissionErrors = {
name: false,
iban: false,
};
},
submitAccount: function () {
this.submitting = true;
this.errorMessage = '';
// reset errors:
this.resetErrors();
// build account array
const submission = this.buildAccount();
let accounts = new Post();
accounts
.post(submission)
.catch(this.processErrors)
.then(this.processSuccess);
},
buildAccount: function () {
let act = {
name: this.name,
iban: this.iban,
type: this.type,
};
if ('asset' === this.type) {
act.account_role = 'defaultAsset';
}
return act;
},
dismissBanner: function () {
this.errorMessage = '';
},
processSuccess: function (response) {
if (!response) {
return;
}
this.submitting = false;
let message = {
level: 'success',
text: 'I am new account lol',
show: true,
action: {
show: true,
text: 'Go to account',
link: {name: 'accounts.show', params: {id: parseInt(response.data.data.id)}}
}
};
// store flash
this.$q.localStorage.set('flash', message);
if (this.doReturnHere) {
window.dispatchEvent(new CustomEvent('flash', {
detail: {
flash: this.$q.localStorage.getItem('flash')
}
}));
}
if (!this.doReturnHere) {
// return to previous page.
this.$router.go(-1);
}
},
processErrors: function (error) {
if (error.response) {
let errors = error.response.data; // => the response payload
this.errorMessage = errors.message;
console.log(errors);
for (let i in errors.errors) {
if (errors.errors.hasOwnProperty(i)) {
this.submissionErrors[i] = errors.errors[i][0];
this.hasSubmissionErrors[i] = true;
}
}
}
this.submitting = false;
},
}
}
</script>
<style scoped>
</style>