Skip to content

Open source personal finance application

refactor(categories): consolidate category utility functions and remove unused files

Ilya Komichev97200dd7eb1b
diff --git a/app/assets/js/emo.ts b/app/assets/js/emo.ts
index d434b47b..6e48dc1c 100755
--- a/app/assets/js/emo.ts
+++ b/app/assets/js/emo.ts
@@ -1,8 +1,7 @@
const successEmo = ['🀟', '🀘', 'πŸ€™', 'πŸ‘Œ', 'πŸ‘', 'πŸ’ͺ', 'πŸ‘', 'πŸ€—', 'πŸ₯°', '😍', '🀩']
const errorEmo = ['πŸ€·β€β™€οΈ', 'πŸ€¦β€β™€οΈ', 'πŸ€”', '😲']
-const updateEmo = ['🀟', 'πŸ’ƒ', '✨', 'πŸ”₯', 'πŸ’₯', 'πŸŽ‰', '🀘', 'πŸ€™']
-function random(items: string[]): string {
+function random<T>(items: T[]): T {
return items[Math.floor(Math.random() * items.length)]!
}
@@ -10,5 +9,4 @@ export {
errorEmo,
random,
successEmo,
- updateEmo,
}
diff --git a/app/components/categories/getCategories.ts b/app/components/categories/getCategories.ts
deleted file mode 100644
index a7c20387..00000000
--- a/app/components/categories/getCategories.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import type { Categories, CategoryId, CategoryItem } from '~/components/categories/types'
-
-export function getParentCategory(items: Categories, parentId: CategoryId | 0): CategoryItem | false {
- if (parentId === 0)
- return false
-
- return items[parentId] ?? false
-}
-
-export function getParentCategoryId(items: Categories, categoryId: CategoryId): CategoryId {
- const category = items[categoryId]
- if (!category || category.parentId === 0)
- return categoryId
- return category.parentId ?? categoryId
-}
diff --git a/app/components/categories/getForm.ts b/app/components/categories/getForm.ts
deleted file mode 100644
index 134afe98..00000000
--- a/app/components/categories/getForm.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import type { CategoryForm } from '~/components/categories/types'
-
-import { random } from '~/assets/js/emo'
-import icons from '~/assets/js/icons'
-import { colorsArray } from '~/components/color/colors'
-
-export function getPreparedFormData(values?: any): CategoryForm {
- return {
- color: values?.color ?? random(colorsArray),
- icon: values?.icon ?? random(random(icons)),
- name: values?.name ?? '',
- order: values?.order ?? 1,
- parentId: values?.parentId ?? 0,
- showInLastUsed: values?.showInLastUsed ?? true,
- showInQuickSelector: values?.showInQuickSelector ?? false,
- }
-}
diff --git a/app/components/categories/page/New.vue b/app/components/categories/page/New.vue
index 6ad31e1e..791b1bab 100644
--- a/app/components/categories/page/New.vue
+++ b/app/components/categories/page/New.vue
@@ -1,21 +1,12 @@
<script setup lang="ts">
-import type { CategoryId } from '~/components/categories/types'
+import type { CategoryId, CategoryItem } from '~/components/categories/types'
-import { getPreparedFormData } from '~/components/categories/getForm'
+import { getPreparedFormData } from '~/components/categories/utils'
const { t } = useI18n()
const router = useRouter()
-
const categoryForm = ref(getPreparedFormData())
-function updateValue(id: CategoryId, value: unknown) {
- return categoryForm.value[id] = value
-}
-
-function afterSave() {
- return router.replace('/categories/')
-}
-
useHead({
title: `${t('base.add')}: ${categoryForm.value.name ? categoryForm.value.name : t('categories.form.name.label')}`,
})
@@ -24,15 +15,13 @@ useHead({
<template>
<UiPage class="flex h-full flex-col">
<UiHeader>
- <UiHeaderTitle>
- {{ t('categories.createNewTitle') }}
- </UiHeaderTitle>
+ <UiHeaderTitle>{{ t('categories.createNewTitle') }}</UiHeaderTitle>
</UiHeader>
<CategoriesForm
:categoryForm="categoryForm"
- @updateValue="updateValue"
- @afterSave="afterSave"
+ @updateValue="(key: CategoryId, value: keyof CategoryItem) => categoryForm[key] = value"
+ @afterSave="() => router.replace('/categories')"
/>
</UiPage>
</template>
diff --git a/app/components/categories/page/id/Edit.vue b/app/components/categories/page/id/Edit.vue
index d0a6c2d8..cdc41bcd 100644
--- a/app/components/categories/page/id/Edit.vue
+++ b/app/components/categories/page/id/Edit.vue
@@ -1,8 +1,8 @@
<script setup lang="ts">
import type { CategoryId, CategoryItem } from '~/components/categories/types'
-import { getPreparedFormData } from '~/components/categories/getForm'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
+import { getPreparedFormData } from '~/components/categories/utils'
const { t } = useI18n()
const router = useRouter()
@@ -13,16 +13,8 @@ const categoryId = computed<CategoryId>(() => route.params.id)
const category = computed<CategoryItem>(() => categoriesStore.items[categoryId.value])
const categoryForm = ref(getPreparedFormData(category.value))
-function updateValue(id: CategoryId, value: any) {
- categoryForm.value[id] = value
-}
-
-function afterSave() {
- return router.replace(`/categories/${categoryId.value}`)
-}
-
useHead({
- title: `${t('base.edit')}: ${categoryForm.value?.name ? categoryForm.value?.name : t('categories.form.name.label')}`,
+ title: `${t('base.edit')}: ${categoryForm.value?.name || t('categories.form.name.label')}`,
})
</script>
@@ -32,16 +24,14 @@ useHead({
class="flex h-full flex-col"
>
<UiHeader>
- <UiHeaderTitle>
- {{ t('base.edit') }} : {{ category.name }}
- </UiHeaderTitle>
+ <UiHeaderTitle>{{ t('base.edit') }} : {{ category.name }}</UiHeaderTitle>
</UiHeader>
<CategoriesForm
:categoryId="categoryId"
:categoryForm="categoryForm"
- @updateValue="updateValue"
- @afterSave="afterSave"
+ @updateValue="(key: CategoryId, value: keyof CategoryItem) => categoryForm[key] = value"
+ @afterSave="() => router.replace(`/categories/${categoryId}`)"
/>
</UiPage>
</template>
diff --git a/app/components/categories/useCategoriesStore.ts b/app/components/categories/useCategoriesStore.ts
index babd6aa0..0f71de3e 100644
--- a/app/components/categories/useCategoriesStore.ts
+++ b/app/components/categories/useCategoriesStore.ts
@@ -8,8 +8,7 @@ import { getDataAndWatch, removeData, saveData, unsubscribeData, updateData } fr
import type { AddCategoryParams, Categories, CategoryId, CategoryItem, CategoryItemWithId } from '~/components/categories/types'
import type { TrnId } from '~/components/trns/types'
-import { getPreparedFormData } from '~/components/categories/getForm'
-import { compareCategoriesByParentAndName, getTransactibleCategoriesIds, getTransferCategoriesIds } from '~/components/categories/utils'
+import { compareCategoriesByParentAndName, getPreparedFormData, getTransactibleCategoriesIds, getTransferCategoriesIds } from '~/components/categories/utils'
import { useDemo } from '~/components/demo/useDemo'
import { useTrnsStore } from '~/components/trns/useTrnsStore'
import { useUserStore } from '~/components/user/useUserStore'
diff --git a/app/components/categories/utils.ts b/app/components/categories/utils.ts
index 1443d5ee..359ff263 100644
--- a/app/components/categories/utils.ts
+++ b/app/components/categories/utils.ts
@@ -1,5 +1,9 @@
import type { Categories, CategoryId, CategoryItem } from '~/components/categories/types'
+import { random } from '~/assets/js/emo'
+import icons from '~/assets/js/icons'
+import { colorsArray } from '~/components/color/colors'
+
export function getTransactibleCategoriesIds(items: Categories, ids?: CategoryId[]) {
return (ids ?? Object.keys(items ?? [])).reduce((acc, id) => {
const category = items?.[id]
@@ -21,8 +25,39 @@ export function getTransferCategoriesIds(items: Categories): CategoryId[] {
]
}
+export function getParentCategory(items: Categories, parentId: CategoryId | 0): CategoryItem | false {
+ if (parentId === 0)
+ return false
+
+ return items[parentId] ?? false
+}
+
+export function getParentCategoryIdOrReturnSame(items: Categories, categoryId: CategoryId): CategoryId {
+ const category = items[categoryId]
+ if (!category || category.parentId === 0)
+ return categoryId
+ return category.parentId ?? categoryId
+}
+
+export function getParentCategoryId2(items: Categories, categoryId: CategoryId): CategoryId | undefined {
+ const category = items[categoryId]
+ return category?.parentId === 0 ? undefined : category?.parentId
+}
+
export function compareCategoriesByParentAndName(a: CategoryItem, b: CategoryItem, items: Categories): number {
const parentNameA = items[a.parentId]?.name || ''
const parentNameB = items[b.parentId]?.name || ''
return parentNameA.localeCompare(parentNameB) || a.name.localeCompare(b.name)
}
+
+export function getPreparedFormData(values?: any): CategoryItem {
+ return {
+ color: values?.color ?? random(colorsArray),
+ icon: values?.icon ?? random(random(icons)),
+ name: values?.name ?? '',
+ order: values?.order ?? 1,
+ parentId: values?.parentId ?? 0,
+ showInLastUsed: values?.showInLastUsed ?? true,
+ showInQuickSelector: values?.showInQuickSelector ?? false,
+ }
+}
diff --git a/app/components/stat/categories/useStatCategories.ts b/app/components/stat/categories/useStatCategories.ts
index 89f1f61b..203fcedd 100644
--- a/app/components/stat/categories/useStatCategories.ts
+++ b/app/components/stat/categories/useStatCategories.ts
@@ -4,6 +4,7 @@ import type { TrnId } from '~/components/trns/types'
import { useAmount } from '~/components/amount/useAmount'
import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
+import { getParentCategoryId2 } from '~/components/categories/utils'
import { sortCategoriesByAmount } from '~/components/stat/utils'
import { useTrnsStore } from '~/components/trns/useTrnsStore'
@@ -12,11 +13,6 @@ export function useStatCategories() {
const trnsStore = useTrnsStore()
const { getTotalOfTrnsIds } = useAmount()
- function getParentCategoryId(categoryId: CategoryId): CategoryId | undefined {
- const category = categoriesStore.items[categoryId]
- return category?.parentId === 0 ? undefined : category?.parentId
- }
-
function collectCategoriesByTrns(trnsIds: TrnId[], preCategoriesIds?: CategoryId[]): CategoriesWithData {
const categoriesByTrns = trnsIds.reduce((acc, trnId) => {
const categoryId = trnsStore.items?.[trnId]?.categoryId
@@ -65,7 +61,7 @@ export function useStatCategories() {
function groupCategories(categoriesByTrns: CategoriesWithData): CategoryWithData[] {
const groupedCategories = Object.values(categoriesByTrns).reduce((acc, cat) => {
- const parentId = getParentCategoryId(cat.id)
+ const parentId = getParentCategoryId2(categoriesStore.items, cat.id)
const parentCategory = parentId && categoriesStore.items[parentId]
if (!parentId || !parentCategory) {
@@ -112,6 +108,5 @@ export function useStatCategories() {
return {
getCategoriesWithData,
- getParentCategoryId,
}
}