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

0% Statements 0/48
0% Branches 0/19
0% Functions 0/15
0% Lines 0/41

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91                                                                                                                                                                                     
import { RootState } from '..';
import { createSlice, createSelector } from '@reduxjs/toolkit';
import { selectAddressBalance as selectAddressDetails } from '@store/address';
import { selectIsStackingCallPending } from '@store/pending-transaction';
import {
  selectActiveStackingTxId,
  selectLoadingStacking,
  selectStackerInfo,
  selectStackingError,
} from '@store/stacking';
 
type HomeModals = 'txModal' | 'receiveModal' | 'revokeDelegationModal';
 
export interface HomeState {
  activeModal: HomeModals | null;
}
 
const initialState: HomeState = {
  activeModal: null,
};
 
export const homeSlice = createSlice({
  name: 'home',
  initialState,
  reducers: {
    //
    // TODO: refactor post-delegation
    openTxModal: () => ({ activeModal: 'txModal' as HomeModals }),
    closeTxModal: () => ({ activeModal: null }),
    openReceiveModal: () => ({ activeModal: 'receiveModal' as HomeModals }),
    closeReceiveModal: () => ({ activeModal: null }),
    openRevokeDelegationModal: () => ({ activeModal: 'revokeDelegationModal' as HomeModals }),
    closeRevokeDelegationModal: () => ({ activeModal: null }),
  },
});
 
export const homeActions = homeSlice.actions;
 
export const selectHomeState = (state: RootState) => state.home;
 
export const selectTxModalOpen = createSelector(
  selectHomeState,
  state => state.activeModal === 'txModal'
);
export const selectReceiveModalOpen = createSelector(
  selectHomeState,
  state => state.activeModal === 'receiveModal'
);
export const selectRevokeDelegationModalOpen = createSelector(
  selectHomeState,
  state => state.activeModal === 'revokeDelegationModal'
);
 
export enum HomeCardState {
  LoadingResources,
  NotEnoughStx,
  EligibleToParticipate,
  StackingPendingContactCall,
  StackingPreCycle,
  StackingActive,
  PostStacking,
  StackingError,
}
 
const selectLoadingCardResources = createSelector(
  selectAddressDetails,
  selectLoadingStacking,
  (balance, isLoadingStacking) => balance === null || isLoadingStacking
);
 
const selectShowErrorCard = createSelector(selectStackingError, state => {
  return Object.values(state).some(val => val === true);
});
 
export const selectHomeCardState = createSelector(
  selectLoadingCardResources,
  selectActiveStackingTxId,
  selectIsStackingCallPending,
  selectStackerInfo,
  selectShowErrorCard,
  (loadingResources, activeStackingTxId, stackingCallPending, stackerInfo, stackingErr) => {
    Iif (stackingErr) return HomeCardState.StackingError;
    Iif (loadingResources) return HomeCardState.LoadingResources;
    Iif (stackingCallPending || typeof activeStackingTxId === 'string')
      return HomeCardState.StackingPendingContactCall;
    Iif (stackerInfo?.isPreStackingPeriodStart) return HomeCardState.StackingPreCycle;
    Iif (stackerInfo?.isCurrentlyStacking) return HomeCardState.StackingActive;
    return HomeCardState.EligibleToParticipate;
  }
);