From 414ef4fd3853ddc035b71f50fc61e5c55b6103e0 Mon Sep 17 00:00:00 2001 From: Thomas Dickson Date: Thu, 14 Nov 2024 15:35:38 +1300 Subject: [PATCH] feat: when creating a new year, now copy the current year's categories --- src/store/state/index.ts | 7 +++---- src/store/state/utils.ts | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/store/state/index.ts b/src/store/state/index.ts index 9cd10bde..ad33b8a2 100644 --- a/src/store/state/index.ts +++ b/src/store/state/index.ts @@ -5,7 +5,7 @@ import { moveInArrays, readFile, remove, uuid } from '@utils'; import { DeepReadonly, inject, reactive, readonly, ShallowRef, shallowRef, watch } from 'vue'; import { migrateApplicationState } from './migrator'; import { AvailableCurrency, Budget, BudgetGroup, BudgetYear, DataState, DataStates, DataStateV1 } from './types'; -import { generateBudgetYear } from './utils'; +import { generateBudgetYearFromCurrent } from './utils'; export const DATA_STORE_KEY = Symbol('DataStore'); @@ -181,11 +181,10 @@ export const createDataStore = (storage?: Storage): Store => { }, changeYear(year: number) { - let data = state.years.find((v) => v.year === year); + const data = state.years.find((v) => v.year === year); if (!data) { - data = generateBudgetYear(year); - state.years.push(data); + state.years.push(generateBudgetYearFromCurrent(year, getCurrentYear())); state.years.sort((a, b) => a.year - b.year); } diff --git a/src/store/state/utils.ts b/src/store/state/utils.ts index 0fb4ef5a..30b1ec0a 100644 --- a/src/store/state/utils.ts +++ b/src/store/state/utils.ts @@ -18,3 +18,17 @@ export const generateBudgetYear = (year: number): BudgetYear => ({ income: [generateBudgetGroup('General', ['Other'])], expenses: [generateBudgetGroup('General', ['Other'])] }); + +export const generateBudgetYearFromCurrent = (year: number, current: BudgetYear): BudgetYear => { + const clearValues = (value: BudgetGroup): BudgetGroup => ({ + id: uuid(), + name: value.name, + budgets: value.budgets.map((budget) => generateBudget(budget.name)) + }); + + return { + year, + income: current.income.map(clearValues), + expenses: current.expenses.map(clearValues) + }; +};