Skip to content

Commit

Permalink
feat: when creating a new year, now copy the current year's categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoverth authored and simonwep committed Nov 23, 2024
1 parent 184f1e8 commit d350fb7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/store/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);
}

Expand Down
14 changes: 14 additions & 0 deletions src/store/state/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
};

0 comments on commit d350fb7

Please sign in to comment.