diff --git a/src/models/transaction_category.ts b/src/models/transaction_category.ts index c0058040..869a148b 100644 --- a/src/models/transaction_category.ts +++ b/src/models/transaction_category.ts @@ -127,6 +127,16 @@ export class TransactionCategory implements TransactionCategoryInfoResponse { return ret; } + public static findNameById(categories: TransactionCategory[], id: string): string | null { + for (const category of categories) { + if (category.id === id) { + return category.name; + } + } + + return null; + } + public static createNewCategory(type?: CategoryType, parentId?: string): TransactionCategory { return new TransactionCategory('', '', parentId || '0', type || CategoryType.Income, DEFAULT_CATEGORY_ICON_ID, DEFAULT_CATEGORY_COLOR, '', 0, true); } diff --git a/src/views/base/categories/CategoryEditPageBase.ts b/src/views/base/categories/CategoryEditPageBase.ts new file mode 100644 index 00000000..583facc7 --- /dev/null +++ b/src/views/base/categories/CategoryEditPageBase.ts @@ -0,0 +1,65 @@ +import { ref, computed } from 'vue'; + +import { useTransactionCategoriesStore } from '@/stores/transactionCategory.ts'; + +import { CategoryType } from '@/core/category.ts'; +import { TransactionCategory } from '@/models/transaction_category.ts'; + +import { allVisiblePrimaryTransactionCategoriesByType } from '@/lib/category.ts'; + +export function useCategoryEditPageBase(type?: CategoryType, parentId?: string) { + const transactionCategoriesStore = useTransactionCategoriesStore(); + + const editCategoryId = ref(null); + const clientSessionId = ref(''); + const loading = ref(false); + const submitting = ref(false); + const category = ref(TransactionCategory.createNewCategory(type, parentId)); + + const allAvailableCategories = computed(() => allVisiblePrimaryTransactionCategoriesByType(transactionCategoriesStore.allTransactionCategories, category.value.type)); + + const title = computed(() => { + if (!editCategoryId.value) { + if (category.value.parentId === '0') { + return 'Add Primary Category'; + } else { + return 'Add Secondary Category'; + } + } else { + return 'Edit Category'; + } + }); + + const saveButtonTitle = computed(() => { + if (!editCategoryId.value) { + return 'Add'; + } else { + return 'Save'; + } + }); + + const inputEmptyProblemMessage = computed(() => { + if (!category.value.name) { + return 'Category name cannot be blank'; + } else { + return null; + } + }); + + const inputIsEmpty = computed(() => !!inputEmptyProblemMessage.value); + + return { + // states + editCategoryId, + clientSessionId, + loading, + submitting, + category, + // computed states + allAvailableCategories, + title, + saveButtonTitle, + inputEmptyProblemMessage, + inputIsEmpty + }; +} diff --git a/src/views/desktop/categories/list/dialogs/EditDialog.vue b/src/views/desktop/categories/list/dialogs/EditDialog.vue index 9af84eec..ee52959f 100644 --- a/src/views/desktop/categories/list/dialogs/EditDialog.vue +++ b/src/views/desktop/categories/list/dialogs/EditDialog.vue @@ -3,7 +3,7 @@ @@ -15,8 +15,8 @@ type="text" persistent-placeholder :disabled="loading || submitting" - :label="$t('Category Name')" - :placeholder="$t('Category Name')" + :label="tt('Category Name')" + :placeholder="tt('Category Name')" v-model="category.name" /> @@ -26,10 +26,10 @@ item-value="id" persistent-placeholder :disabled="loading || submitting" - :label="$t('Primary Category')" - :placeholder="$t('Primary Category')" + :label="tt('Primary Category')" + :placeholder="tt('Primary Category')" :items="allAvailableCategories" - :no-data-text="$t('No available primary category')" + :no-data-text="tt('No available primary category')" v-model="category.parentId" > - diff --git a/src/views/mobile/categories/EditPage.vue b/src/views/mobile/categories/EditPage.vue index 3cb3bb17..5949ae58 100644 --- a/src/views/mobile/categories/EditPage.vue +++ b/src/views/mobile/categories/EditPage.vue @@ -1,10 +1,10 @@ - + @@ -145,8 +145,14 @@ -