Skip to content

Open source personal finance application

feat(StatGroup): refactor

ilkome6f41dc014f49
diff --git a/components/stat/group/Empty.vue b/components/stat/group/Empty.vue
index 19061720..6be915f3 100644
--- a/components/stat/group/Empty.vue
+++ b/components/stat/group/Empty.vue
@@ -1,24 +1,15 @@
-<script>
+<script setup lang="ts">
import useStatPage from '~/components/stat/useStatPage'
-export default defineComponent({
- props: {
- typeText: { type: String, required: true },
- },
+const props = defineProps<{
+ typeText: String
+}>()
- setup(props) {
- const { typeText } = toRefs(props)
- const { statPage } = useStatPage()
-
- const isShow = computed(() => statPage.current[typeText.value]?.categoriesIds?.length === 0)
-
- return {
- isShow,
- }
- },
-})
+const { statPage } = useStatPage()
+const isShow = computed(() => statPage.current[props.typeText]?.categoriesIds?.length === 0)
</script>
<template lang="pug">
-.my-4.px-2.text-sm(v-if="isShow") {{ $t('stat.empty') }}
+.my-4.px-2.text-sm(v-if="isShow")
+ | {{ $t('stat.empty') }}
</template>
diff --git a/components/stat/group/Horizontal.vue b/components/stat/group/Horizontal.vue
index 321990e6..0d4857f1 100644
--- a/components/stat/group/Horizontal.vue
+++ b/components/stat/group/Horizontal.vue
@@ -1,34 +1,23 @@
-<script>
+<script setup lang="ts">
import useStat from '~/modules/stat/useStat'
import useStatPage from '~/components/stat/useStatPage'
import useUIView from '~/components/layout/useUIView'
-export default defineComponent({
- props: {
- typeText: { type: String, required: true },
- },
+const props = defineProps<{
+ typeText: String
+}>()
- setup(props) {
- const { typeText } = toRefs(props)
- const { statPage } = useStatPage()
- const { ui } = useUIView()
- const { moneyTypes } = useStat()
+const { statPage } = useStatPage()
+const { ui } = useUIView()
+const { moneyTypes } = useStat()
- const isShow = computed(() => ui.showCatsHorizontalList && statPage.current[typeText.value]?.categoriesIds?.length)
- const typeNumber = moneyTypes.find(t => t.id === typeText.value)?.type
-
- return {
- statPage,
- isShow,
- typeNumber,
- }
- },
-})
+const isShow = computed(() => ui.showCatsHorizontalList && statPage.current[props.typeText]?.categoriesIds?.length)
+const typeNumber = moneyTypes.find(t => t.id === props.typeText)?.type
</script>
<template lang="pug">
.my-6.flex.flex-col.gap-2(v-if="isShow")
- StatItem(
+ StatGroupHorizontalItem(
v-for="categoryId in statPage.current[typeText].categoriesIds"
:key="categoryId"
:biggest="statPage.current[typeText].biggest"
diff --git a/components/stat/StatItem.vue b/components/stat/group/HorizontalItem.vue
similarity index 99%
rename from components/stat/StatItem.vue
rename to components/stat/group/HorizontalItem.vue
index b68366d0..67b042ba 100644
--- a/components/stat/StatItem.vue
+++ b/components/stat/group/HorizontalItem.vue
@@ -106,7 +106,7 @@ export default {
@click.stop=""
)
template(v-if="isCategoryHasChildren")
- StatItemChildCats(
+ StatGroupHorizontalItemCat(
:categoryId="categoryId"
:type="type"
)
diff --git a/components/stat/StatItemChildCats.vue b/components/stat/group/HorizontalItemCat.vue
similarity index 65%
rename from components/stat/StatItemChildCats.vue
rename to components/stat/group/HorizontalItemCat.vue
index 0c643e82..3b7677bb 100644
--- a/components/stat/StatItemChildCats.vue
+++ b/components/stat/group/HorizontalItemCat.vue
@@ -1,16 +1,10 @@
<script>
-export default {
- name: 'StatItemChildCats',
+import { getTotal } from '~/components/trns/getTotal'
+export default {
props: {
- categoryId: {
- type: String,
- required: true,
- },
- type: {
- type: Number,
- required: true,
- },
+ categoryId: { type: String, required: true },
+ type: { type: Number, required: true },
},
computed: {
@@ -50,13 +44,35 @@ export default {
trnsIds = trnsIds.filter(id => trns[id].type === this.type)
return trnsIds
},
+
+ getCategoryStat({ categoryId, trnsIds }) {
+ const trnsItems = this.$store.state.trns.items
+ const walletsItems = this.$store.state.wallets.items
+ const baseCurrencyCode = this.$store.state.currencies.base
+ const rates = this.$store.state.currencies.rates
+
+ const total = getTotal({
+ baseCurrencyCode,
+ rates,
+ trnsIds,
+ trnsItems,
+ walletsItems,
+ })
+
+ return {
+ categoryId,
+ total: total.sumTransactions,
+ incomes: total.incomeTransactions,
+ expense: total.expenseTransactions,
+ }
+ },
},
}
</script>
<template lang="pug">
div
- StatItemChildCatsItem(
+ StatGroupHorizontalItemCatItem(
v-for="category in statCategories"
:key="category.categoryId"
:biggest="$store.getters['stat/statCurrentPeriod'][typeName].biggest"
diff --git a/components/stat/StatItemChildCatsItem.vue b/components/stat/group/HorizontalItemCatItem.vue
similarity index 100%
rename from components/stat/StatItemChildCatsItem.vue
rename to components/stat/group/HorizontalItemCatItem.vue
diff --git a/components/stat/group/Round.vue b/components/stat/group/Round.vue
index 3f75edf6..9bec208a 100644
--- a/components/stat/group/Round.vue
+++ b/components/stat/group/Round.vue
@@ -1,67 +1,48 @@
-<script>
+<script setup lang="ts">
import useStat from '~/modules/stat/useStat'
import useStatPage from '~/components/stat/useStatPage'
import useUIView from '~/components/layout/useUIView'
-export default defineComponent({
- props: {
- typeText: { type: String, required: true },
- },
+const props = defineProps<{
+ typeText: String
+}>()
- setup(props) {
- const { typeText } = toRefs(props)
- const { statPage } = useStatPage()
- const { moneyTypes } = useStat()
- const { ui } = useUIView()
- const incomeRef = ref(null)
- const expenseRef = ref(null)
+const { typeText } = toRefs(props)
+const { statPage } = useStatPage()
+const { moneyTypes } = useStat()
+const { ui } = useUIView()
+const roundRef = ref(null)
- const isShow = computed(() => ui.showRoundCats && statPage.current[typeText.value]?.categoriesIds?.length)
- const typeNumber = moneyTypes.find(t => t.id === typeText.value)?.type
+const isShow = computed(() => ui.showRoundCats && statPage.current[props.typeText]?.categoriesIds?.length)
+const typeNumber = moneyTypes.find(t => t.id === props.typeText)?.type
- /**
- * Watch for changes in categories
- * Get max width from child elements name or amount
- * Set different width for income and expense
- */
- function updateWidth() {
- setTimeout(() => {
- let minWidth = 60
- let childs
- typeText.value === 'income'
- ? childs = incomeRef.value?.querySelectorAll('.js-getWidth')
- : childs = expenseRef.value?.querySelectorAll('.js-getWidth')
+/**
+ * Get max width from child elements name or amount
+ * Set different width for income and expense
+ */
+function updateWidth() {
+ setTimeout(() => {
+ const elements: Element[] = roundRef.value
+ ?.querySelector(`[data-type-text="${props.typeText}"]`)
+ ?.querySelectorAll('.js-getWidth')
- if (!childs)
- return
+ const minWidth: number = Array.from(elements)
+ .reduce((acc, e) => e.clientWidth > acc && e.clientWidth, 60)
- for (const key in childs) {
- if (childs[key].clientWidth > minWidth)
- minWidth = childs[key].clientWidth
- }
- typeText.value === 'income'
- ? incomeRef.value.style.setProperty('--minWidth', `${minWidth + 12}px`)
- : expenseRef.value.style.setProperty('--minWidth', `${minWidth + 12}px`)
- }, 100)
- }
- watch(statPage.current[typeText.value]?.categoriesIds, updateWidth, { immediate: true })
- watch(isShow, updateWidth, { immediate: true })
+ roundRef.value
+ ?.querySelector(`[data-type-text="${props.typeText}"]`)
+ ?.style.setProperty('--minWidth', `${minWidth + 12}px`)
+ }, 100)
+}
- return {
- statPage,
- isShow,
- typeNumber,
- incomeRef,
- expenseRef,
- }
- },
-})
+watch(statPage.current[props.typeText]?.categoriesIds, updateWidth, { immediate: true })
+watch(isShow, updateWidth, { immediate: true })
</script>
<template lang="pug">
-.my-6.rounded-lg.bg-skin-item-main-bg.border.dark_border-neutral-800(v-if="isShow")
- .items.grid.py-2(:ref="`${typeText}Ref`")
- LazyStatItemRound(
+.my-6.rounded-lg.bg-skin-item-main-bg.border.dark_border-neutral-800(v-if="isShow" ref="roundRef")
+ .items.grid.py-2(:data-type-text="`${typeText}`")
+ LazyStatGroupRoundItem(
v-if="isShow"
v-for="categoryId in statPage.current[typeText].categoriesIds"
:category="$store.state.categories.items[categoryId]"
@@ -75,7 +56,7 @@ export default defineComponent({
template(v-if="statPage.filter.categoryId")
template(v-for="categoryId in $store.getters['categories/getChildCategoriesIds'](statPage.filter.categoryId)")
template(v-if="!statPage.current[typeText].categoriesIds.includes(categoryId)")
- LazyStatItemRound(
+ LazyStatGroupRoundItem(
v-if="statPage.filter.categoryId"
:category="$store.state.categories.items[categoryId]"
:categoryId="categoryId"
diff --git a/components/stat/StatItemRound.vue b/components/stat/group/RoundItem.vue
similarity index 98%
rename from components/stat/StatItemRound.vue
rename to components/stat/group/RoundItem.vue
index 527c3d5c..29d97696 100644
--- a/components/stat/StatItemRound.vue
+++ b/components/stat/group/RoundItem.vue
@@ -2,8 +2,6 @@
import useFilter from '~/modules/filter/useFilter'
export default {
- name: 'StatItemRound',
-
props: {
category: { type: Object, required: true },
categoryId: { type: String, required: true },