Skip to content

Commit 668b741

Browse files
committed
Cache total in transactions in Redux as heavy request
1 parent 3ea3271 commit 668b741

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

app/src/interfaces/transaction.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ export interface TransactionsActionsInterface {
1616
transactions: TransactionInterface[],
1717
transaction?: TransactionInterface,
1818
dateRange?: TransactionsRangeDateInterface,
19-
error?: string,
2019
range?: TransactionsRangeDateInterface,
20+
total?: number,
21+
error?: string,
2122
},
2223
}
2324

@@ -48,6 +49,7 @@ export interface TransactionLoading {
4849
export interface TransactionStateInterface {
4950
transactionSelected?: TransactionInterface | null,
5051
currentDateRange?: TransactionsRangeDateInterface | null,
52+
totalAllTransactions: number | null,
5153
data: TransactionInterface[];
5254
loading: TransactionLoading;
5355
modalOpen: boolean;

app/src/pages/Dashboard/Overview/index.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState, useEffect, useMemo } from 'react';
2-
import { useSelector } from 'react-redux';
2+
import { useSelector, useDispatch } from 'react-redux';
33
import api from '../../../services/api';
44

55
import { StoreInterface } from '../../../interfaces/store';
@@ -8,6 +8,7 @@ import { globalVariables } from '../../../styles/variables';
88
import Amount from '../../../components/Amount';
99
import TransactionTable from '../../../components/TransactionTable';
1010

11+
import { Creators as TransactionsActions } from '../../../store/ducks/transactions';
1112
import { Container, AmountContainer, TransactionsContainer } from './styles';
1213
import { TransactionInterface, TransactionType } from '../../../interfaces/transaction';
1314

@@ -20,6 +21,7 @@ const Overview = () => {
2021
const [loading, setLoading] = useState<boolean>(true);
2122
const [totalLoading, setTotalLoading] = useState<boolean>(true);
2223
const [totalTransactions, setTotalTransactions] = useState<number>(0);
24+
const dispatch = useDispatch();
2325

2426
const transactions = useSelector((state: StoreInterface) => state.transactions);
2527

@@ -34,9 +36,17 @@ const Overview = () => {
3436
const currentBalance = memoizedTotalCredit - memoizedTotalDebit;
3537

3638
const handleTransactionsTotalRequest = async () => {
39+
if (transactions.totalAllTransactions) {
40+
setTotalTransactions(transactions.totalAllTransactions);
41+
setTotalLoading(false);
42+
43+
return;
44+
}
45+
3746
const { data } = await api.get<TotalTransactionInterface>('/transactions/completeCashFlow');
3847
const balance = data.credit - data.debit;
3948

49+
dispatch(TransactionsActions.addTotalTransactions(balance));
4050
setTotalTransactions(balance);
4151
setTotalLoading(false);
4252
};

app/src/store/ducks/transactions.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export const Types = {
1717
UPDATE_TRANSACTION_SUCCESS: '@transaction/UPDATE_TRANSACTION_SUCCESS',
1818
TRANSACTIONS_ERROR: '@transaction/TRANSACTIONS_ERROR',
1919
TRANSACTION_MODAL_TOGGLE: '@transaction/TRANSACTION_MODAL_TOGGLE',
20+
ADD_TOTAL_TRANSACTIONS: '@transaction/ADD_TOTAL_TRANSACTIONS',
21+
CLEAR_TOTAL_TRANSACTIONS: '@transaction/CLEAR_TOTAL_TRANSACTIONS',
2022
};
2123

2224
export const LOADING_DEFAULT: TransactionLoading = {
@@ -29,6 +31,7 @@ export const LOADING_DEFAULT: TransactionLoading = {
2931
const INITIAL_STATE: TransactionStateInterface = {
3032
data: [],
3133
error: null,
34+
totalAllTransactions: null,
3235
currentDateRange: null,
3336
transactionSelected: null,
3437
loading: LOADING_DEFAULT,
@@ -39,6 +42,10 @@ export default function Transactions(state = INITIAL_STATE, action: Transactions
3942
const { payload } = action;
4043

4144
switch (action.type) {
45+
case Types.ADD_TOTAL_TRANSACTIONS:
46+
return { ...state, totalAllTransactions: payload.total };
47+
case Types.CLEAR_TOTAL_TRANSACTIONS:
48+
return { ...state, totalAllTransactions: null };
4249
case Types.GET_TRANSACTIONS_REQUEST:
4350
return {
4451
...state,
@@ -65,12 +72,14 @@ export default function Transactions(state = INITIAL_STATE, action: Transactions
6572
error: null,
6673
loading: { ...state.loading, addLoading: false },
6774
data: [payload.transaction, ...state.data],
75+
totalAllTransactions: null,
6876
};
6977
case Types.UPDATE_TRANSACTION_SUCCESS:
7078
return {
7179
...state,
7280
error: null,
7381
transactionSelected: null,
82+
totalAllTransactions: null,
7483
loading: { ...state.loading, editLoading: false },
7584
data: state.data.map(item => (
7685
payload.transaction && item.id === payload.transaction.id
@@ -80,6 +89,7 @@ export default function Transactions(state = INITIAL_STATE, action: Transactions
8089
case Types.DELETE_TRANSACTION_SUCCESS:
8190
return {
8291
...state,
92+
totalAllTransactions: null,
8393
loading: { ...state.loading, deleteLoading: false },
8494
data: state.data.filter(item => payload.transaction && item.id !== payload.transaction.id),
8595
};
@@ -134,4 +144,11 @@ export const Creators = {
134144
type: Types.TRANSACTION_MODAL_TOGGLE,
135145
payload: { transaction },
136146
}),
147+
addTotalTransactions: (total: number) => ({
148+
type: Types.ADD_TOTAL_TRANSACTIONS,
149+
payload: { total },
150+
}),
151+
clearTotalTransactions: () => ({
152+
type: Types.CLEAR_TOTAL_TRANSACTIONS,
153+
}),
137154
};

0 commit comments

Comments
 (0)