generated from multiversx/mx-template-dapp
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to switch networks (#39)
* Fixes * fixed TokenRow.tsx * Removed EGLD from sovereign transfer * Refactor * Fixed tests * Added ability to switch networks * Merge conflicts
- Loading branch information
1 parent
74654a5
commit 58148bb
Showing
27 changed files
with
519 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { useState } from 'react'; | ||
import { faChevronDown } from '@fortawesome/free-solid-svg-icons'; | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import { Button } from '../Button'; | ||
|
||
export interface DropdownOption { | ||
label: string; | ||
value: string; | ||
} | ||
|
||
interface DropdownPropsType { | ||
initialOption: DropdownOption; | ||
onSelectOption: (option: DropdownOption) => void; | ||
options: DropdownOption[]; | ||
} | ||
|
||
export const Dropdown = ({ | ||
initialOption, | ||
onSelectOption, | ||
options | ||
}: DropdownPropsType) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [selectedOption, setSelectedOption] = useState(initialOption.label); | ||
|
||
const toggleDropdown = () => { | ||
setIsOpen(!isOpen); | ||
}; | ||
|
||
const handleSelectOption = (option: DropdownOption) => () => { | ||
setSelectedOption(option.label); | ||
onSelectOption(option); | ||
toggleDropdown(); | ||
}; | ||
|
||
return ( | ||
<div className='relative inline-block text-left'> | ||
<Button | ||
aria-expanded='true' | ||
aria-haspopup='true' | ||
className='flex flex-row flex-nowrap items-center justify-center w-full px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-300 rounded-md shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-indigo-500' | ||
onClick={toggleDropdown} | ||
> | ||
{selectedOption} | ||
<FontAwesomeIcon className='-mr-1 ml-2' icon={faChevronDown} /> | ||
</Button> | ||
|
||
{isOpen && ( | ||
<div | ||
className='origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5 focus:outline-none' | ||
role='menu' | ||
aria-orientation='vertical' | ||
aria-labelledby='menu-button' | ||
tabIndex={-1} | ||
> | ||
<div className='py-1' role='none'> | ||
{options.map((option, index) => ( | ||
<Button | ||
className='block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 hover:text-gray-900' | ||
data-testid={option.value} | ||
key={`${option.value}-${index}`} | ||
onClick={handleSelectOption(option)} | ||
> | ||
{option.label} | ||
</Button> | ||
))} | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './Dropdown'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { networks } from 'config'; | ||
import { networkSelector } from 'redux/selectors'; | ||
import { changeNetwork } from 'redux/slices'; | ||
import { Dropdown, DropdownOption } from '../Dropdown'; | ||
|
||
export const NetworkSwitcher = () => { | ||
const { activeNetwork } = useSelector(networkSelector); | ||
const dispatch = useDispatch(); | ||
const networkOptions = networks.map((network) => ({ | ||
label: network.name, | ||
value: network.id | ||
})); | ||
|
||
const currentNetwork = { | ||
label: activeNetwork.name, | ||
value: activeNetwork.id | ||
}; | ||
|
||
const handleNetworkSwitch = (option: DropdownOption) => { | ||
const selectedNetwork = networks.find( | ||
(network) => network.id === option.value | ||
); | ||
|
||
if (!selectedNetwork) { | ||
return; | ||
} | ||
|
||
dispatch(changeNetwork(selectedNetwork)); | ||
}; | ||
|
||
return ( | ||
<Dropdown | ||
initialOption={currentNetwork} | ||
options={networkOptions} | ||
onSelectOption={handleNetworkSwitch} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './NetworkSwitcher'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,58 @@ | ||
import { NetworkType } from 'redux/slices'; | ||
import { EnvironmentsEnum } from 'types'; | ||
|
||
export * from './sharedConfig'; | ||
|
||
export const API_URL = 'https://devnet-api.multiversx.com'; | ||
export const GATEWAY_URL = ''; // either GATEWAY_URL or API_URL must be set | ||
export const sampleAuthenticatedDomains = [API_URL]; | ||
export const environment = EnvironmentsEnum.devnet; | ||
export const sovereignContractAddress = ''; | ||
export const WEGLDID = ''; | ||
export const networks: NetworkType[] = [ | ||
{ | ||
default: true, | ||
id: EnvironmentsEnum.devnet, | ||
name: 'Devnet', | ||
apiAddress: 'https://devnet-api.multiversx.com', | ||
gatewayUrl: '', | ||
sampleAuthenticatedDomains: ['https://devnet-api.multiversx.com'], | ||
sovereignContractAddress: '', | ||
WEGLDid: '' | ||
}, | ||
{ | ||
default: false, | ||
id: EnvironmentsEnum.devnet, | ||
name: 'Gateway', | ||
apiAddress: '', | ||
gatewayUrl: 'https://devnet-gateway.multiversx.com', | ||
sampleAuthenticatedDomains: [''], | ||
sovereignContractAddress: '', | ||
WEGLDid: '' | ||
}, | ||
{ | ||
default: false, | ||
id: EnvironmentsEnum.mainnet, | ||
name: 'Mainnet', | ||
apiAddress: 'https://api.multiversx.com', | ||
gatewayUrl: '', | ||
sampleAuthenticatedDomains: ['https://api.multiversx.com'], | ||
sovereignContractAddress: '', | ||
WEGLDid: '' | ||
}, | ||
{ | ||
default: false, | ||
id: 'sovereign', | ||
name: 'Sovereign', | ||
apiAddress: 'https://api-sovereign-test.elrond.ro', | ||
gatewayUrl: '', | ||
sampleAuthenticatedDomains: ['https://api-sovereign-test.elrond.ro'], | ||
sovereignContractAddress: | ||
'erd1qqqqqqqqqqqqqpgqfcm6l6rd42hwhskmk4thlp9kz58npfq50gfqdrthqa', | ||
WEGLDid: 'WEGLD-bd4d79' | ||
}, | ||
{ | ||
default: false, | ||
id: EnvironmentsEnum.testnet, | ||
name: 'Testnet', | ||
apiAddress: 'https://testnet-api.multiversx.com', | ||
gatewayUrl: '', | ||
sampleAuthenticatedDomains: ['https://testnet-api.multiversx.com'], | ||
sovereignContractAddress: '', | ||
WEGLDid: '' | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,58 @@ | ||
import { EnvironmentsEnum } from 'types'; | ||
import { NetworkType } from '../redux/slices'; | ||
|
||
export * from './sharedConfig'; | ||
|
||
export const API_URL = ''; // either GATEWAY_URL or API_URL must be set | ||
export const GATEWAY_URL = 'https://devnet-gateway.multiversx.com'; | ||
export const sampleAuthenticatedDomains = [API_URL]; | ||
export const environment = EnvironmentsEnum.devnet; | ||
export const sovereignContractAddress = ''; | ||
export const WEGLDID = ''; | ||
export const networks: NetworkType[] = [ | ||
{ | ||
default: false, | ||
id: EnvironmentsEnum.devnet, | ||
name: 'Devnet', | ||
apiAddress: 'https://devnet-api.multiversx.com', | ||
gatewayUrl: '', | ||
sampleAuthenticatedDomains: ['https://devnet-api.multiversx.com'], | ||
sovereignContractAddress: '', | ||
WEGLDid: '' | ||
}, | ||
{ | ||
default: true, | ||
id: EnvironmentsEnum.devnet, | ||
name: 'Gateway', | ||
apiAddress: '', | ||
gatewayUrl: 'https://devnet-gateway.multiversx.com', | ||
sampleAuthenticatedDomains: [''], | ||
sovereignContractAddress: '', | ||
WEGLDid: '' | ||
}, | ||
{ | ||
default: false, | ||
id: EnvironmentsEnum.mainnet, | ||
name: 'Mainnet', | ||
apiAddress: 'https://api.multiversx.com', | ||
gatewayUrl: '', | ||
sampleAuthenticatedDomains: ['https://api.multiversx.com'], | ||
sovereignContractAddress: '', | ||
WEGLDid: '' | ||
}, | ||
{ | ||
default: false, | ||
id: 'sovereign', | ||
name: 'Sovereign', | ||
apiAddress: 'https://api-sovereign-test.elrond.ro', | ||
gatewayUrl: '', | ||
sampleAuthenticatedDomains: ['https://api-sovereign-test.elrond.ro'], | ||
sovereignContractAddress: | ||
'erd1qqqqqqqqqqqqqpgqfcm6l6rd42hwhskmk4thlp9kz58npfq50gfqdrthqa', | ||
WEGLDid: 'WEGLD-bd4d79' | ||
}, | ||
{ | ||
default: false, | ||
id: EnvironmentsEnum.testnet, | ||
name: 'Testnet', | ||
apiAddress: 'https://testnet-api.multiversx.com', | ||
gatewayUrl: '', | ||
sampleAuthenticatedDomains: ['https://testnet-api.multiversx.com'], | ||
sovereignContractAddress: '', | ||
WEGLDid: '' | ||
} | ||
]; |
Oops, something went wrong.