Open source personal finance application
refactor(categories, wallets): remove Delete components and integrate delete functionality into Index pages
Ilya Komichev783a4ea17643
diff --git a/app/components/categories/Delete.vue b/app/components/categories/Delete.vuedeleted file mode 100644index b65f250b..00000000--- a/app/components/categories/Delete.vue+++ /dev/null@@ -1,92 +0,0 @@-<script setup lang="ts">-import type { CategoryId } from '~/components/categories/types'--import { errorEmo, random, successEmo } from '~/assets/js/emo'-import { useCategoriesStore } from '~/components/categories/useCategoriesStore'-import { useTrnsStore } from '~/components/trns/useTrnsStore'-import UiToastContent from '~/components/ui/ToastContent.vue'--const props = defineProps<{- categoryId: CategoryId-}>()--const emit = defineEmits<{- close: []-}>()--const categoriesStore = useCategoriesStore()-const router = useRouter()-const trnsStore = useTrnsStore()-const { $toast } = useNuxtApp()-const { t } = useI18n()--const trnsIds = computed(() => trnsStore.getStoreTrnsIds({- categoriesIds: categoriesStore.getChildsIdsOrParent(props.categoryId),-}))--// TODO: translate-const deleteDescText = computed(() => {- if (trnsIds.value && trnsIds.value.length > 0)- return t('categories.form.delete.alertWithTrns', { trns: t('trns.plural', trnsIds.value.length) })- return undefined-})--const isShowDeleteConfirm = ref(false)--function onClickDelete() {- emit('close')-- for (const id in categoriesStore.items) {- if (categoriesStore.items[id]?.parentId === props.categoryId) {- $toast(UiToastContent, {- data: {- description: t('categories.form.delete.errorChilds'),- title: random(errorEmo),- },- toastId: 'delete-category-with-child-error',- type: 'error',- })-- return false- }- }-- isShowDeleteConfirm.value = true-}--async function onDeleteConfirm() {- const trnsIdsS = JSON.parse(JSON.stringify(trnsIds.value))- router.push('/categories')- await categoriesStore.deleteCategory(JSON.parse(JSON.stringify(props.categoryId), trnsIdsS))-- // Give some time to complete redirect- setTimeout(async () => {- $toast(UiToastContent, {- data: {- description: trnsIdsS?.length > 0- ? t('categories.form.delete.okWithTrns', { length: trnsIdsS.length, trns: t('trns.plural', trnsIdsS.length) })- : t('categories.form.delete.okWithoutTrns'),- title: random(successEmo),- },- toastId: 'delete-category-with-child-success',- type: 'success',- })- }, 300)-}-</script>--<template>- <UiHeaderLink- icon="mdi:delete-empty-outline"- @click="onClickDelete"- >- {{ t('base.delete') }}- </UiHeaderLink>-- <LayoutConfirmModal- v-if="isShowDeleteConfirm"- :description="deleteDescText"- @closed="isShowDeleteConfirm = false"- @onConfirm="onDeleteConfirm"- />-</template>diff --git a/app/components/categories/page/id/Index.vue b/app/components/categories/page/id/Index.vueindex 7a2bca31..3f0e998a 100644--- a/app/components/categories/page/id/Index.vue+++ b/app/components/categories/page/id/Index.vue@@ -5,6 +5,7 @@ import { differenceInDays } from 'date-fns'import type { CategoryId } from '~/components/categories/types'import type { StatTabSlug } from '~/components/stat/types'+import { errorEmo, random, successEmo } from '~/assets/js/emo'import { useCategoriesStore } from '~/components/categories/useCategoriesStore'import { useStatDate } from '~/components/date/useStatDate'import { calculateBestIntervalsBy } from '~/components/date/utils'@@ -13,15 +14,17 @@ import { useStatConfig } from '~/components/stat/useStatConfig'import { getTypesMapping } from '~/components/stat/utils'import { useTrnsFormStore } from '~/components/trnForm/useTrnsFormStore'import { useTrnsStore } from '~/components/trns/useTrnsStore'+import UiToastContent from '~/components/ui/ToastContent.vue'+const { $toast } = useNuxtApp()const { t } = useI18n()-const trnsStore = useTrnsStore()-const trnsFormStore = useTrnsFormStore()-const router = useRouter()-const route = useRoute()const categoriesStore = useCategoriesStore()-+const route = useRoute()+const router = useRouter()+const trnsFormStore = useTrnsFormStore()+const trnsStore = useTrnsStore()const filter = useFilter()+provide('filter', filter)const categoryId = computed(() => route.params.id) as ComputedRef<CategoryId>@@ -87,6 +90,60 @@ function onEditClick(close: () => void) {}useHead({ title: category.value?.name })++// TODO: translate+const deleteDescText = computed(() => {+ if (trnsIds.value && trnsIds.value.length > 0)+ return t('categories.form.delete.alertWithTrns', { trns: t('trns.plural', trnsIds.value.length) })+ return undefined+})++const isShowDeleteConfirm = ref(false)+function onClickDelete(close: () => void) {+ close()++ for (const id in categoriesStore.items) {+ if (categoriesStore.items[id]?.parentId === categoryId.value) {+ $toast(UiToastContent, {+ data: {+ description: t('categories.form.delete.errorChilds'),+ title: random(errorEmo),+ },+ toastId: 'delete-category-with-child-error',+ type: 'error',+ })++ return false+ }+ }++ isShowDeleteConfirm.value = true+}++async function onDeleteConfirm() {+ const trnsIdsS = JSON.parse(JSON.stringify(+ trnsStore.getStoreTrnsIds({+ categoriesIds: categoriesStore.getChildsIdsOrParent(categoryId.value),+ }),+ ))++ router.push('/categories')+ await categoriesStore.deleteCategory(JSON.parse(JSON.stringify(categoryId.value), trnsIdsS))++ // Give some time to complete redirect+ setTimeout(async () => {+ $toast(UiToastContent, {+ data: {+ description: trnsIdsS?.length > 0+ ? t('categories.form.delete.okWithTrns', { length: trnsIdsS.length, trns: t('trns.plural', trnsIdsS.length) })+ : t('categories.form.delete.okWithoutTrns'),+ title: random(successEmo),+ },+ toastId: 'delete-category-with-child-success',+ type: 'success',+ })+ }, 300)+}</script><template>@@ -114,10 +171,22 @@ useHead({ title: category.value?.name }){{ t('base.edit') }}</UiHeaderLink>- <CategoriesDelete :categoryId @close="close" />+ <UiHeaderLink+ icon="mdi:delete-empty-outline"+ @click="onClickDelete(close)"+ >+ {{ t('base.delete') }}+ </UiHeaderLink></template></StatHeader>+ <LayoutConfirmModal+ v-if="isShowDeleteConfirm"+ :description="deleteDescText"+ @closed="isShowDeleteConfirm = false"+ @onConfirm="onDeleteConfirm"+ />+<StatWrap:activeTab:hasChildren="categoriesIdsOrParent.length > 1"diff --git a/app/components/stat/ConfigModal.vue b/app/components/stat/ConfigModal.vueindex 0235ae3a..a999666b 100644--- a/app/components/stat/ConfigModal.vue+++ b/app/components/stat/ConfigModal.vue@@ -52,7 +52,7 @@ function updateStatAverage(value: number) {</template><template #content="{ close }">- <div class="grid gap-4 px-1 py-3 md:px-3 md:!pb-0">+ <div class="grid gap-4 px-1 py-3 md:px-3 md:!pb-2"><BottomSheetClose @click="close" /><!-- Chart -->@@ -221,7 +221,7 @@ function updateStatAverage(value: number) {<StatCategoriesButtons />- <slot />+ <slot :close="close" /></div></template></BottomSheetOrDropdown>diff --git a/app/components/stat/Header.vue b/app/components/stat/Header.vueindex a6c6a595..aefd36d1 100644--- a/app/components/stat/Header.vue+++ b/app/components/stat/Header.vue@@ -70,9 +70,16 @@ const statConfigModal: Ref<StatConfigModal> = ref({:isShowWallets="!!props.config.isShowWallets":statConfigModal>- <div v-if="$slots.popover">- <slot name="popover" :close="statConfigModal.close" />- </div>+ <template v-if="$slots.popover" #default="{ close }">+ <div>+ <slot+ name="popover" :close="() => {+ statConfigModal.close()+ close()+ }"+ />+ </div>+ </template></StatConfigModal></div></template>diff --git a/app/components/wallets/Delete.vue b/app/components/wallets/Delete.vuedeleted file mode 100644index efd21866..00000000--- a/app/components/wallets/Delete.vue+++ /dev/null@@ -1,79 +0,0 @@-<script setup lang="ts">-import type { TrnId } from '~/components/trns/types'-import type { WalletId } from '~/components/wallets/types'--import { random, successEmo } from '~/assets/js/emo'-import { useTrnsStore } from '~/components/trns/useTrnsStore'-import UiToastContent from '~/components/ui/ToastContent.vue'-import { useWalletsStore } from '~/components/wallets/useWalletsStore'--const props = defineProps<{- walletId: WalletId-}>()--const emit = defineEmits<{- close: []-}>()--const router = useRouter()-const trnsStore = useTrnsStore()-const walletStore = useWalletsStore()-const { $toast } = useNuxtApp()-const { t } = useI18n()--const trnsIds = computed<TrnId[]>(() => trnsStore.getStoreTrnsIds({- walletsIds: [props.walletId],-}))--// TODO: translate-const deleteDescText = computed(() => {- if (trnsIds.value.length > 0)- return `This action will delete ${trnsIds.value.length} trns in this wallet`- return undefined-})--const isShowDeleteConfirm = ref(false)-function onClickDelete() {- isShowDeleteConfirm.value = true-}--async function onDeleteConfirm() {- emit('close')-- const trnsIdsS: TrnId[] = JSON.parse(JSON.stringify(trnsIds.value))- router.push('/wallets')- await walletStore.deleteWallet(JSON.parse(JSON.stringify(props.walletId)), trnsIdsS)-- // Give some time to complete redirect- setTimeout(async () => {- $toast(UiToastContent, {- data: {- description: trnsIdsS?.length > 0- ? t('wallets.form.delete.okWithTrns', { length: trnsIdsS.length, trns: t('trns.plural', trnsIdsS.length) })- : t('wallets.form.delete.okWithoutTrns'),- title: random(successEmo),- },- toastId: 'delete-wallet',- type: 'success',- })- }, 300)-}-</script>--<template>- <div>- <UiHeaderLink- icon="mdi:delete-empty-outline"- @click="onClickDelete"- >- {{ t('base.delete') }}- </UiHeaderLink>-- <LayoutConfirmModal- v-if="isShowDeleteConfirm"- :description="deleteDescText"- @closed="isShowDeleteConfirm = false"- @onConfirm="onDeleteConfirm"- />- </div>-</template>diff --git a/app/components/wallets/page/id/Index.vue b/app/components/wallets/page/id/Index.vueindex a54439da..50247975 100644--- a/app/components/wallets/page/id/Index.vue+++ b/app/components/wallets/page/id/Index.vue@@ -2,23 +2,28 @@import { useStorage } from '@vueuse/core'import type { StatTabSlug } from '~/components/stat/types'+import type { TrnId } from '~/components/trns/types'import type { WalletId } from '~/components/wallets/types'+import { random, successEmo } from '~/assets/js/emo'import { useStatDate } from '~/components/date/useStatDate'import { useFilter } from '~/components/filter/useFilter'import { useStatConfig } from '~/components/stat/useStatConfig'import { getTypesMapping } from '~/components/stat/utils'import { useTrnsFormStore } from '~/components/trnForm/useTrnsFormStore'import { useTrnsStore } from '~/components/trns/useTrnsStore'+import UiToastContent from '~/components/ui/ToastContent.vue'import { icons } from '~/components/wallets/types'import { useWalletsStore } from '~/components/wallets/useWalletsStore'+const { $toast } = useNuxtApp()const { t } = useI18n()const route = useRoute()const router = useRouter()const trnsFormStore = useTrnsFormStore()const trnsStore = useTrnsStore()const walletsStore = useWalletsStore()+const walletStore = useWalletsStore()const filter = useFilter()provide('filter', filter)@@ -59,16 +64,55 @@ watch(filter.categoriesIds, () => {statConfig.config.value.isShowEmptyCategories = filter.categoriesIds.value.length > 0})+useHead({ title: wallet.value?.name })+onActivated(() => trnsFormStore.values.walletId = walletId.value)const total = computed(() => walletsStore.itemsComputed[walletId.value]?.amount ?? 0)-function onEditClick(close: () => void) {+function onClickEdit() {router.push(`/wallets/${walletId.value}/edit`)+}++// TODO: translate+const deleteDescText = computed(() => {+ if (trnsIds.value.length > 0)+ return `This action will delete ${trnsIds.value.length} trns in this wallet`+ return undefined+})++const isShowDeleteConfirm = ref(false)+function onClickDelete(close: () => void) {close()+ isShowDeleteConfirm.value = true}-useHead({ title: wallet.value?.name })+async function onDeleteConfirm() {+ console.log('onDeleteConfirm')++ const trnsIdsS: TrnId[] = JSON.parse(JSON.stringify(+ trnsStore.getStoreTrnsIds({+ walletsIds: [walletId.value],+ }),+ ))++ router.push('/wallets')+ await walletStore.deleteWallet(JSON.parse(JSON.stringify(walletId.value)), trnsIdsS)++ // Give some time to complete redirect+ setTimeout(async () => {+ $toast(UiToastContent, {+ data: {+ description: trnsIdsS?.length > 0+ ? t('wallets.form.delete.okWithTrns', { length: trnsIdsS.length, trns: t('trns.plural', trnsIdsS.length) })+ : t('wallets.form.delete.okWithoutTrns'),+ title: random(successEmo),+ },+ toastId: 'delete-wallet',+ type: 'success',+ })+ }, 300)+}</script><template>@@ -92,15 +136,27 @@ useHead({ title: wallet.value?.name })<template #popover="{ close }"><UiHeaderLinkicon="mdi:pencil-outline"- @click="onEditClick(close)"+ @click="onClickEdit">{{ t('base.edit') }}</UiHeaderLink>- <WalletsDelete :walletId @close="close" />+ <UiHeaderLink+ icon="mdi:delete-empty-outline"+ @click="onClickDelete(close)"+ >+ {{ t('base.delete') }}+ </UiHeaderLink></template></StatHeader>+ <LayoutConfirmModal+ v-if="isShowDeleteConfirm"+ :description="deleteDescText"+ @closed="isShowDeleteConfirm = false"+ @onConfirm="onDeleteConfirm"+ />+<div class="px-3 pb-2 lg:gap-8 lg:px-4 2xl:px-8"><divv-if="wallet.type !== 'credit'"