migrate account edit page to composition API and typescript

This commit is contained in:
MaysWind
2025-01-24 00:42:37 +08:00
parent eca0574e41
commit 8fce3f2bcc
4 changed files with 666 additions and 704 deletions
@@ -1,19 +1,19 @@
<template>
<v-dialog :width="account.type === allAccountTypes.MultiSubAccounts.type ? 1000 : 800" :persistent="!!persistent" v-model="showState">
<v-dialog :width="account.type === AccountType.MultiSubAccounts.type ? 1000 : 800" :persistent="!!persistent" v-model="showState">
<v-card class="pa-2 pa-sm-4 pa-md-8">
<template #title>
<div class="d-flex align-center justify-center">
<div class="d-flex w-100 align-center justify-center">
<h4 class="text-h4">{{ $t(title) }}</h4>
<h4 class="text-h4">{{ tt(title) }}</h4>
<v-progress-circular indeterminate size="22" class="ml-2" v-if="loading"></v-progress-circular>
</div>
<v-btn density="comfortable" color="default" variant="text" class="ml-2" :icon="true"
:disabled="loading || submitting || !!editAccountId || account.type !== allAccountTypes.MultiSubAccounts.type">
:disabled="loading || submitting || !!editAccountId || account.type !== AccountType.MultiSubAccounts.type">
<v-icon :icon="icons.more" />
<v-menu activator="parent">
<v-list>
<v-list-item :prepend-icon="icons.add"
:title="$t('Add Sub-account')"
:title="tt('Add Sub-account')"
@click="addSubAccount"></v-list-item>
</v-list>
</v-menu>
@@ -21,14 +21,14 @@
</div>
</template>
<v-card-text class="d-flex flex-column flex-md-row mt-md-4 pt-0">
<div class="mb-4" v-if="account.type === allAccountTypes.MultiSubAccounts.type">
<div class="mb-4" v-if="account.type === AccountType.MultiSubAccounts.type">
<v-tabs direction="vertical" :disabled="loading || submitting" v-model="currentAccountIndex">
<v-tab :value="-1">
<span>{{ $t('Main Account') }}</span>
<span>{{ tt('Main Account') }}</span>
</v-tab>
<template v-if="account.type === allAccountTypes.MultiSubAccounts.type">
<template v-if="account.type === AccountType.MultiSubAccounts.type">
<v-tab :key="idx" :value="idx" v-for="(subAccount, idx) in subAccounts">
<span>{{ $t('Sub Account') + ' #' + (idx + 1) }}</span>
<span>{{ tt('Sub Account') + ' #' + (idx + 1) }}</span>
<v-btn class="ml-2" color="error" size="24" variant="text"
:icon="icons.delete" v-if="!editAccountId"
@click="removeSubAccount(subAccount)"></v-btn>
@@ -38,21 +38,21 @@
</div>
<v-window class="d-flex flex-grow-1 disable-tab-transition w-100-window-container"
:class="{ 'ml-md-5': account.type === allAccountTypes.MultiSubAccounts.type }"
:class="{ 'ml-md-5': account.type === AccountType.MultiSubAccounts.type }"
v-model="activeTab">
<v-window-item value="account">
<v-form class="mt-2">
<v-row>
<v-col cols="12" md="12" v-if="account.type === allAccountTypes.SingleAccount.type || currentAccountIndex < 0">
<v-col cols="12" md="12" v-if="account.type === AccountType.SingleAccount.type || currentAccountIndex < 0">
<v-select
item-title="displayName"
item-value="type"
persistent-placeholder
:disabled="loading || submitting"
:label="$t('Account Category')"
:placeholder="$t('Account Category')"
:label="tt('Account Category')"
:placeholder="tt('Account Category')"
:items="allAccountCategories"
:no-data-text="$t('No results')"
:no-data-text="tt('No results')"
v-model="selectedAccount.category"
>
<template #item="{ props, item }">
@@ -71,16 +71,16 @@
</template>
</v-select>
</v-col>
<v-col cols="12" md="12" v-if="account.type === allAccountTypes.SingleAccount.type || currentAccountIndex < 0">
<v-col cols="12" md="12" v-if="account.type === AccountType.SingleAccount.type || currentAccountIndex < 0">
<v-select
item-title="displayName"
item-value="type"
persistent-placeholder
:disabled="loading || submitting || !!editAccountId"
:label="$t('Account Type')"
:placeholder="$t('Account Type')"
:items="allAccountTypesArray"
:no-data-text="$t('No results')"
:label="tt('Account Type')"
:placeholder="tt('Account Type')"
:items="allAccountTypes"
:no-data-text="tt('No results')"
v-model="selectedAccount.type"
/>
</v-col>
@@ -89,36 +89,36 @@
type="text"
persistent-placeholder
:disabled="loading || submitting"
:label="currentAccountIndex < 0 ? $t('Account Name') : $t('Sub-account Name')"
:placeholder="currentAccountIndex < 0 ? $t('Your account name') : $t('Your sub-account name')"
:label="currentAccountIndex < 0 ? tt('Account Name') : tt('Sub-account Name')"
:placeholder="currentAccountIndex < 0 ? tt('Your account name') : tt('Your sub-account name')"
v-model="selectedAccount.name"
/>
</v-col>
<v-col cols="12" md="6">
<icon-select icon-type="account"
:all-icon-infos="allAccountIcons"
:label="currentAccountIndex < 0 ? $t('Account Icon') : $t('Sub-account Icon')"
:all-icon-infos="ALL_ACCOUNT_ICONS"
:label="currentAccountIndex < 0 ? tt('Account Icon') : tt('Sub-account Icon')"
:color="selectedAccount.color"
:disabled="loading || submitting"
v-model="selectedAccount.icon" />
</v-col>
<v-col cols="12" md="6">
<color-select :all-color-infos="allAccountColors"
:label="currentAccountIndex < 0 ? $t('Account Color') : $t('Sub-account Color')"
<color-select :all-color-infos="ALL_ACCOUNT_COLORS"
:label="currentAccountIndex < 0 ? tt('Account Color') : tt('Sub-account Color')"
:disabled="loading || submitting"
v-model="selectedAccount.color" />
</v-col>
<v-col cols="12" :md="currentAccountIndex < 0 && isAccountSupportCreditCardStatementDate() ? 6 : 12" v-if="account.type === allAccountTypes.SingleAccount.type || currentAccountIndex >= 0">
<v-col cols="12" :md="currentAccountIndex < 0 && isAccountSupportCreditCardStatementDate ? 6 : 12" v-if="account.type === AccountType.SingleAccount.type || currentAccountIndex >= 0">
<v-autocomplete
item-title="displayName"
item-value="currencyCode"
auto-select-first
persistent-placeholder
:disabled="loading || submitting || !!editAccountId"
:label="$t('Currency')"
:placeholder="$t('Currency')"
:label="tt('Currency')"
:placeholder="tt('Currency')"
:items="allCurrencies"
:no-data-text="$t('No results')"
:no-data-text="tt('No results')"
v-model="selectedAccount.currency"
>
<template #append-inner>
@@ -126,37 +126,37 @@
</template>
</v-autocomplete>
</v-col>
<v-col cols="12" :md="account.type === allAccountTypes.SingleAccount.type || currentAccountIndex >= 0 ? 6 : 12" v-if="currentAccountIndex < 0 && isAccountSupportCreditCardStatementDate()">
<v-col cols="12" :md="account.type === AccountType.SingleAccount.type || currentAccountIndex >= 0 ? 6 : 12" v-if="currentAccountIndex < 0 && isAccountSupportCreditCardStatementDate">
<v-autocomplete
item-title="displayName"
item-value="day"
auto-select-first
persistent-placeholder
:disabled="loading || submitting"
:label="$t('Statement Date')"
:placeholder="$t('Statement Date')"
:label="tt('Statement Date')"
:placeholder="tt('Statement Date')"
:items="allAvailableMonthDays"
:no-data-text="$t('No results')"
:no-data-text="tt('No results')"
v-model="account.creditCardStatementDate"
></v-autocomplete>
</v-col>
<v-col cols="12" :md="!editAccountId && selectedAccount.balance ? 6 : 12"
v-if="account.type === allAccountTypes.SingleAccount.type || currentAccountIndex >= 0">
v-if="account.type === AccountType.SingleAccount.type || currentAccountIndex >= 0">
<amount-input :disabled="loading || submitting || !!editAccountId"
:persistent-placeholder="true"
:currency="selectedAccount.currency"
:show-currency="true"
:label="currentAccountIndex < 0 ? $t('Account Balance') : $t('Sub-account Balance')"
:placeholder="currentAccountIndex < 0 ? $t('Account Balance') : $t('Sub-account Balance')"
:label="currentAccountIndex < 0 ? tt('Account Balance') : tt('Sub-account Balance')"
:placeholder="currentAccountIndex < 0 ? tt('Account Balance') : tt('Sub-account Balance')"
v-model="selectedAccount.balance"/>
</v-col>
<v-col cols="12" md="6" v-show="selectedAccount.balance"
v-if="!editAccountId && (account.type === allAccountTypes.SingleAccount.type || currentAccountIndex >= 0)">
v-if="!editAccountId && (account.type === AccountType.SingleAccount.type || currentAccountIndex >= 0)">
<date-time-select
:disabled="loading || submitting"
:label="$t('Balance Time')"
:label="tt('Balance Time')"
v-model="selectedAccount.balanceTime"
@error="showDateTimeError" />
@error="onShowDateTimeError" />
</v-col>
<v-col cols="12" md="12">
<v-textarea
@@ -164,14 +164,14 @@
persistent-placeholder
rows="3"
:disabled="loading || submitting"
:label="$t('Description')"
:placeholder="currentAccountIndex < 0 ? $t('Your account description (optional)') : $t('Your sub-account description (optional)')"
:label="tt('Description')"
:placeholder="currentAccountIndex < 0 ? tt('Your account description (optional)') : tt('Your sub-account description (optional)')"
v-model="selectedAccount.comment"
/>
</v-col>
<v-col class="py-0" cols="12" md="12" v-if="editAccountId">
<v-switch :disabled="loading || submitting"
:label="$t('Visible')" v-model="selectedAccount.visible"/>
:label="tt('Visible')" v-model="selectedAccount.visible"/>
</v-col>
</v-row>
</v-form>
@@ -181,11 +181,11 @@
<v-card-text class="overflow-y-visible">
<div class="w-100 d-flex justify-center mt-2 mt-sm-4 mt-md-6 gap-4">
<v-btn :disabled="isInputEmpty() || loading || submitting" @click="save">
{{ $t(saveButtonTitle) }}
{{ tt(saveButtonTitle) }}
<v-progress-circular indeterminate size="22" class="ml-2" v-if="submitting"></v-progress-circular>
</v-btn>
<v-btn color="secondary" variant="tonal"
:disabled="loading || submitting" @click="cancel">{{ $t('Cancel') }}</v-btn>
:disabled="loading || submitting" @click="cancel">{{ tt('Cancel') }}</v-btn>
</div>
</v-card-text>
</v-card>
@@ -195,14 +195,21 @@
<snack-bar ref="snackbar" />
</template>
<script>
import { mapStores } from 'pinia';
import { useSettingsStore } from '@/stores/setting.ts';
<script setup lang="ts">
import ConfirmDialog from '@/components/desktop/ConfirmDialog.vue';
import SnackBar from '@/components/desktop/SnackBar.vue';
import { ref, computed, useTemplateRef, watch } from 'vue';
import { useI18n } from '@/locales/helpers.ts';
import { useAccountEditPageBaseBase } from '@/views/base/accounts/AccountEditPageBase.ts';
import { useAccountsStore } from '@/stores/account.ts';
import { AccountType, AccountCategory } from '@/core/account.ts';
import { AccountType } from '@/core/account.ts';
import { ALL_ACCOUNT_ICONS } from '@/consts/icon.ts';
import { ALL_ACCOUNT_COLORS } from '@/consts/color.ts';
import type { Account } from '@/models/account.ts';
import { isNumber } from '@/lib/common.ts';
import { generateRandomUUID } from '@/lib/misc.ts';
@@ -214,290 +221,176 @@ import {
mdiDeleteOutline
} from '@mdi/js';
export default {
props: [
'persistent',
'show'
],
expose: [
'open'
],
data() {
const accountsStore = useAccountsStore();
const newAccount = accountsStore.generateNewAccountModel();
return {
showState: false,
activeTab: 'account',
editAccountId: null,
clientSessionId: '',
loading: false,
account: newAccount,
subAccounts: [],
currentAccountIndex: -1,
submitting: false,
resolve: null,
reject: null,
icons: {
more: mdiDotsVertical,
add: mdiCreditCardPlusOutline,
delete: mdiDeleteOutline
}
};
},
computed: {
...mapStores(useSettingsStore, useAccountsStore),
title() {
if (!this.editAccountId) {
return 'Add Account';
} else {
return 'Edit Account';
}
},
saveButtonTitle() {
if (!this.editAccountId) {
return 'Add';
} else {
return 'Save';
}
},
allAccountTypes() {
return AccountType.all();
},
allAccountCategories() {
return this.$locale.getAllAccountCategories();
},
allAccountTypesArray() {
return this.$locale.getAllAccountTypes();
},
allAccountIcons() {
return ALL_ACCOUNT_ICONS;
},
allAccountColors() {
return ALL_ACCOUNT_COLORS;
},
allCurrencies() {
return this.$locale.getAllCurrencies();
},
allAvailableMonthDays() {
const allAvailableDays = [];
allAvailableDays.push({
day: 0,
displayName: this.$t('Not set'),
});
for (let i = 1; i <= 28; i++) {
allAvailableDays.push({
day: i,
displayName: this.$locale.getMonthdayShortName(i),
});
}
return allAvailableDays;
},
selectedAccount() {
if (this.currentAccountIndex < 0) {
return this.account;
}
return this.subAccounts[this.currentAccountIndex];
}
},
watch: {
'account.category': function (newValue, oldValue) {
this.chooseSuitableIcon(oldValue, newValue);
},
'account.type': function () {
if (this.subAccounts.length < 1) {
this.addSubAccount();
}
}
},
methods: {
open(options) {
const self = this;
self.showState = true;
self.loading = true;
self.submitting = false;
const newAccount = self.accountsStore.generateNewAccountModel();
self.account.from(newAccount);
self.subAccounts = [];
self.currentAccountIndex = -1;
if (options && options.id) {
if (options.currentAccount) {
self.setAccount(options.currentAccount);
}
self.editAccountId = options.id;
self.accountsStore.getAccount({
accountId: self.editAccountId
}).then(account => {
self.setAccount(account);
self.loading = false;
}).catch(error => {
self.loading = false;
self.showState = false;
if (!error.processed) {
if (self.reject) {
self.reject(error);
}
}
});
} else {
if (isNumber(options.category)) {
self.account.category = options.category;
self.chooseSuitableIcon(1, options.category);
}
self.editAccountId = null;
self.clientSessionId = generateRandomUUID();
self.loading = false;
}
return new Promise((resolve, reject) => {
self.resolve = resolve;
self.reject = reject;
});
},
addSubAccount() {
if (this.account.type !== this.allAccountTypes.MultiSubAccounts.type) {
return;
}
const subAccount = this.accountsStore.generateNewSubAccountModel(this.account);
this.subAccounts.push(subAccount);
},
removeSubAccount(subAccount) {
const self = this;
self.$refs.confirmDialog.open('Are you sure you want to remove this sub-account?').then(() => {
for (let i = 0; i < self.subAccounts.length; i++) {
if (self.subAccounts[i] === subAccount) {
self.subAccounts.splice(i, 1);
if (self.currentAccountIndex >= self.subAccounts.length) {
self.currentAccountIndex = self.subAccounts.length - 1;
}
}
}
});
},
save() {
const self = this;
let problemMessage = self.getInputEmptyProblemMessage(self.account, false);
if (!problemMessage && self.account.type === self.allAccountTypes.MultiSubAccounts.type) {
for (let i = 0; i < self.subAccounts.length; i++) {
problemMessage = self.getInputEmptyProblemMessage(self.subAccounts[i], true);
if (problemMessage) {
break;
}
}
}
if (problemMessage) {
self.$refs.snackbar.showMessage(problemMessage);
return;
}
self.submitting = true;
self.accountsStore.saveAccount({
account: self.account,
subAccounts: self.subAccounts,
isEdit: !!self.editAccountId,
clientSessionId: self.clientSessionId
}).then(() => {
self.submitting = false;
let message = 'You have saved this account';
if (!self.editAccountId) {
message = 'You have added a new account';
}
if (self.resolve) {
self.resolve({
message: message
});
}
self.showState = false;
}).catch(error => {
self.submitting = false;
if (!error.processed) {
self.$refs.snackbar.showError(error);
}
});
},
cancel() {
if (this.reject) {
this.reject();
}
this.showState = false;
},
isAccountSupportCreditCardStatementDate() {
return this.account && this.account.category === AccountCategory.CreditCard.type;
},
chooseSuitableIcon(oldCategory, newCategory) {
setAccountSuitableIcon(this.account, oldCategory, newCategory);
},
isInputEmpty() {
const isAccountEmpty = !!this.getInputEmptyProblemMessage(this.account, false);
if (isAccountEmpty) {
return true;
}
if (this.account.type === this.allAccountTypes.MultiSubAccounts.type) {
for (let i = 0; i < this.subAccounts.length; i++) {
const isSubAccountEmpty = !!this.getInputEmptyProblemMessage(this.subAccounts[i], true);
if (isSubAccountEmpty) {
return true;
}
}
}
return false;
},
getInputEmptyProblemMessage(account, isSubAccount) {
if (!isSubAccount && !account.category) {
return 'Account category cannot be blank';
} else if (!isSubAccount && !account.type) {
return 'Account type cannot be blank';
} else if (!account.name) {
return 'Account name cannot be blank';
} else if (account.type === this.allAccountTypes.SingleAccount.type && !account.currency) {
return 'Account currency cannot be blank';
} else {
return null;
}
},
setAccount(account) {
this.account.from(account);
this.subAccounts = [];
if (account.childrenAccounts && account.childrenAccounts.length > 0) {
for (let i = 0; i < account.childrenAccounts.length; i++) {
const subAccount = this.accountsStore.generateNewSubAccountModel(this.account);
subAccount.from(account.childrenAccounts[i]);
this.subAccounts.push(subAccount);
}
}
},
showDateTimeError(error) {
this.$refs.snackbar.showError(error);
}
}
interface AccountEditResponse {
message: string;
}
type ConfirmDialogType = InstanceType<typeof ConfirmDialog>;
type SnackBarType = InstanceType<typeof SnackBar>;
defineProps<{
persistent?: boolean;
show?: boolean;
}>();
const { tt } = useI18n();
const {
editAccountId,
clientSessionId,
loading,
submitting,
account,
subAccounts,
title,
saveButtonTitle,
allAccountCategories,
allAccountTypes,
allCurrencies,
allAvailableMonthDays,
isAccountSupportCreditCardStatementDate,
isInputEmpty,
getAccountOrSubAccountProblemMessage,
addSubAccount,
setAccount
} = useAccountEditPageBaseBase();
const accountsStore = useAccountsStore();
const icons = {
more: mdiDotsVertical,
add: mdiCreditCardPlusOutline,
delete: mdiDeleteOutline
};
const confirmDialog = useTemplateRef<ConfirmDialogType>('confirmDialog');
const snackbar = useTemplateRef<SnackBarType>('snackbar');
const showState = ref<boolean>(false);
const activeTab = ref<string>('account');
const currentAccountIndex = ref<number>(-1);
const selectedAccount = computed<Account>(() => {
if (currentAccountIndex.value < 0) {
return account.value;
}
return subAccounts.value[currentAccountIndex.value];
});
let resolveFunc: ((value: AccountEditResponse) => void) | null = null;
let rejectFunc: ((reason?: unknown) => void) | null = null;
function open(options?: { id?: string, currentAccount?: Account, category?: number }): Promise<AccountEditResponse> {
showState.value = true;
loading.value = true;
submitting.value = false;
const newAccount = accountsStore.generateNewAccountModel();
account.value.from(newAccount);
subAccounts.value = [];
currentAccountIndex.value = -1;
if (options && options.id) {
if (options.currentAccount) {
setAccount(options.currentAccount);
}
editAccountId.value = options.id;
accountsStore.getAccount({
accountId: editAccountId.value
}).then(response => {
setAccount(response);
loading.value = false;
}).catch(error => {
loading.value = false;
showState.value = false;
if (!error.processed) {
snackbar.value?.showError(error);
}
});
} else {
if (options && isNumber(options.category)) {
account.value.category = options.category;
setAccountSuitableIcon(account.value, 1, options.category);
}
editAccountId.value = null;
clientSessionId.value = generateRandomUUID();
loading.value = false;
}
return new Promise<AccountEditResponse>((resolve, reject) => {
resolveFunc = resolve;
rejectFunc = reject;
});
}
function save(): void {
const problemMessage = getAccountOrSubAccountProblemMessage();
if (problemMessage) {
snackbar.value?.showMessage(problemMessage);
return;
}
submitting.value = true;
accountsStore.saveAccount({
account: account.value,
subAccounts: subAccounts.value,
isEdit: !!editAccountId.value,
clientSessionId: clientSessionId.value
}).then(() => {
submitting.value = false;
let message = 'You have saved this account';
if (!editAccountId.value) {
message = 'You have added a new account';
}
resolveFunc?.({ message });
showState.value = false;
}).catch(error => {
submitting.value = false;
if (!error.processed) {
snackbar.value?.showError(error);
}
});
}
function removeSubAccount(subAccount: Account): void {
confirmDialog.value?.open('Are you sure you want to remove this sub-account?').then(() => {
for (let i = 0; i < subAccounts.value.length; i++) {
if (subAccounts.value[i] === subAccount) {
subAccounts.value.splice(i, 1);
if (currentAccountIndex.value >= subAccounts.value.length) {
currentAccountIndex.value = subAccounts.value.length - 1;
}
}
}
});
}
function cancel(): void {
rejectFunc?.();
showState.value = false;
}
function onShowDateTimeError(error: string): void {
snackbar.value?.showError(error);
}
watch(() => account.value.type, () => {
if (subAccounts.value.length < 1) {
addSubAccount();
}
});
defineExpose({
open
});
</script>