Skip to content

Commit

Permalink
feat: switch network in modal staking reward (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekaartajaya committed Feb 28, 2023
1 parent 2751869 commit bf5b3c4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
18 changes: 15 additions & 3 deletions src/components/molecules/Staked/UnclaimReward.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { PolkadotAccountList } from '../PolkadotAccountList';
import { BN } from '@polkadot/util';
import { IcMyriad } from 'public/icons';
import { Currency } from 'src/interface/CurrencyInterface';
import { Backdrop, CircularProgress } from '@mui/material';

interface UnclaimRewardProps {
instance: ServerListProps;
Expand All @@ -32,7 +33,7 @@ interface ShowWalletListProps {
}

export const UnclaimReward = (props: UnclaimRewardProps) => {
const { instance, onWithdrawReward } = props;
const { instance, onWithdrawReward, onChangeNetwork } = props;
const { enablePolkadotExtension, getPolkadotAccounts } =
usePolkadotExtension();

Expand All @@ -41,6 +42,7 @@ export const UnclaimReward = (props: UnclaimRewardProps) => {
const [extensionInstalled, setExtensionInstalled] = useState(false);
const [showAccountList, setShowAccountList] = useState<boolean>(false);
const [estimateFee, setEstimateFee] = useState<string>('0');
const [loading, setLoading] = useState<boolean>(false);

const hasReward = instance.rewards && instance.rewards.length > 0;

Expand All @@ -56,8 +58,9 @@ export const UnclaimReward = (props: UnclaimRewardProps) => {
getEstimateFee();
}, [getEstimateFee]);

const handleOpenModal = () => {
const handleOpenModal = async () => {
setEstimateFee('0');
if (openModal) await handleChangeNetwork?.('myriad');
setOpenModal(!openModal);
};

Expand Down Expand Up @@ -97,6 +100,12 @@ export const UnclaimReward = (props: UnclaimRewardProps) => {
handleOpenModal();
};

const handleChangeNetwork = async (networkId: string) => {
setLoading(true);
await onChangeNetwork?.(networkId, instance);
setLoading(false);
};

const showListWallet = (rewards?: Currency[], openModal = false) => {
if (!rewards || (rewards && rewards.length === 0)) {
return (
Expand Down Expand Up @@ -156,7 +165,7 @@ export const UnclaimReward = (props: UnclaimRewardProps) => {
</div>
<div className="flex items-center text-[14px] text-softGray mb-5">
<div>Network : </div>
<SwitchNetwork handleSelect={() => null} />
<SwitchNetwork onChangeNetwork={handleChangeNetwork} />
</div>
<div className="mb-4">
{showListWallet(instance.rewards, openModal)}
Expand All @@ -181,6 +190,9 @@ export const UnclaimReward = (props: UnclaimRewardProps) => {
onSelect={handleSelectedSubstrateAccount}
onClose={onCloseAccountList}
/>
<Backdrop style={{ zIndex: 1400, color: '#fff' }} open={loading}>
<CircularProgress style={{ color: '#7342CC' }} />
</Backdrop>
</React.Fragment>
);
};
46 changes: 37 additions & 9 deletions src/components/molecules/SwitchNetwork/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import { ChevronDownIcon } from '@heroicons/react/outline';
import { Button, Menu, MenuItem, SvgIcon, Typography } from '@mui/material';
import {
Backdrop,
Button,
CircularProgress,
Menu,
MenuItem,
SvgIcon,
Typography,
} from '@mui/material';
import Image from 'next/image';
import { IcBackPrimary, IcDebio, IcMyriad } from 'public/icons';
import React from 'react';
import ListNetwork from 'src/components/atoms/ListNetwork';
import ListWallet from 'src/components/atoms/ListWallet';
import { ShimerComponent } from 'src/components/organisms/ServerList/Shimer';
import { InstanceType, useInstances } from 'src/hooks/use-instances.hook';
import { ServerListProps } from 'src/interface/ServerListInterface';
import ShowIf from '../common/show-if.component';

export type NetworkOptionProps = {
handleSelect: (network: any) => void;
onChangeNetwork?: (network: string) => Promise<void>;
};

export const SwitchNetwork: React.FC<NetworkOptionProps> = ({
handleSelect,
}) => {
export const SwitchNetwork: React.FC<NetworkOptionProps> = (props) => {
const { onChangeNetwork } = props;
const { currentNetworkId, setCurrentNetworkId } = useInstances(
InstanceType.OWNED,
);

const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);

const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
Expand All @@ -23,6 +38,12 @@ export const SwitchNetwork: React.FC<NetworkOptionProps> = ({
setAnchorEl(null);
};

const handleChangeNetwork = (networkId: string) => {
onChangeNetwork?.(networkId);
setCurrentNetworkId(networkId);
handleClose();
};

return (
<>
<Button
Expand All @@ -39,7 +60,9 @@ export const SwitchNetwork: React.FC<NetworkOptionProps> = ({
size="small"
color="inherit"
>
<div className="text-[14px] capitalize text-[#12130F] ">Myriad</div>
<div className="text-[14px] capitalize text-[#12130F] ">
{currentNetworkId}
</div>
</Button>
<Menu
anchorEl={anchorEl}
Expand All @@ -48,11 +71,16 @@ export const SwitchNetwork: React.FC<NetworkOptionProps> = ({
open={Boolean(anchorEl)}
onClose={handleClose}
>
<MenuItem>
<div className="h-full w-2 bg-primary absolute left-0 rounded-r"></div>
<MenuItem onClick={() => handleChangeNetwork('myriad')}>
{currentNetworkId === 'myriad' && (
<div className="h-full w-2 bg-primary absolute left-0 rounded-r"></div>
)}
<ListNetwork image={IcMyriad} label="Myriad" />
</MenuItem>
<MenuItem>
<MenuItem onClick={() => handleChangeNetwork('debio')}>
{currentNetworkId === 'debio' && (
<div className="h-full w-2 bg-primary absolute left-0 rounded-r"></div>
)}
<ListNetwork image={IcDebio} label="Debio" />
</MenuItem>
</Menu>
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/use-instances.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,5 +394,7 @@ export const useInstances = (
balance,
totalStaked,
fetchBalance,
currentNetworkId,
setCurrentNetworkId,
};
};

0 comments on commit bf5b3c4

Please sign in to comment.