From 00d6f5d4737a64078fedbd5f0f2d36242ed3af61 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Tue, 4 Feb 2025 14:08:38 +0800 Subject: [PATCH] code refactor --- src/views/mobile/accounts/ListPage.vue | 52 ++++----- src/views/mobile/categories/ListPage.vue | 16 +-- src/views/mobile/tags/ListPage.vue | 142 ++++++++++++----------- 3 files changed, 110 insertions(+), 100 deletions(-) diff --git a/src/views/mobile/accounts/ListPage.vue b/src/views/mobile/accounts/ListPage.vue index af87e51d..40621c84 100644 --- a/src/views/mobile/accounts/ListPage.vue +++ b/src/views/mobile/accounts/ListPage.vue @@ -363,32 +363,6 @@ function setSortable(): void { displayOrderModified.value = false; } -function onSort(event: { el: { id: string }; from: number; to: number }): void { - if (!event || !event.el || !event.el.id) { - showToast('Unable to move account'); - return; - } - - const id = parseAccountIdFromDomId(event.el.id); - - if (!id) { - showToast('Unable to move account'); - return; - } - - accountsStore.changeAccountDisplayOrder({ - accountId: id, - from: event.from - 1, // first item in the list is title, so the index need minus one - to: event.to - 1, - updateListOrder: true, - updateGlobalListOrder: true - }).then(() => { - displayOrderModified.value = true; - }).catch(error => { - showToast(error.message || error); - }); -} - function saveSortResult(): void { if (!displayOrderModified.value) { showHidden.value = false; @@ -416,6 +390,32 @@ function saveSortResult(): void { }); } +function onSort(event: { el: { id: string }; from: number; to: number }): void { + if (!event || !event.el || !event.el.id) { + showToast('Unable to move account'); + return; + } + + const id = parseAccountIdFromDomId(event.el.id); + + if (!id) { + showToast('Unable to move account'); + return; + } + + accountsStore.changeAccountDisplayOrder({ + accountId: id, + from: event.from - 1, // first item in the list is title, so the index need minus one + to: event.to - 1, + updateListOrder: true, + updateGlobalListOrder: true + }).then(() => { + displayOrderModified.value = true; + }).catch(error => { + showToast(error.message || error); + }); +} + function onPageAfterIn(): void { if (accountsStore.accountListStateInvalid && !loading.value) { reload(null); diff --git a/src/views/mobile/categories/ListPage.vue b/src/views/mobile/categories/ListPage.vue index 6f9ed9ab..96b6d7d9 100644 --- a/src/views/mobile/categories/ListPage.vue +++ b/src/views/mobile/categories/ListPage.vue @@ -347,14 +347,6 @@ function saveSortResult(): void { }); } -function onPageAfterIn(): void { - if (transactionCategoriesStore.transactionCategoryListStateInvalid && !loading.value) { - reload(null); - } - - routeBackOnError(props.f7router, loadingError); -} - function onSort(event: { el: { id: string }; from: number; to: number }): void { if (!event || !event.el || !event.el.id) { showToast('Unable to move category'); @@ -379,6 +371,14 @@ function onSort(event: { el: { id: string }; from: number; to: number }): void { }); } +function onPageAfterIn(): void { + if (transactionCategoriesStore.transactionCategoryListStateInvalid && !loading.value) { + reload(null); + } + + routeBackOnError(props.f7router, loadingError); +} + init(); diff --git a/src/views/mobile/tags/ListPage.vue b/src/views/mobile/tags/ListPage.vue index 47909418..53b01d2b 100644 --- a/src/views/mobile/tags/ListPage.vue +++ b/src/views/mobile/tags/ListPage.vue @@ -165,7 +165,7 @@ const props = defineProps<{ }>(); const { tt } = useI18n(); -const { showToast, routeBackOnError } = useI18nUIComponents(); +const { showAlert, showToast, routeBackOnError } = useI18nUIComponents(); const transactionTagsStore = useTransactionTagsStore(); @@ -207,7 +207,24 @@ function parseTagIdFromDomId(domId: string): string | null { return domId.substring(4); // tag_ } -function reload(done?: () => void): void { +function init(): void { + loading.value = true; + + transactionTagsStore.loadAllTags({ + force: false + }).then(() => { + loading.value = false; + }).catch(error => { + if (error.processed) { + loading.value = false; + } else { + loadingError.value = error; + showToast(error.message || error); + } + }); +} + +function reload(done: (() => void) | null): void { if (sortable.value || hasEditingTag.value) { done?.(); return; @@ -273,6 +290,55 @@ function cancelSave(tag: TransactionTag): void { } } +function hide(tag: TransactionTag, hidden: boolean): void { + showLoading(); + + transactionTagsStore.hideTag({ + tag: tag, + hidden: hidden + }).then(() => { + hideLoading(); + }).catch(error => { + hideLoading(); + + if (!error.processed) { + showToast(error.message || error); + } + }); +} + +function remove(tag: TransactionTag | null, confirm: boolean): void { + if (!tag) { + showAlert('An error occurred'); + return; + } + + if (!confirm) { + tagToDelete.value = tag; + showDeleteActionSheet.value = true; + return; + } + + showDeleteActionSheet.value = false; + tagToDelete.value = null; + showLoading(); + + transactionTagsStore.deleteTag({ + tag: tag, + beforeResolve: (done) => { + onSwipeoutDeleted(getTagDomId(tag), done); + } + }).then(() => { + hideLoading(); + }).catch(error => { + hideLoading(); + + if (!error.processed) { + showToast(error.message || error); + } + }); +} + function setSortable(): void { if (sortable.value || hasEditingTag.value) { return; @@ -310,59 +376,6 @@ function saveSortResult(): void { }); } -function hide(tag: TransactionTag, hidden: boolean): void { - showLoading(); - - transactionTagsStore.hideTag({ - tag: tag, - hidden: hidden - }).then(() => { - hideLoading(); - }).catch(error => { - hideLoading(); - - if (!error.processed) { - showToast(error.message || error); - } - }); -} - -function remove(tag: TransactionTag | null, confirm: boolean): void { - if (!tag) { - showToast('An error occurred'); - return; - } - - if (!confirm) { - tagToDelete.value = tag; - showDeleteActionSheet.value = true; - return; - } - - showDeleteActionSheet.value = false; - tagToDelete.value = null; - showLoading(); - - transactionTagsStore.deleteTag({ - tag: tag, - beforeResolve: (done) => { - onSwipeoutDeleted(getTagDomId(tag), done); - } - }).then(() => { - hideLoading(); - }).catch(error => { - hideLoading(); - - if (!error.processed) { - showToast(error.message || error); - } - }); -} - -function onPageAfterIn(): void { - routeBackOnError(props.f7router, loadingError); -} - function onSort(event: { el: { id: string }, from: number, to: number }): void { if (!event || !event.el || !event.el.id) { showToast('Unable to move tag'); @@ -387,18 +400,15 @@ function onSort(event: { el: { id: string }, from: number, to: number }): void { }); } -transactionTagsStore.loadAllTags({ - force: false -}).then(() => { - loading.value = false; -}).catch(error => { - if (error.processed) { - loading.value = false; - } else { - loadingError.value = error; - showToast(error.message || error); +function onPageAfterIn(): void { + if (transactionTagsStore.transactionTagListStateInvalid && !loading.value) { + reload(null); } -}); + + routeBackOnError(props.f7router, loadingError); +} + +init();