From eb2b6d1002369678f51cf11177b8905ecb225fb6 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sat, 11 Jan 2025 23:09:03 +0800 Subject: [PATCH] migrate transaction tag list page to composition API and typescript --- src/components/desktop/ConfirmDialog.vue | 26 +- src/components/desktop/SnackBar.vue | 9 +- src/lib/{tag.js => tag.ts} | 10 +- src/lib/ui/mobile.ts | 20 +- src/views/desktop/tags/ListPage.vue | 466 ++++++++++---------- src/views/mobile/tags/ListPage.vue | 525 +++++++++++------------ 6 files changed, 534 insertions(+), 522 deletions(-) rename src/lib/{tag.js => tag.ts} (59%) diff --git a/src/components/desktop/ConfirmDialog.vue b/src/components/desktop/ConfirmDialog.vue index ea72fca5..3b58fc28 100644 --- a/src/components/desktop/ConfirmDialog.vue +++ b/src/components/desktop/ConfirmDialog.vue @@ -19,7 +19,7 @@ import { ref, watch } from 'vue'; import { useI18n } from '@/locales/helpers.ts'; -import { isString } from '@/lib/common.ts'; +import { isString, isObject } from '@/lib/common.ts'; const props = defineProps<{ show?: boolean @@ -42,16 +42,26 @@ const finalColor = ref(props.color || 'primary'); let resolveFunc: ((value?: unknown) => void) | null = null; let rejectFunc: ((reason?: unknown) => void) | null = null; -function open(titleOrText: string, textOrOptions: string | Record, options: Record): Promise { +function open(titleOrText: string, textOrOptions?: string | Record, options?: Record): Promise { showState.value = true; - if (isString(textOrOptions)) { // second parameter is text - titleContent.value = tt(titleOrText, options); - textContent.value = tt(textOrOptions as string, options); - } else { // second parameter is options - const actualOptions = textOrOptions as Record; + if (!textOrOptions || isObject(textOrOptions)) { // only one parameter or second parameter is options titleContent.value = tt('global.app.title'); - textContent.value = tt(titleOrText, actualOptions); + + if (!textOrOptions) { + textContent.value = tt(titleOrText); + } else { + const actualOptions = textOrOptions as Record; + textContent.value = tt(titleOrText, actualOptions); + } + } else if (isString(textOrOptions)) { // second parameter is text + if (!options) { + titleContent.value = tt(titleOrText); + textContent.value = tt(textOrOptions as string); + } else { + titleContent.value = tt(titleOrText, options); + textContent.value = tt(textOrOptions as string, options); + } } if (options && isString(options.color)) { diff --git a/src/components/desktop/SnackBar.vue b/src/components/desktop/SnackBar.vue index 40e19af4..29bb00ad 100644 --- a/src/components/desktop/SnackBar.vue +++ b/src/components/desktop/SnackBar.vue @@ -24,9 +24,14 @@ const { tt, te } = useI18n(); const showState= ref(false); const messageContent = ref(''); -function showMessage(message: string, options: Record): void { +function showMessage(message: string, options?: Record): void { showState.value = true; - messageContent.value = tt(message, options); + + if (options) { + messageContent.value = tt(message, options); + } else { + messageContent.value = tt(message); + } } function showError(error: string | { message: string }): void { diff --git a/src/lib/tag.js b/src/lib/tag.ts similarity index 59% rename from src/lib/tag.js rename to src/lib/tag.ts index 6938ec94..03d15fae 100644 --- a/src/lib/tag.js +++ b/src/lib/tag.ts @@ -1,4 +1,6 @@ -export function isNoAvailableTag(tags, showHidden) { +import { TransactionTag } from '@/models/transaction_tag.ts'; + +export function isNoAvailableTag(tags: TransactionTag[], showHidden: boolean): boolean { for (let i = 0; i < tags.length; i++) { if (showHidden || !tags[i].hidden) { return false; @@ -8,7 +10,7 @@ export function isNoAvailableTag(tags, showHidden) { return true; } -export function getAvailableTagCount(tags, showHidden) { +export function getAvailableTagCount(tags: TransactionTag[], showHidden: boolean): number { let count = 0; for (let i = 0; i < tags.length; i++) { @@ -20,7 +22,7 @@ export function getAvailableTagCount(tags, showHidden) { return count; } -export function getFirstShowingId(tags, showHidden) { +export function getFirstShowingId(tags: TransactionTag[], showHidden: boolean): string | null { for (let i = 0; i < tags.length; i++) { if (showHidden || !tags[i].hidden) { return tags[i].id; @@ -30,7 +32,7 @@ export function getFirstShowingId(tags, showHidden) { return null; } -export function getLastShowingId(tags, showHidden) { +export function getLastShowingId(tags: TransactionTag[], showHidden: boolean): string | null { for (let i = tags.length - 1; i >= 0; i--) { if (showHidden || !tags[i].hidden) { return tags[i].id; diff --git a/src/lib/ui/mobile.ts b/src/lib/ui/mobile.ts index 3bed4fb8..092a1a21 100644 --- a/src/lib/ui/mobile.ts +++ b/src/lib/ui/mobile.ts @@ -1,3 +1,4 @@ +import { type Ref, watch } from 'vue'; import { useI18n as useVueI18n } from 'vue-i18n'; import { f7, f7ready } from 'framework7-vue'; import type { Dialog, Picker, Router } from 'framework7/types'; @@ -211,9 +212,26 @@ export function scrollToSelectedItem(parentEl: Framework7Dom, containerSelector: export function useI18nUIComponents() { const i18nGlobal = useVueI18n(); + function routeBackOnError(f7router: Router.Router, errorRef: Ref): void { + const unwatch = watch(errorRef, (newValue) => { + if (newValue) { + setTimeout(() => { + if (unwatch) { + unwatch(); + } + + f7router.back(); + }, 200); + } + }, { + immediate: true + }); + } + return { showAlert: (message: string, confirmCallback: (dialog: Dialog.Dialog, e: Event) => void) => showAlert(message, confirmCallback, i18nGlobal.t), showConfirm: (message: string, confirmCallback: (dialog: Dialog.Dialog, e: Event) => void, cancelCallback: (dialog: Dialog.Dialog, e: Event) => void): void => showConfirm(message, confirmCallback, cancelCallback, i18nGlobal.t), - showToast: (message: string, timeout?: number): void => showToast(message, timeout, i18nGlobal.t) + showToast: (message: string, timeout?: number): void => showToast(message, timeout, i18nGlobal.t), + routeBackOnError } } diff --git a/src/views/desktop/tags/ListPage.vue b/src/views/desktop/tags/ListPage.vue index e6ec602f..6b01fc03 100644 --- a/src/views/desktop/tags/ListPage.vue +++ b/src/views/desktop/tags/ListPage.vue @@ -4,12 +4,12 @@ - {{ $t('Refresh') }} + {{ tt('Refresh') }} @@ -42,9 +42,9 @@
- {{ $t('Tag Title') }} + {{ tt('Tag Title') }} - {{ $t('Operation') }} + {{ tt('Operation') }}
@@ -60,7 +60,7 @@ - {{ $t('No available tag') }} + {{ tt('No available tag') }} @@ -89,7 +89,7 @@ - {{ element.hidden ? $t('Show') : $t('Hide') }} + {{ element.hidden ? tt('Show') : tt('Hide') }}
- {{ $t('Edit') }} + {{ tt('Edit') }} - {{ $t('Delete') }} + {{ tt('Delete') }} - {{ $t('Save') }} + {{ tt('Save') }} - {{ $t('Cancel') }} + {{ tt('Cancel') }} - - {{ $t('Drag to Reorder') }} + {{ tt('Drag to Reorder') }} @@ -175,12 +175,12 @@ - +
-