All files / app/store/transaction transaction.reducer.ts

0% Statements 0/24
0% Branches 0/5
0% Functions 0/11
0% Lines 0/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65                                                                                                                                 
import { RootState } from '..';
import {
  fetchTransactionsFail,
  addNewTransaction,
  fetchTransactionsDone,
  pendingTransactionSuccessful,
  broadcastTx,
  fetchTransactions,
} from './transaction.actions';
import { createEntityAdapter, EntityState, createReducer, createSelector } from '@reduxjs/toolkit';
import { AddressTransactionWithTransfers } from '@stacks/stacks-blockchain-api-types';
 
export interface TransactionState extends EntityState<AddressTransactionWithTransfers> {
  mostRecentBroadcastError: string | null;
  fetchTxError: string | null;
  loading: boolean;
}
 
const transactionAdapter = createEntityAdapter<AddressTransactionWithTransfers>({
  selectId: ({ tx }) => tx.tx_id,
  sortComparer: (tx1, tx2) => {
    Iif ((tx1.tx as any).is_unanchored) return -1;
    return tx2.tx.burn_block_time - tx1.tx.burn_block_time;
  },
});
 
const initialState = transactionAdapter.getInitialState({
  mostRecentBroadcastError: null as string | null,
  fetchTxError: null as string | null,
  loading: true,
});
 
export const transactionReducer = createReducer(initialState, builder =>
  builder
    .addCase(fetchTransactions, (state, action) => ({
      ...state,
      fetchTxError: null,
      loading: action.payload.displayLoading ?? true,
    }))
    .addCase(broadcastTx, state => ({ ...state, mostRecentBroadcastError: null }))
    .addCase(fetchTransactionsDone, (state, action) => ({
      ...transactionAdapter.addMany({ ...state }, action),
      loading: false,
    }))
    .addCase(fetchTransactionsFail, (state, action) => ({
      ...state,
      loading: false,
      fetchTxError: action.payload,
    }))
    .addCase(pendingTransactionSuccessful, transactionAdapter.addOne)
    .addCase(addNewTransaction, transactionAdapter.addOne)
);
 
const selectTxState = (state: RootState) => state.transaction;
const selectors = transactionAdapter.getSelectors(selectTxState);
 
export const selectTransactionList = (state: RootState) => selectors.selectAll(state);
 
export const selectTransactionsLoading = createSelector(selectTxState, state => state.loading);
 
export const selectTransactionListFetchError = createSelector(
  selectTxState,
  state => state.fetchTxError
);