Open source personal finance application
improvements: refactor
ilkomeee987a035315
diff --git a/components/categories/Form.vue b/components/categories/Form.vueindex b7906330..d8da73b1 100644--- a/components/categories/Form.vue+++ b/components/categories/Form.vue@@ -3,6 +3,7 @@ import generateId from '~/utils/id'import icons from '~/assets/js/icons'import type { CategoryForm, CategoryID, CategoryItem } from '~/components/categories/types'import { allColors } from '~/assets/js/colors'+import { getPreparedFormData } from '~/components/categories/getForm'import { saveData } from '~/services/firebase/api'const props = defineProps<{@@ -18,7 +19,7 @@ const editCategoryId = categoryId.value ?? generateId()const activeTab = ref('data')const isUpdateChildCategoriesColor = ref(true)-const isAllowChangeParent = computed(() => !getChildCategoriesIds(categoryId.value))+const isAllowChangeParent = computed(() => getChildCategoriesIds(categoryId.value).length === 0)const tabs = computed(() => [{id: 'data',@@ -52,15 +53,16 @@ function findCategoryIconByColor(color) {* Get child categories ids*/function getChildCategoriesIds(categoryId: CategoryID) {- // TODO: always return arrayif (!categoryId)- return+ return []- const category: CategoryItem = $store.state.categories.items[categoryId]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 []}/**@@ -112,19 +114,6 @@ function validate({ values, categoriesItems }) {return true}-function prepareForm({ values, categoryChildIds }): CategoryForm {- return {- color: values.color,- icon: values.icon,- name: values.name,- order: values.order,- parentId: values.parentId,- showInLastUsed: categoryChildIds ? false : values.showInLastUsed,- showInQuickSelector: categoryChildIds ? false : values.showInQuickSelector,- showStat: categoryChildIds ? false : values.showStat,- }-}-async function onSave() {const categoriesItems = $store.state.categories.items@@ -134,7 +123,7 @@ async function onSave() {const uid = $store.state.user.user.uidconst categoryChildIds = getChildCategoriesIds(editCategoryId)- const categoryValues = prepareForm({ values: categoryForm.value, categoryChildIds })+ const categoryValues = getPreparedFormData(categoryForm.value, categoryChildIds)// Update categoryawait saveData(`users/${uid}/categories/${editCategoryId}`, categoryValues)@@ -180,21 +169,21 @@ div)LazySharedContextMenuItem(- v-if="getChildCategoriesIds(categoryId)"+ v-if="getChildCategoriesIds(categoryId).length > 0 ":checkboxValue="isUpdateChildCategoriesColor":title="$t('categories.form.childColor')"showCheckbox@onClick="isUpdateChildCategoriesColor = !isUpdateChildCategoriesColor")LazySharedContextMenuItem(- v-if="!getChildCategoriesIds(categoryId)"+ v-if="getChildCategoriesIds(categoryId).length === 0":checkboxValue="categoryForm.showInLastUsed":title="$t('categories.form.lastUsed')"showCheckbox@onClick="categoryForm.showInLastUsed = !categoryForm.showInLastUsed")SharedContextMenuItem(- v-if="!getChildCategoriesIds(categoryId)"+ v-if="getChildCategoriesIds(categoryId).length === 0":checkboxValue="categoryForm.showInQuickSelector":title="$t('categories.form.quickSelector')"showCheckbox@@ -238,6 +227,7 @@ div//- Parent//---------------------------------template(v-if="activeTab === 'parent'")+ pre {{ isAllowChangeParent }}template(v-if="!isAllowChangeParent")//- TODO: translate.p-4 You can not change parent category because edited category has childs categories.diff --git a/components/categories/getForm.ts b/components/categories/getForm.tsindex 2c3da170..117a21f4 100644--- a/components/categories/getForm.ts+++ b/components/categories/getForm.ts@@ -1,9 +1,9 @@import icons from '~/assets/js/icons'-import type { CategoryForm } from '~/components/categories/types'+import type { CategoryForm, CategoryID } from '~/components/categories/types'import { allColors } from '~/assets/js/colors'import { random } from '~/assets/js/emo'-export function getPreparedFormData(values?: any): CategoryForm {+export function getPreparedFormData(values?: any, childIds?: CategoryID[]): CategoryForm {return {color: values?.color ?? random(allColors),icon: values?.icon ?? random(random(icons)),@@ -13,5 +13,6 @@ export function getPreparedFormData(values?: any): CategoryForm {showInLastUsed: values?.showInLastUsed ?? true,showInQuickSelector: values?.showInQuickSelector ?? false,showStat: values?.showStat ?? true,+ childIds: childIds ?? false,}}diff --git a/components/categories/types.ts b/components/categories/types.tsindex deb2dd24..513d63ea 100644--- a/components/categories/types.ts+++ b/components/categories/types.ts@@ -21,4 +21,5 @@ export interface CategoryForm {showInLastUsed: booleanshowInQuickSelector: booleanshowStat: boolean+ childIds: string[] | false}diff --git a/components/trnForm/Categories.vue b/components/trnForm/Categories.vueindex 0e2f471d..62ad0900 100644--- a/components/trnForm/Categories.vue+++ b/components/trnForm/Categories.vue@@ -47,20 +47,20 @@ export default {.contentWrap__box.swiper-container(ref="trnFormCategories").swiper-wrapper+ //- Recent.swiper-slide- .scrollBlock.scrollerBlock(- v-if="$store.getters['categories/lastUsedCategoriesIdsByDate'] && $store.getters['categories/lastUsedCategoriesIdsByDate'].length > 0"- )+ .scrollBlock.scrollerBlock.py-4.px-3.text-center.text-skin-item-base.text-xl.font-nunito.font-semibold.bg-skin-layout-main.rounded-t-2xl| {{ $t('categories.lastUsedTitle') }} {{ $t('categories.title') }}.pb-1.px-3CategoriesList(:activeItemId="$store.state.trnForm.values.categoryId"- :ids="$store.getters['categories/lastUsedCategoriesIdsByDate']"+ :ids="$store.getters['categories/recentCategoriesIds']"class="!gap-x-1"@onClick="handleCategoryClick")+ //- Main.swiper-slide.scrollBlock.scrollerBlock.py-4.px-3.text-center.text-skin-item-base.text-xl.font-nunito.font-semibold.bg-skin-layout-main.rounded-t-2xl@@ -73,10 +73,9 @@ export default {@onClick="handleCategoryClick")+ //- Favorite.swiper-slide- .scrollBlock.scrollerBlock(- v-if="$store.getters['categories/favoriteCategoriesIds'] && $store.getters['categories/favoriteCategoriesIds'].length"- )+ .scrollBlock.scrollerBlock.py-4.px-3.text-center.text-skin-item-base.text-xl.font-nunito.font-semibold.bg-skin-layout-main.rounded-t-2xl| {{ $t('categories.favoriteTitle') }} {{ $t('categories.title') }}.pb-1.px-3diff --git a/components/trnForm/Index.vue b/components/trnForm/Index.vueindex a80920cd..4c8681ac 100644--- a/components/trnForm/Index.vue+++ b/components/trnForm/Index.vue@@ -269,8 +269,10 @@ function handleSubmitForm() {)//- Favorite categories- .pb-7(v-if="$store.getters['categories/favoriteCategoriesIds'] && $store.getters['categories/favoriteCategoriesIds'].length > 0")- .subTitle.text-center.pb-2.text-xs {{ $t('categories.favoriteTitle') }} {{ $t('categories.title') }}+ .pb-7(v-if="$store.getters['categories/favoriteCategoriesIds'].length > 0")+ .subTitle.text-center.pb-2.text-xs+ | {{ $t('categories.favoriteTitle') }} {{ $t('categories.title') }}+.px-3CategoriesList(v-if="sliderObj"@@ -281,13 +283,15 @@ function handleSubmitForm() {@onClick="onCategoryClick")- //- Last used categories- .pb-7- .subTitle.text-center.pb-2.text-xs {{ $t('categories.lastUsedTitle') }} {{ $t('categories.title') }}+ //- Recent categories+ .pb-7(v-if="$store.getters['categories/recentCategoriesIds'].length > 0")+ .subTitle.text-center.pb-2.text-xs+ | {{ $t('categories.lastUsedTitle') }} {{ $t('categories.title') }}+.px-3CategoriesList(v-if="sliderObj"- :ids="$store.getters['categories/lastUsedCategoriesIdsByDate']"+ :ids="$store.getters['categories/recentCategoriesIds']":activeItemId="$store.state.trnForm.values.categoryId":slider="sliderObj"class="!gap-x-1"diff --git a/components/wallets/Form.vue b/components/wallets/Form.vueindex 86f412ea..6087617d 100644--- a/components/wallets/Form.vue+++ b/components/wallets/Form.vue@@ -1,7 +1,9 @@<script setup lang="ts">-import { allColors } from '~/assets/js/colors'import generateId from '~/utils/id'import type { WalletForm, WalletID } from '~/components/wallets/types'+import { allColors } from '~/assets/js/colors'+import { getPreparedFormData } from '~/components/wallets/getForm'+import { saveData } from '~/services/firebase/api'const props = defineProps<{walletId?: WalletID@@ -19,7 +21,7 @@ const activeTab = ref('data')/*** Validate*/-function validate({ values, wallets }) {+function validate(values, wallets) {// nameif (!values.name) {$notify({@@ -38,6 +40,7 @@ function validate({ values, wallets }) {return false}+ // TODO: refactorfor (const id in wallets) {if (wallets[id].name === values.name) {if (editWalletId) {@@ -62,27 +65,21 @@ function validate({ values, wallets }) {return true}-function prepareForm({ values }) {- return {- color: values.color,- countTotal: values.countTotal,- currency: values.currency,- isCredit: values.isCredit,- name: values.name,- order: values.order,- }-}+async function saveWalletData(id, values) {+ const uid = $store.state.user.user.uid-function onSave() {- const wallets = $store.state.wallets.items+ // Set default currency based on first created wallet+ if (!$store.getters['wallets/hasWallets'])+ $store.dispatch('currencies/setBaseCurrency', values.currency)- const isFormValid = validate({ values: walletForm.value, wallets })- if (!isFormValid)- return+ await saveData(`users/${uid}/accounts/${id}`, values)+}- const walletsValues = prepareForm({ values: walletForm.value })- $store.dispatch('wallets/addWallet', { id: editWalletId, values: walletsValues })+async function onSave() {+ if (!validate(walletForm.value, $store.state.wallets.items))+ return+ await saveWalletData(editWalletId, getPreparedFormData(walletForm.value))emit('afterSave')}</script>@@ -112,7 +109,7 @@ div(v-if="walletForm")//- Data//-----------------------------------template(v-if="activeTab === 'data'")- .mb-4+ .mb-8.pb-2.text-skin-item-base-down.text-sm.leading-none {{ $t('wallets.form.name.label') }}input.w-full.m-0.py-3.px-4.rounded-lg.text-base.font-normal.text-skin-item-base.bg-skin-item-main-bg.border.border-solid.border-skin-item-main-hover.placeholder_text-skin-item-base-down.transition.ease-in-out.focus_text-skin-item-base-up.focus_bg-skin-item-main-hover.focus_border-blue3.focus_outline-none(:placeholder="$t('wallets.form.name.placeholder')"@@ -121,6 +118,15 @@ div(v-if="walletForm")@input="event => emit('updateValue', 'name', event.target.value)")+ .mb-6+ .pb-2.text-skin-item-base-down.text-sm.leading-none {{ $t('wallets.form.description.label') }}+ input.w-full.m-0.py-3.px-4.rounded-lg.text-base.font-normal.text-skin-item-base.bg-skin-item-main-bg.border.border-solid.border-skin-item-main-hover.placeholder_text-skin-item-base-down.transition.ease-in-out.focus_text-skin-item-base-up.focus_bg-skin-item-main-hover.focus_border-blue3.focus_outline-none(+ :placeholder="$t('wallets.form.description.placeholder')"+ :value="walletForm.description"+ type="text"+ @input="event => emit('updateValue', 'description', event.target.value)"+ )+SharedContextMenuItem(:checkboxValue="walletForm.countTotal":title="$t('wallets.form.total.placeholder')"diff --git a/components/wallets/getForm.ts b/components/wallets/getForm.tsnew file mode 100644index 00000000..e6d2787b--- /dev/null+++ b/components/wallets/getForm.ts@@ -0,0 +1,15 @@+import type { WalletForm } from '~/components/wallets/types'+import { allColors } from '~/assets/js/colors'+import { random } from '~/assets/js/emo'++export function getPreparedFormData(values?: any): WalletForm {+ return {+ order: values?.order ?? 1,+ name: values?.name ?? '',+ isCredit: values?.isCredit ?? false,+ description: values?.description ?? null,+ currency: values?.currency ?? 'USD',+ countTotal: values?.countTotal ?? true,+ color: values?.color ?? random(allColors),+ }+}diff --git a/components/wallets/page/New.vue b/components/wallets/page/New.vueindex 319245db..acb26e75 100644--- a/components/wallets/page/New.vue+++ b/components/wallets/page/New.vue@@ -1,26 +1,11 @@<script setup lang="ts">-import { allColors } from '~/assets/js/colors'-import { random } from '~/assets/js/emo'-import type { WalletForm } from '~/components/wallets/types'+import { getPreparedFormData } from '~/components/wallets/getForm'const router = useRouter()+const walletForm = ref(getPreparedFormData())-const walletForm = ref<WalletForm>({- color: random(random(allColors)),- countTotal: true,- currency: 'USD',- isCredit: false,- name: null,- order: 1,-})--function updateValue(id, value) {- walletForm.value[id] = value-}--function afterSave() {- router.push('/wallets/')-}+const updateValue = (id, value) => walletForm.value[id] = value+const afterSave = () => router.replace('/wallets')</script><script lang="ts">diff --git a/components/wallets/page/id/Edit.vue b/components/wallets/page/id/Edit.vueindex fb9920e4..3d978727 100644--- a/components/wallets/page/id/Edit.vue+++ b/components/wallets/page/id/Edit.vue@@ -1,7 +1,5 @@<script setup lang="ts">-import { allColors } from '~/assets/js/colors'-import { random } from '~/assets/js/emo'-import type { WalletForm } from '~/components/wallets/types'+import { getPreparedFormData } from '~/components/wallets/getForm'const { $store } = useNuxtApp()const route = useRoute()@@ -9,23 +7,10 @@ const router = useRouter()const walletId = computed(() => route.params.id)const wallet = computed(() => $store.state.wallets.items[walletId.value])+const walletForm = ref(getPreparedFormData(wallet.value))-const walletForm = ref<WalletForm>({- color: wallet.value?.color || random(allColors),- countTotal: wallet.value?.countTotal,- currency: wallet.value?.currency || 'RUB',- isCredit: wallet.value?.isCredit,- name: wallet.value?.name || null,- order: wallet.value?.order || 1,-})--function updateValue(id, value) {- walletForm.value[id] = value-}--function afterSave() {- router.push(`/wallets/${walletId.value}`)-}+const updateValue = (id, value) => walletForm.value[id] = value+const afterSave = () => router.push(`/wallets/${walletId.value}`)</script><script lang="ts">diff --git a/components/wallets/page/id/Index.vue b/components/wallets/page/id/Index.vueindex f181f74d..b8bd95d6 100644--- a/components/wallets/page/id/Index.vue+++ b/components/wallets/page/id/Index.vue@@ -74,6 +74,8 @@ UiPage(v-if="wallet"):currencyCode="wallet.currency")+ .mb-6.px-2.text-sm.text-skin-item-base-down(v-if="wallet.description") {{ wallet.description }}+.mb-6.px-2.flex.cursor-pointer.p-1.px-3.flex.items-center.gap-3.bg-gray-50.dark_bg-dark4.rounded-md.hocus_bg-gray-100.dark_hocus_bg-neutral-800.shadow.hocus_shadow-lg(diff --git a/components/wallets/types.ts b/components/wallets/types.tsindex a03d3a74..40b2c529 100644--- a/components/wallets/types.ts+++ b/components/wallets/types.ts@@ -17,4 +17,5 @@ export interface WalletForm {isCredit: booleanname: stringorder: number+ description?: string}diff --git a/locales/en-US.js b/locales/en-US.jsindex f5c2ab11..b311df62 100644--- a/locales/en-US.js+++ b/locales/en-US.js@@ -141,6 +141,10 @@ export default {error: 'Write wallet name',exist: 'Wallet with same name is exist',},+ description: {+ label: 'Wallet description (not required)',+ placeholder: 'Write...',+ },colors: {label: 'Color',placeholder: 'Select color',diff --git a/locales/ru-RU.js b/locales/ru-RU.jsindex fb4adecf..43356e4e 100644--- a/locales/ru-RU.js+++ b/locales/ru-RU.js@@ -142,6 +142,10 @@ export default {error: 'Напишите имя кошелька',exist: 'Кошелек с таким именем уже существует',},+ description: {+ label: 'Описание кошелька (не обязательно)',+ placeholder: 'Напишите...',+ },colors: {label: 'Цвет',placeholder: 'Выберите цвет',diff --git a/package.json b/package.jsonindex d13ca1b9..af06198f 100644--- a/package.json+++ b/package.json@@ -1,6 +1,6 @@{"name": "finapp",- "version": "3.8.3",+ "version": "3.8.4","scripts": {"dev": "cross-env NUXT_HOST=0.0.0.0 PORT=3010 nuxi dev","build": "nuxi generate",diff --git a/store/categories/getters.ts b/store/categories/getters.tsindex 129106da..590e6fee 100644--- a/store/categories/getters.ts+++ b/store/categories/getters.ts@@ -62,7 +62,7 @@ export default {return state.items[categoryId]?.childIds || []},- lastUsedCategoriesIdsByDate(state, getters, rootState, rootGetters) {+ recentCategoriesIds(state, getters, rootState, rootGetters) {if (!getters.hasCategories || !rootGetters['trns/hasTrns'])return []diff --git a/store/wallets/actions.js b/store/wallets/actions.jsindex c60e7057..eb966c3b 100644--- a/store/wallets/actions.js+++ b/store/wallets/actions.js@@ -1,5 +1,5 @@import localforage from 'localforage'-import { getDataAndWatch, saveData, unsubscribeData, updateData } from '~/services/firebase/api'+import { getDataAndWatch, unsubscribeData, updateData } from '~/services/firebase/api'export default {initWallets({ dispatch, rootState }) {@@ -14,25 +14,6 @@ export default {localforage.setItem('finapp.wallets', items)},- addWallet({ dispatch, rootState, getters }, { id, values }) {- const uid = rootState.user.user.uid-- const formattedValues = {- color: values.color,- countTotal: values.countTotal,- currency: values.currency,- isCredit: values.isCredit,- name: values.name,- order: parseInt(values.order) || 1,- }-- // set default currency based on first created wallet- if (!getters.hasWallets)- dispatch('currencies/setBaseCurrency', values.currency, { root: true })-- saveData(`users/${uid}/accounts/${id}`, formattedValues)- },-/*** Get object of wallets with order value* Create object with path to order field in DBdiff --git a/store/wallets/getters.js b/store/wallets/getters.jsindex 357107a9..bd5480e5 100644--- a/store/wallets/getters.js+++ b/store/wallets/getters.js@@ -2,12 +2,9 @@ import { getTrnsIds } from '~/components/trns/getTrns'import { getTotal } from '~/components/amount/getTotal'export default {- hasWallets(state, getters, rootState) {- if (rootState.wallets.items) {- if (Object.keys(rootState.wallets.items).length > 0)- return true- }- return false+ hasWallets(_state, _getters, rootState) {+ if (Object.keys(rootState.wallets.items).length > 0)+ return true},/**diff --git a/store/wallets/mutations.js b/store/wallets/mutations.jsindex 6125490c..004d3745 100644--- a/store/wallets/mutations.js+++ b/store/wallets/mutations.js@@ -1,11 +1,13 @@export default {setWallets(state, items) {- const freezedItems = {}- if (items) {- for (const itemId of Object.keys(items))- freezedItems[itemId] = Object.freeze(items[itemId])+ if (!items) {+ state.items = {}+ return}- state.items = freezedItems+ state.items = Object.keys(items).reduce((acc, id) => {+ acc[id] = Object.freeze(items[id])+ return acc+ }, {})},}