Skip to content

Commit

Permalink
fix: monthly budget update
Browse files Browse the repository at this point in the history
  • Loading branch information
begprod committed Sep 2, 2024
1 parent 23b4cbf commit 2cafdb3
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 20 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Changelog
## 0.9.2
## 0.9.3

### Bug Fixes

Expand Down Expand Up @@ -45,7 +45,7 @@
### Bug Fixes

- input z-index

## 0.7.1

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "budget.it",
"version": "0.9.2",
"version": "0.9.3",
"description": "Another budget app, but this one is handy",
"repository": "https://github.com/begprod/budget.it",
"homepage": "https://begprod.github.io/budget.it/",
Expand Down
2 changes: 0 additions & 2 deletions src/components/BaseExpensesList/BaseExpensesList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,9 @@ describe('BaseExpensesList', () => {
monthlyDailyBudget.value = {
'032024': {
dailyBudget: dailyBudget.value,
isCurrent: false,
},
'042024': {
dailyBudget: 900,
isCurrent: true,
},
};

Expand Down
10 changes: 2 additions & 8 deletions src/stores/settings/settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,12 @@ describe('Settings store', () => {
});

it('should set daily budget', () => {
const currentMonth = Object.keys(getMonthlyDailyBudget.value).find(
(key: string) => getMonthlyDailyBudget.value[key].isCurrent,
);
const currentMonth = new Date().toLocaleDateString().substring(3, 10).replace('.', '');

setDailyBudget(1000);

const currentMonthDailyBudget = currentMonth
? getMonthlyDailyBudget.value[currentMonth].dailyBudget
: NaN;

expect(dailyBudget.value).toEqual(1000);
expect(currentMonthDailyBudget).toEqual(1000);
expect(getMonthlyDailyBudget.value[currentMonth].dailyBudget).toEqual(1000);
});

it('should add new currency', () => {
Expand Down
13 changes: 7 additions & 6 deletions src/stores/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const useSettingsStore = defineStore('settings', {
}),

getters: {
getMonthlyDailyBudget(state): Record<string, { dailyBudget: number; isCurrent: boolean }> {
getMonthlyDailyBudget(state): Record<string, { dailyBudget: number }> {
return state.monthlyDailyBudget;
},
getActiveCurrency(state): ICurrency {
Expand All @@ -48,14 +48,17 @@ export const useSettingsStore = defineStore('settings', {

actions: {
initMonthlyDailyBudgetObject() {
if (Object.keys(this.monthlyDailyBudget).length !== 0) {
return;
}

const monthsList = generateMonths(5);
const nextMonth = generateMonths(0, 1);
const allMonths = [...nextMonth, ...monthsList];

allMonths.forEach((month: IMonth) => {
this.monthlyDailyBudget[month.id] = {
dailyBudget: this.dailyBudget,
isCurrent: month.isCurrent,
};
});
},
Expand All @@ -67,11 +70,9 @@ export const useSettingsStore = defineStore('settings', {
});
},
setDailyBudget(value: number) {
const currentMonth = Object.keys(this.monthlyDailyBudget).find(
(key: string) => this.monthlyDailyBudget[key].isCurrent,
);
const currentMonth = new Date().toLocaleDateString().substring(3, 10).replace('.', '');

if (!currentMonth) {
if (!this.monthlyDailyBudget[currentMonth]) {
throw new Error('Current month not found');
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface IExpense {
export interface ISettingsStore {
currencies: RemovableRef<Array<ICurrency>>;
dailyBudget: RemovableRef<number>;
monthlyDailyBudget: RemovableRef<Record<string, { dailyBudget: number; isCurrent: boolean }>>;
monthlyDailyBudget: RemovableRef<Record<string, { dailyBudget: number }>>;
}

export interface ICurrency {
Expand Down

0 comments on commit 2cafdb3

Please sign in to comment.