Commit 866bea44 authored by Benjamin Bouvier's avatar Benjamin Bouvier
Browse files

client: don't fetch the budget twice when changing budget date

This was sending the fetch-budget request twice:

- once in `setPeriod`
- once because the effect would be triggered, noticing budgets haven't
  been set for this particular date combination, thus it would fetch
  them from the server.

Instead, make `setPeriod` a pure UI request, and just rely on the effect
for fetching the budgets.
parent 815ba375
Pipeline #407758 passed with stage
in 4 minutes and 4 seconds
......@@ -139,8 +139,9 @@ const BudgetsList = (): ReactElement => {
const currentView = useContext(ViewContext);
const setPeriod = useGenericError(
useCallback((year, month) => actions.setBudgetsPeriod(dispatch, year, month), [dispatch])
const setPeriod = useCallback(
(year, month) => actions.setBudgetsPeriod(dispatch, year, month),
[dispatch]
);
const fetchBudgets = useGenericError(
......
......@@ -15,7 +15,6 @@ import {
import { SET_BUDGETS_PERIOD, FETCH_BUDGETS, UPDATE_BUDGET, RESET_BUDGETS } from './actions';
import { assertDefined } from '../helpers';
import { GetStateType } from '.';
// State structure.
export interface BudgetState {
......@@ -108,15 +107,9 @@ function reduceFetch(state: BudgetState, action: Action<FetchBudgetParams>): Bud
// Set the selected period for budgets.
export function setSelectedPeriod(year: number, month: number) {
return (dispatch: Dispatch, getState: GetStateType): Promise<void> => {
return (dispatch: Dispatch) => {
const action = setPeriodAction({ year, month });
dispatch(action);
if (!fromSelectedPeriod(getState().budgets)) {
return fetchFromYearAndMonth(year, month)(dispatch);
}
return Promise.resolve();
};
}
......
......@@ -436,7 +436,7 @@ export const actions = {
// *** Budgets ************************************************************
setBudgetsPeriod(dispatch: Dispatch, year: number, month: number) {
return dispatch(BudgetStore.setSelectedPeriod(year, month));
dispatch(BudgetStore.setSelectedPeriod(year, month));
},
fetchBudgetsByYearMonth(dispatch: Dispatch, year: number, month: number) {
return dispatch(BudgetStore.fetchFromYearAndMonth(year, month));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment