Skip to content

Commit

Permalink
Create useWallet.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent a57b168 commit 179306a
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions projects/PiWalletBot/hooks/useWallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { useState, useEffect } from 'react';
import { useApi } from './useApi';
import { useAuth } from './useAuth';

const useWallet = () => {
const [balance, setBalance] = useState(0);
const [transactions, setTransactions] = useState([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const api = useApi();
const { user, token } = useAuth();

useEffect(() => {
const getBalance = async () => {
try {
setLoading(true);
const response = await api.get('/pi/balance', { headers: { Authorization: `Bearer ${token}` } });
setBalance(response.data);
} catch (error) {
setError(error);
} finally {
setLoading(false);
}
};
if (user) {
getBalance();
}
}, [user, token]);

useEffect(() => {
const getTransactions = async () => {
try {
setLoading(true);
const response = await api.get('/pi/transactions', { headers: { Authorization: `Bearer ${token}` } });
setTransactions(response.data);
} catch (error) {
setError(error);
} finally {
setLoading(false);
} finally {
setLoading(false);
}
};
if (user) {
getTransactions();
}
}, [user, token]);

const sendTransaction = async (recipient, amount) => {
try {
setLoading(true);
const response = await api.post('/pi/transactions', { recipient, amount }, { headers: { Authorization: `Bearer ${token}` } });
setTransactions([...transactions, response.data]);
} catch (error) {
setError(error);
} finally {
setLoading(false);
}
};

return { balance, transactions, loading, error, getBalance, getTransactions, sendTransaction };
};

export default useWallet;

0 comments on commit 179306a

Please sign in to comment.