mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-20 01:34:24 +08:00
code refactor
This commit is contained in:
@@ -2,17 +2,6 @@ import { CategoryType } from '@/core/category.ts';
|
|||||||
import { TransactionType } from '@/core/transaction.ts';
|
import { TransactionType } from '@/core/transaction.ts';
|
||||||
import { type TransactionCategoriesWithVisibleCount, TransactionCategory } from '@/models/transaction_category.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 {
|
export function transactionTypeToCategoryType(transactionType: TransactionType): CategoryType | null {
|
||||||
if (transactionType === TransactionType.Income) {
|
if (transactionType === TransactionType.Income) {
|
||||||
return CategoryType.Income;
|
return CategoryType.Income;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import type { ColorValue } from '@/core/color.ts';
|
||||||
import { type LocalizedPresetCategory, CategoryType } from '@/core/category.ts';
|
import { type LocalizedPresetCategory, CategoryType } from '@/core/category.ts';
|
||||||
import { DEFAULT_CATEGORY_ICON_ID } from '@/consts/icon.ts';
|
import { DEFAULT_CATEGORY_ICON_ID } from '@/consts/icon.ts';
|
||||||
import { DEFAULT_CATEGORY_COLOR } from '@/consts/color.ts';
|
import { DEFAULT_CATEGORY_COLOR } from '@/consts/color.ts';
|
||||||
@@ -6,15 +7,15 @@ export class TransactionCategory implements TransactionCategoryInfoResponse {
|
|||||||
public id: string;
|
public id: string;
|
||||||
public name: string;
|
public name: string;
|
||||||
public parentId: string;
|
public parentId: string;
|
||||||
public type: number;
|
public type: CategoryType;
|
||||||
public icon: string;
|
public icon: string;
|
||||||
public color: string;
|
public color: ColorValue;
|
||||||
public comment: string;
|
public comment: string;
|
||||||
public displayOrder: number;
|
public displayOrder: number;
|
||||||
public visible: boolean;
|
public visible: boolean;
|
||||||
public secondaryCategories?: TransactionCategory[];
|
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.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.parentId = parentId;
|
this.parentId = parentId;
|
||||||
@@ -52,6 +53,17 @@ export class TransactionCategory implements TransactionCategoryInfoResponse {
|
|||||||
return ret;
|
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 {
|
public toCreateRequest(clientSessionId: string): TransactionCategoryCreateRequest {
|
||||||
return {
|
return {
|
||||||
name: this.name,
|
name: this.name,
|
||||||
@@ -92,13 +104,13 @@ export class TransactionCategory implements TransactionCategoryInfoResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static ofMany(categoryResponses: TransactionCategoryInfoResponse[]): TransactionCategory[] {
|
public static ofMany(categoryResponses: TransactionCategoryInfoResponse[]): TransactionCategory[] {
|
||||||
const tags: TransactionCategory[] = [];
|
const categories: TransactionCategory[] = [];
|
||||||
|
|
||||||
for (const tagResponse of categoryResponses) {
|
for (const categoryResponse of categoryResponses) {
|
||||||
tags.push(TransactionCategory.of(tagResponse));
|
categories.push(TransactionCategory.of(categoryResponse));
|
||||||
}
|
}
|
||||||
|
|
||||||
return tags;
|
return categories;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ofMap(categoriesByType: Record<number, TransactionCategoryInfoResponse[]>): Record<number, TransactionCategory[]> {
|
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 { TransactionCategory } from '@/models/transaction_category.ts';
|
||||||
|
|
||||||
import { generateRandomUUID } from '@/lib/misc.ts';
|
import { generateRandomUUID } from '@/lib/misc.ts';
|
||||||
import {
|
import { allVisiblePrimaryTransactionCategoriesByType } from '@/lib/category.ts';
|
||||||
setCategoryModelByAnotherCategory,
|
|
||||||
allVisiblePrimaryTransactionCategoriesByType
|
|
||||||
} from '@/lib/category.ts';
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
props: [
|
props: [
|
||||||
@@ -176,18 +173,18 @@ export default {
|
|||||||
self.submitting = false;
|
self.submitting = false;
|
||||||
|
|
||||||
const newTransactionCategory = TransactionCategory.createNewCategory();
|
const newTransactionCategory = TransactionCategory.createNewCategory();
|
||||||
setCategoryModelByAnotherCategory(self.category, newTransactionCategory);
|
self.category.from(newTransactionCategory);
|
||||||
|
|
||||||
if (options.id) {
|
if (options.id) {
|
||||||
if (options.currentCategory) {
|
if (options.currentCategory) {
|
||||||
setCategoryModelByAnotherCategory(self.category, options.currentCategory);
|
self.category.from(options.currentCategory);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.editCategoryId = options.id;
|
self.editCategoryId = options.id;
|
||||||
self.transactionCategoriesStore.getCategory({
|
self.transactionCategoriesStore.getCategory({
|
||||||
categoryId: self.editCategoryId
|
categoryId: self.editCategoryId
|
||||||
}).then(category => {
|
}).then(category => {
|
||||||
setCategoryModelByAnotherCategory(self.category, category);
|
self.category.from(category);
|
||||||
self.loading = false;
|
self.loading = false;
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
self.loading = false;
|
self.loading = false;
|
||||||
|
|||||||
@@ -156,10 +156,7 @@ import { TransactionCategory } from '@/models/transaction_category.ts';
|
|||||||
|
|
||||||
import { getNameByKeyValue } from '@/lib/common.ts';
|
import { getNameByKeyValue } from '@/lib/common.ts';
|
||||||
import { generateRandomUUID } from '@/lib/misc.ts';
|
import { generateRandomUUID } from '@/lib/misc.ts';
|
||||||
import {
|
import { allVisiblePrimaryTransactionCategoriesByType } from '@/lib/category.ts';
|
||||||
setCategoryModelByAnotherCategory,
|
|
||||||
allVisiblePrimaryTransactionCategoriesByType
|
|
||||||
} from '@/lib/category.ts';
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
props: [
|
props: [
|
||||||
@@ -238,7 +235,7 @@ export default {
|
|||||||
self.transactionCategoriesStore.getCategory({
|
self.transactionCategoriesStore.getCategory({
|
||||||
categoryId: self.editCategoryId
|
categoryId: self.editCategoryId
|
||||||
}).then(category => {
|
}).then(category => {
|
||||||
setCategoryModelByAnotherCategory(self.category, category);
|
self.category.from(category);
|
||||||
self.loading = false;
|
self.loading = false;
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
if (error.processed) {
|
if (error.processed) {
|
||||||
|
|||||||
Reference in New Issue
Block a user