All files / src/store wallet-store.ts

100% Statements 28/28
100% Branches 6/6
100% Functions 22/22
100% Lines 25/25

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                                                                                                                        13x   13x                                 8x       8x       4x           3x   4x           3x 6x         4x   4x 3x 3x     4x 4x 3x 3x       6x 6x 5x 5x   5x       5x                   58x                             120x              
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
 
// Import types from the wallet feature for consistency
export interface WalletData {
  totalEarnings: string;
  balance: string;
  currency: string;
}
 
export interface Transaction {
  id: string;
  amount: string;
  type: 'deposit' | 'withdrawal' | 'transfer';
  status: 'pending' | 'completed' | 'failed';
  date: string;
  description?: string;
}
 
export interface WalletState {
  // Wallet data
  walletData: WalletData | null;
  transactions: Transaction[];
 
  // Loading states
  isWalletLoading: boolean;
  isTransactionsLoading: boolean;
  isWithdrawing: boolean;
  isDepositing: boolean;
 
  // Error states
  walletError: string | null;
  transactionsError: string | null;
  withdrawError: string | null;
  depositError: string | null;
 
  // Actions
  setWalletData: (data: WalletData) => void;
  setTransactions: (transactions: Transaction[]) => void;
  addTransaction: (transaction: Transaction) => void;
  updateTransaction: (id: string, updates: Partial<Transaction>) => void;
  removeTransaction: (id: string) => void;
 
  // Loading actions
  setWalletLoading: (loading: boolean) => void;
  setTransactionsLoading: (loading: boolean) => void;
  setWithdrawing: (loading: boolean) => void;
  setDepositing: (loading: boolean) => void;
 
  // Error actions
  setWalletError: (error: string | null) => void;
  setTransactionsError: (error: string | null) => void;
  setWithdrawError: (error: string | null) => void;
  setDepositError: (error: string | null) => void;
 
  // Complex actions
  updateBalance: (amount: string, type: 'add' | 'subtract') => void;
  clearWalletData: () => void;
}
 
export const useWalletStore = create<WalletState>()(
  persist(
    (set, get) => ({
      // Initial state
      walletData: null,
      transactions: [],
 
      isWalletLoading: false,
      isTransactionsLoading: false,
      isWithdrawing: false,
      isDepositing: false,
 
      walletError: null,
      transactionsError: null,
      withdrawError: null,
      depositError: null,
 
      // Actions
      setWalletData: data => {
        set({ walletData: data, walletError: null });
      },
 
      setTransactions: transactions => {
        set({ transactions, transactionsError: null });
      },
 
      addTransaction: transaction => {
        set(state => ({
          transactions: [transaction, ...state.transactions],
        }));
      },
 
      updateTransaction: (id, updates) => {
        set(state => ({
          transactions: state.transactions.map(t =>
            t.id === id ? { ...t, ...updates } : t
          ),
        }));
      },
 
      removeTransaction: id => {
        set(state => ({
          transactions: state.transactions.filter(t => t.id !== id),
        }));
      },
 
      // Loading actions
      setWalletLoading: loading => set({ isWalletLoading: loading }),
      setTransactionsLoading: loading =>
        set({ isTransactionsLoading: loading }),
      setWithdrawing: loading => set({ isWithdrawing: loading }),
      setDepositing: loading => set({ isDepositing: loading }),
 
      // Error actions
      setWalletError: error => set({ walletError: error }),
      setTransactionsError: error => set({ transactionsError: error }),
      setWithdrawError: error => set({ withdrawError: error }),
      setDepositError: error => set({ depositError: error }),
 
      // Complex actions
      updateBalance: (amount, type) => {
        const currentData = get().walletData;
        if (currentData) {
          const currentBalance = parseFloat(currentData.balance);
          const changeAmount = parseFloat(amount);
          const newBalance =
            type === 'add'
              ? currentBalance + changeAmount
              : currentBalance - changeAmount;
 
          set({
            walletData: {
              ...currentData,
              balance: newBalance.toFixed(2),
            },
          });
        }
      },
 
      clearWalletData: () =>
        set({
          walletData: null,
          transactions: [],
          walletError: null,
          transactionsError: null,
          withdrawError: null,
          depositError: null,
          isWalletLoading: false,
          isTransactionsLoading: false,
          isWithdrawing: false,
          isDepositing: false,
        }),
    }),
    {
      name: 'wallet-storage',
      partialize: state => ({
        walletData: state.walletData,
        transactions: state.transactions,
      }),
    }
  )
);