kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
Refactor fetch code
rodzic
1d2c7b2de6
commit
b8fa6631ad
|
@ -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<React.SetStateAction<WalletData | undefined>> }> = ({ 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<string[]>([]);
|
||||
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);
|
||||
};
|
||||
|
||||
|
|
|
@ -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<string[]>([]);
|
||||
const [initialMints, setInitialMints] = useState<string[]>([]);
|
||||
const [mints, setMints] = useState<string[]>([]);
|
||||
|
||||
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 (
|
||||
<Column label={intl.formatMessage(messages.title)} >
|
||||
<Stack space={2}>
|
||||
|
|
|
@ -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<WalletData | undefined>(undefined);
|
||||
const { wallet: walletData, getWallet } = useCashu();
|
||||
const [isLoading, setIsLoading] = useState<boolean>(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 = () => {
|
|||
:
|
||||
<>
|
||||
<CardBody>
|
||||
<CreateWallet setWalletData={setWalletData} />
|
||||
<CreateWallet />
|
||||
</CardBody>
|
||||
|
||||
</>
|
||||
|
|
Ładowanie…
Reference in New Issue