Skip to content

Open source personal finance application

improvements: refactor

ilkome6cab45f3e746
diff --git a/components/categories/Form.vue b/components/categories/Form.vue
index d8da73b1..26def1e5 100644
--- a/components/categories/Form.vue
+++ b/components/categories/Form.vue
@@ -19,7 +19,8 @@ const editCategoryId = categoryId.value ?? generateId()
const activeTab = ref('data')
const isUpdateChildCategoriesColor = ref(true)
-const isAllowChangeParent = computed(() => getChildCategoriesIds(categoryId.value).length === 0)
+const isAllowChangeParent = computed(() =>
+ $store.getters['categories/getChildCategoriesIds'](categoryId.value).length === 0)
const tabs = computed(() => [{
id: 'data',
@@ -49,22 +50,6 @@ function findCategoryIconByColor(color) {
?.icon
}
-/**
- * Get child categories ids
- */
-function getChildCategoriesIds(categoryId: CategoryID) {
- if (!categoryId)
- return []
-
- const categoriesItems: Record<CategoryID, CategoryItem> = $store.state.categories.items
- const category: CategoryItem = categoriesItems[categoryId]
-
- if (category?.parentId === 0)
- return Object.keys(categoriesItems).filter(id => categoriesItems[id]?.parentId === categoryId)
-
- return []
-}
-
/**
* Select parent
*/
@@ -122,7 +107,7 @@ async function onSave() {
return
const uid = $store.state.user.user.uid
- const categoryChildIds = getChildCategoriesIds(editCategoryId)
+ const categoryChildIds = $store.getters['categories/getChildCategoriesIds'](editCategoryId)
const categoryValues = getPreparedFormData(categoryForm.value, categoryChildIds)
// Update category
@@ -169,21 +154,21 @@ div
)
LazySharedContextMenuItem(
- v-if="getChildCategoriesIds(categoryId).length > 0 "
+ v-if="$store.getters['categories/getChildCategoriesIds'](categoryId).length > 0 "
:checkboxValue="isUpdateChildCategoriesColor"
:title="$t('categories.form.childColor')"
showCheckbox
@onClick="isUpdateChildCategoriesColor = !isUpdateChildCategoriesColor"
)
LazySharedContextMenuItem(
- v-if="getChildCategoriesIds(categoryId).length === 0"
+ v-if="$store.getters['categories/getChildCategoriesIds'](categoryId).length === 0"
:checkboxValue="categoryForm.showInLastUsed"
:title="$t('categories.form.lastUsed')"
showCheckbox
@onClick="categoryForm.showInLastUsed = !categoryForm.showInLastUsed"
)
SharedContextMenuItem(
- v-if="getChildCategoriesIds(categoryId).length === 0"
+ v-if="$store.getters['categories/getChildCategoriesIds'](categoryId).length === 0"
:checkboxValue="categoryForm.showInQuickSelector"
:title="$t('categories.form.quickSelector')"
showCheckbox
diff --git a/components/categories/Item.vue b/components/categories/Item.vue
index a6444ee8..05cf9c2c 100644
--- a/components/categories/Item.vue
+++ b/components/categories/Item.vue
@@ -1,49 +1,36 @@
-<script>
-// TODO: setup
+<script setup lang="ts">
+import type { CategoryID, CategoryItem } from '~/components/categories/types'
import useFilter from '~/components/filter/useFilter'
-export default defineComponent({
- props: {
- activeItemId: { type: String || 0, default: null },
- category: { type: Object, required: true },
- id: { type: String, required: true },
- isHideParentCategory: { type: Boolean, default: false },
- slider: { type: Object, required: true },
- },
-
- setup(props, { listeners }) {
- const { $store } = useNuxtApp()
- const { setFilterCatsId } = useFilter()
-
- const childCategoriesIds = computed(() => $store.getters['categories/getChildCategoriesIds'](props.id))
- const parentCategory = computed(() => $store.state.categories.items[props.category?.parentId])
-
- function onClickItem() {
- if (listeners.onClick)
- listeners.onClick(props.id)
- }
-
- function onClickIcon() {
- // Prevent filter when using in TrnForm
- if (props.slider) {
- onClickItem()
- return
- }
-
- // TODO: useFilter
- setFilterCatsId(props.id)
- $store.commit('filter/setFilterDateNow')
- $store.dispatch('ui/setActiveTabStat', 'details')
- }
-
- return {
- childCategoriesIds,
- parentCategory,
- onClickItem,
- onClickIcon,
- }
- },
-})
+const props = defineProps<{
+ activeItemId: string | 0
+ category: CategoryItem
+ id: CategoryID
+ isHideParentCategory?: boolean
+ slider: any
+}>()
+const emit = defineEmits(['onClick'])
+
+const { $store } = useNuxtApp()
+const { setFilterCatsId } = useFilter()
+const childCategoriesIds = computed(() =>
+ $store.getters['categories/getChildCategoriesIds'](props.id))
+
+const parentCategory = computed(() => $store.state.categories.items[props.category?.parentId])
+const onClickItem = () => emit('onClick', props.id)
+
+function onClickIcon() {
+ // Prevent filter when using in TrnForm
+ if (props.slider) {
+ onClickItem()
+ return
+ }
+
+ // TODO: useFilter
+ setFilterCatsId(props.id)
+ $store.commit('filter/setFilterDateNow')
+ $store.dispatch('ui/setActiveTabStat', 'details')
+}
</script>
<template lang="pug">
diff --git a/components/trnForm/modal/TrnFormModalCatsChild.vue b/components/trnForm/modal/TrnFormModalCatsChild.vue
index 65167e60..0e69e8d2 100644
--- a/components/trnForm/modal/TrnFormModalCatsChild.vue
+++ b/components/trnForm/modal/TrnFormModalCatsChild.vue
@@ -1,4 +1,5 @@
<script>
+// TODO: setup
export default {
data() {
return {
diff --git a/store/categories/getters.ts b/store/categories/getters.ts
index 590e6fee..afa5af6f 100644
--- a/store/categories/getters.ts
+++ b/store/categories/getters.ts
@@ -59,7 +59,12 @@ export default {
if (!getters.hasCategories)
return []
- return state.items[categoryId]?.childIds || []
+ const category: CategoryItem = state.items[categoryId]
+
+ if (category?.parentId === 0)
+ return Object.keys(state.items).filter(id => state.items[id]?.parentId === categoryId)
+
+ return []
},
recentCategoriesIds(state, getters, rootState, rootGetters) {