diff --git a/src/store/state/index.ts b/src/store/state/index.ts index 9cd10bd..ad33b8a 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 0fb4ef5..30b1ec0 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) + }; +};