code refactor

This commit is contained in:
MaysWind
2025-01-16 23:16:20 +08:00
parent 4f21762533
commit 6878d5260d
4 changed files with 25 additions and 30 deletions
-11
View File
@@ -2,17 +2,6 @@ import { CategoryType } from '@/core/category.ts';
import { TransactionType } from '@/core/transaction.ts';
import { type TransactionCategoriesWithVisibleCount, TransactionCategory } from '@/models/transaction_category.ts';
export function setCategoryModelByAnotherCategory(category: TransactionCategory, category2: TransactionCategory): void {
category.id = category2.id;
category.type = category2.type;
category.parentId = category2.parentId;
category.name = category2.name;
category.icon = category2.icon;
category.color = category2.color;
category.comment = category2.comment;
category.visible = !category2.hidden;
}
export function transactionTypeToCategoryType(transactionType: TransactionType): CategoryType | null {
if (transactionType === TransactionType.Income) {
return CategoryType.Income;
+19 -7
View File
@@ -1,3 +1,4 @@
import type { ColorValue } from '@/core/color.ts';
import { type LocalizedPresetCategory, CategoryType } from '@/core/category.ts';
import { DEFAULT_CATEGORY_ICON_ID } from '@/consts/icon.ts';
import { DEFAULT_CATEGORY_COLOR } from '@/consts/color.ts';
@@ -6,15 +7,15 @@ export class TransactionCategory implements TransactionCategoryInfoResponse {
public id: string;
public name: string;
public parentId: string;
public type: number;
public type: CategoryType;
public icon: string;
public color: string;
public color: ColorValue;
public comment: string;
public displayOrder: number;
public visible: boolean;
public secondaryCategories?: TransactionCategory[];
private constructor(id: string, name: string, parentId: string, type: number, icon: string, color: string, comment: string, displayOrder: number, visible: boolean, secondaryCategories?: TransactionCategory[]) {
private constructor(id: string, name: string, parentId: string, type: CategoryType, icon: string, color: ColorValue, comment: string, displayOrder: number, visible: boolean, secondaryCategories?: TransactionCategory[]) {
this.id = id;
this.name = name;
this.parentId = parentId;
@@ -52,6 +53,17 @@ export class TransactionCategory implements TransactionCategoryInfoResponse {
return ret;
}
public from(other: TransactionCategory): void {
this.id = other.id;
this.name = other.name;
this.parentId = other.parentId;
this.type = other.type;
this.icon = other.icon;
this.color = other.color;
this.comment = other.comment;
this.visible = other.visible;
}
public toCreateRequest(clientSessionId: string): TransactionCategoryCreateRequest {
return {
name: this.name,
@@ -92,13 +104,13 @@ export class TransactionCategory implements TransactionCategoryInfoResponse {
}
public static ofMany(categoryResponses: TransactionCategoryInfoResponse[]): TransactionCategory[] {
const tags: TransactionCategory[] = [];
const categories: TransactionCategory[] = [];
for (const tagResponse of categoryResponses) {
tags.push(TransactionCategory.of(tagResponse));
for (const categoryResponse of categoryResponses) {
categories.push(TransactionCategory.of(categoryResponse));
}
return tags;
return categories;
}
public static ofMap(categoriesByType: Record<number, TransactionCategoryInfoResponse[]>): Record<number, TransactionCategory[]> {
@@ -103,10 +103,7 @@ import { ALL_CATEGORY_COLORS } from '@/consts/color.ts';
import { TransactionCategory } from '@/models/transaction_category.ts';
import { generateRandomUUID } from '@/lib/misc.ts';
import {
setCategoryModelByAnotherCategory,
allVisiblePrimaryTransactionCategoriesByType
} from '@/lib/category.ts';
import { allVisiblePrimaryTransactionCategoriesByType } from '@/lib/category.ts';
export default {
props: [
@@ -176,18 +173,18 @@ export default {
self.submitting = false;
const newTransactionCategory = TransactionCategory.createNewCategory();
setCategoryModelByAnotherCategory(self.category, newTransactionCategory);
self.category.from(newTransactionCategory);
if (options.id) {
if (options.currentCategory) {
setCategoryModelByAnotherCategory(self.category, options.currentCategory);
self.category.from(options.currentCategory);
}
self.editCategoryId = options.id;
self.transactionCategoriesStore.getCategory({
categoryId: self.editCategoryId
}).then(category => {
setCategoryModelByAnotherCategory(self.category, category);
self.category.from(category);
self.loading = false;
}).catch(error => {
self.loading = false;
+2 -5
View File
@@ -156,10 +156,7 @@ import { TransactionCategory } from '@/models/transaction_category.ts';
import { getNameByKeyValue } from '@/lib/common.ts';
import { generateRandomUUID } from '@/lib/misc.ts';
import {
setCategoryModelByAnotherCategory,
allVisiblePrimaryTransactionCategoriesByType
} from '@/lib/category.ts';
import { allVisiblePrimaryTransactionCategoriesByType } from '@/lib/category.ts';
export default {
props: [
@@ -238,7 +235,7 @@ export default {
self.transactionCategoriesStore.getCategory({
categoryId: self.editCategoryId
}).then(category => {
setCategoryModelByAnotherCategory(self.category, category);
self.category.from(category);
self.loading = false;
}).catch(error => {
if (error.processed) {