diff --git a/src/features/wallet/components/create-wallet.tsx b/src/features/wallet/components/create-wallet.tsx index 2d9625da0..e8088041b 100644 --- a/src/features/wallet/components/create-wallet.tsx +++ b/src/features/wallet/components/create-wallet.tsx @@ -11,10 +11,9 @@ import Stack from 'soapbox/components/ui/stack.tsx'; import SvgIcon from 'soapbox/components/ui/svg-icon.tsx'; import Text from 'soapbox/components/ui/text.tsx'; import { MintEditor } from 'soapbox/features/wallet/components/editable-lists.tsx'; +import { useCashu } from 'soapbox/features/zap/hooks/useCashu.ts'; import { useApi } from 'soapbox/hooks/useApi.ts'; import { useOwnAccount } from 'soapbox/hooks/useOwnAccount.ts'; -import { WalletData, baseWalletSchema } from 'soapbox/schemas/wallet.ts'; -import toast from 'soapbox/toast.tsx'; const messages = defineMessages({ title: { id: 'wallet.create_wallet.title', defaultMessage: 'You don\'t have a wallet' }, @@ -23,35 +22,24 @@ const messages = defineMessages({ mints: { id: 'wallet.mints', defaultMessage: 'Mints' }, }); -const CreateWallet: React.FC<{ setWalletData: React.Dispatch> }> = ({ setWalletData }) => { +const CreateWallet = () => { const api = useApi(); const intl = useIntl(); const { account } = useOwnAccount(); const [formActive, setFormActive] = useState(false); const [isLoading, setIsLoading] = useState(false); const [mints, setMints] = useState([]); + const { createWallet } = useCashu(); const handleSubmit = async () => { setIsLoading(true); - const wallet = { + const walletInfo = { mints: mints, relays: [], }; - try { - const response = await api.put('/api/v1/ditto/cashu/wallet', wallet); - const data = await response.json(); - if (data) { - // toast.success('Deu certo garai') - const normalizedData = baseWalletSchema.parse(data); - toast.success('Walllet Created with success'); // TO DO : create translated text - setWalletData(normalizedData); - } - } catch (e) { - toast.error('An error had occured'); // TO DO : create translated text - } - + await createWallet(api, walletInfo); setIsLoading(false); }; diff --git a/src/features/wallet/components/wallet-mints.tsx b/src/features/wallet/components/wallet-mints.tsx index cca555345..5a30df0a6 100644 --- a/src/features/wallet/components/wallet-mints.tsx +++ b/src/features/wallet/components/wallet-mints.tsx @@ -5,8 +5,8 @@ import Button from 'soapbox/components/ui/button.tsx'; import { Column } from 'soapbox/components/ui/column.tsx'; import Stack from 'soapbox/components/ui/stack.tsx'; import { MintEditor } from 'soapbox/features/wallet/components/editable-lists.tsx'; +import { useCashu } from 'soapbox/features/zap/hooks/useCashu.ts'; import { useApi } from 'soapbox/hooks/useApi.ts'; -import { WalletData, baseWalletSchema } from 'soapbox/schemas/wallet.ts'; import toast from 'soapbox/toast.tsx'; import { isURL } from 'soapbox/utils/auth.ts'; @@ -23,27 +23,12 @@ const messages = defineMessages({ const WalletMints = () => { const intl = useIntl(); const api = useApi(); + const { wallet, getWallet } = useCashu(); const [relays, setRelays] = useState([]); const [initialMints, setInitialMints] = useState([]); const [mints, setMints] = useState([]); - const fetchWallet = async () => { - try { - const response = await api.get('/api/v1/ditto/cashu/wallet'); - const data: WalletData = await response.json(); - if (data) { - const normalizedData = baseWalletSchema.parse(data); - setMints(normalizedData.mints); - setInitialMints(normalizedData.mints); - setRelays(normalizedData.relays); - } - - } catch (error) { - toast.error(intl.formatMessage(messages.loadingError)); - } - }; - const handleClick = async () =>{ if (mints.length <= 0) { toast.error(intl.formatMessage(messages.empty)); @@ -70,9 +55,20 @@ const WalletMints = () => { }; useEffect(() => { - fetchWallet(); + getWallet(api, false); + toast.error(intl.formatMessage(messages.loadingError)); }, []); + useEffect( + () => { + if (wallet) { + setMints(wallet.mints ?? []); + setInitialMints(wallet.mints ?? []); + setRelays(wallet.relays ?? []); + } + }, [wallet], + ); + return ( diff --git a/src/features/wallet/index.tsx b/src/features/wallet/index.tsx index ed11e5ca7..7f972d3d5 100644 --- a/src/features/wallet/index.tsx +++ b/src/features/wallet/index.tsx @@ -10,11 +10,10 @@ import { SelectDropdown } from 'soapbox/features/forms/index.tsx'; import Balance from 'soapbox/features/wallet/components/balance.tsx'; import CreateWallet from 'soapbox/features/wallet/components/create-wallet.tsx'; import Transactions from 'soapbox/features/wallet/components/transactions.tsx'; +import { useCashu } from 'soapbox/features/zap/hooks/useCashu.ts'; import { usePaymentMethod } from 'soapbox/features/zap/usePaymentMethod.ts'; import { useApi } from 'soapbox/hooks/useApi.ts'; import { useOwnAccount } from 'soapbox/hooks/useOwnAccount.ts'; -import { WalletData, baseWalletSchema } from 'soapbox/schemas/wallet.ts'; -import toast from 'soapbox/toast.tsx'; const messages = defineMessages({ @@ -37,29 +36,13 @@ const Wallet = () => { const intl = useIntl(); const { account } = useOwnAccount(); - const [walletData, setWalletData] = useState(undefined); + const { wallet: walletData, getWallet } = useCashu(); const [isLoading, setIsLoading] = useState(true); const { method, changeMethod } = usePaymentMethod(); - const fetchWallet = async () => { - - try { - const response = await api.get('/api/v1/ditto/cashu/wallet'); - const data: WalletData = await response.json(); - if (data) { - const normalizedData = baseWalletSchema.parse(data); - setWalletData(normalizedData); - } - - } catch (error) { - toast.error('Wallet not found'); - } finally { - setIsLoading(false); - } - }; - useEffect(() => { - fetchWallet(); + getWallet(api); + setIsLoading(false); }, []); if (!account) return null; @@ -118,7 +101,7 @@ const Wallet = () => { : <> - +