Skip to content

Commit

Permalink
update button
Browse files Browse the repository at this point in the history
  • Loading branch information
wendychaung committed May 20, 2023
1 parent 6c727b7 commit ac28de3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/packages/Widget/ButtonBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ export default function ButtonBox(props:ButtonProps){
const [account,setAccount] =useState<string>('');
const [error,setError] =useState<string>('');
const [loading,setLoading] = useState(false);



const [showText,setShowText] = useState(false);
const [currentChain,setCurrentChain] = useState(1);

const getAccount = async () =>{
const { ethereum } = window as any;
Expand All @@ -32,6 +31,39 @@ export default function ButtonBox(props:ButtonProps){
PublicObj.chainChange(accept,0);
},[])


useEffect(()=>{
setError('');
getChain()
},[current])

useEffect(()=>{

const arr = GeneralConfig.chainList.filter(item=>item.blockchain === accept[current].blockchain);
console.log(arr)
const arrChain = eval(arr[0].onlineChainId!).toString(10);
console.error(parseInt(arrChain),currentChain,parseInt(arrChain) === currentChain)
if(parseInt(arrChain) !== currentChain){
setShowText(true)
}else{
setShowText(false)
}

},[current,currentChain])

useEffect(()=>{
(window as any).ethereum.on('chainChanged', () => {
setShowText(false)
});
},[])

const getChain = async() =>{
const { ethereum } = window as any;
const web3Provider = new ethers.providers.Web3Provider(ethereum);
const {chainId} = await web3Provider.getNetwork();
setCurrentChain(chainId)
}

const handleTransfer = async () =>{
setLoading(true);
setError('')
Expand Down Expand Up @@ -88,7 +120,7 @@ export default function ButtonBox(props:ButtonProps){
loading &&<Loading />
}

<span>Pay</span>
<span>{showText?"Switch chain":"Pay"}</span>
</button>
</div>
}
1 change: 1 addition & 0 deletions src/packages/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const GeneralConfig = {
chainList:[
{
onlineChainId: '0x1',
blockchain: 'ethereum',
chainName:"Ethereum",
logo:"https://web3camp.us/LogoCollection/static/media/ethereum-eth.a591fb8fd8f17e30185b.png",
Expand Down

0 comments on commit ac28de3

Please sign in to comment.