-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: pooling modal * restructure utility functions * fix test * Display 'Task: Instantaneous Pool' --------- Co-authored-by: Sergej <sakacszergej@gmail.com>
- Loading branch information
Showing
34 changed files
with
527 additions
and
218 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
26 changes: 26 additions & 0 deletions
26
src/components/Elements/Selectors/FinalitySelector/index.module.scss
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,26 @@ | ||
.options { | ||
display: flex; | ||
justify-content: center; | ||
} | ||
|
||
.option, | ||
.activeOption { | ||
min-width: 15rem; | ||
margin: 1rem 5rem; | ||
border-radius: 0.5rem !important; | ||
text-transform: capitalize; | ||
} | ||
|
||
.activeOption { | ||
border: 1px solid #e84d68; | ||
background: linear-gradient(180deg, #e84d68 0%, #ad2b49 100%); | ||
-webkit-background-clip: text; | ||
-webkit-text-fill-color: transparent; | ||
background-clip: text; | ||
} | ||
|
||
.option { | ||
border: 1px solid #cccccc !important; | ||
background-color: white; | ||
color: black; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/components/Elements/Selectors/FinalitySelector/index.tsx
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,27 @@ | ||
import { MenuItem, Select } from '@mui/material'; | ||
|
||
import { FinalityType } from '@/models'; | ||
|
||
interface FinalitySelectorProps { | ||
finality: FinalityType; | ||
setFinality: (_: FinalityType) => void; | ||
} | ||
|
||
export const FinalitySelector = ({ | ||
finality, | ||
setFinality, | ||
}: FinalitySelectorProps) => { | ||
return ( | ||
<Select | ||
value={finality} | ||
onChange={(e) => setFinality(e.target.value as FinalityType)} | ||
fullWidth | ||
> | ||
{Object.values(FinalityType).map((value, index) => ( | ||
<MenuItem key={index} value={value}> | ||
{value} | ||
</MenuItem> | ||
))} | ||
</Select> | ||
); | ||
}; |
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,4 +1,5 @@ | ||
export * from './AssetSelector'; | ||
export * from './ChainSelector'; | ||
export * from './FinalitySelector'; | ||
export * from './RecipientSelector'; | ||
export * from './RegionSelector'; |
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,26 @@ | ||
.container { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 0.75rem; | ||
min-width: 35rem; | ||
} | ||
|
||
.content { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 0.75rem; | ||
margin: 1rem 0; | ||
} | ||
|
||
.options { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 1rem; | ||
padding: 1rem; | ||
} | ||
|
||
.optionItem { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 0.5rem; | ||
} |
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,173 @@ | ||
import { | ||
Box, | ||
Button, | ||
Dialog, | ||
DialogActions, | ||
DialogContent, | ||
Input, | ||
InputAdornment, | ||
Paper, | ||
Typography, | ||
useTheme, | ||
} from '@mui/material'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { isValidAddress } from '@/utils/functions'; | ||
|
||
import { | ||
FinalitySelector, | ||
ProgressButton, | ||
SimpleRegionCard, | ||
} from '@/components/Elements'; | ||
|
||
import { useAccounts } from '@/contexts/account'; | ||
import { useCoretimeApi } from '@/contexts/apis'; | ||
import { useRegions } from '@/contexts/regions'; | ||
import { useToast } from '@/contexts/toast'; | ||
import { FinalityType, RegionMetadata } from '@/models'; | ||
|
||
import styles from './index.module.scss'; | ||
|
||
interface PoolingModalProps { | ||
open: boolean; | ||
onClose: () => void; | ||
regionMetadata: RegionMetadata; | ||
} | ||
|
||
export const PoolingModal = ({ | ||
open, | ||
onClose, | ||
regionMetadata, | ||
}: PoolingModalProps) => { | ||
const theme = useTheme(); | ||
const { | ||
state: { activeAccount, activeSigner }, | ||
} = useAccounts(); | ||
|
||
const { | ||
state: { api: coretimeApi }, | ||
} = useCoretimeApi(); | ||
const { fetchRegions } = useRegions(); | ||
const { toastError, toastInfo, toastSuccess } = useToast(); | ||
|
||
const [finality, setFinality] = useState<FinalityType>(FinalityType.FINAL); | ||
const [payee, setPayee] = useState<string>(''); | ||
|
||
const [working, setWorking] = useState(false); | ||
|
||
const onPool = async () => { | ||
if (!coretimeApi || !activeAccount || !activeSigner) return; | ||
|
||
const txPooling = coretimeApi.tx.broker.pool( | ||
regionMetadata.region.getOnChainRegionId(), // region id | ||
activeAccount.address, // payee | ||
finality | ||
); | ||
|
||
try { | ||
setWorking(true); | ||
await txPooling.signAndSend( | ||
activeAccount.address, | ||
{ signer: activeSigner }, | ||
({ status, events }) => { | ||
if (status.isReady) toastInfo('Transaction was initiated'); | ||
else if (status.isInBlock) toastInfo(`In Block`); | ||
else if (status.isFinalized) { | ||
setWorking(false); | ||
events.forEach(({ event: { method } }) => { | ||
if (method === 'ExtrinsicSuccess') { | ||
toastSuccess( | ||
'Successfully contributed to the instantaneous region pool' | ||
); | ||
onClose(); | ||
fetchRegions(); | ||
} else if (method === 'ExtrinsicFailed') { | ||
toastError( | ||
`Failed to contribute to the instantaneous region pool` | ||
); | ||
} | ||
}); | ||
} | ||
} | ||
); | ||
} catch (e) { | ||
toastError(`Failed to contribute to the instantaneous region pool ${e}`); | ||
setWorking(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (!open) return; | ||
setFinality(FinalityType.FINAL); | ||
setPayee(''); | ||
setWorking(false); | ||
}, [open]); | ||
|
||
return ( | ||
<Dialog open={open} onClose={onClose} maxWidth='md'> | ||
<DialogContent className={styles.container}> | ||
<Box> | ||
<Typography | ||
variant='subtitle1' | ||
sx={{ color: theme.palette.common.black }} | ||
> | ||
Task Pooling | ||
</Typography> | ||
<Typography | ||
variant='subtitle2' | ||
sx={{ color: theme.palette.text.primary }} | ||
> | ||
You can contribute your region to the pool of instantaneous regions | ||
</Typography> | ||
</Box> | ||
<Box className={styles.content}> | ||
<SimpleRegionCard regionMetadata={regionMetadata} /> | ||
</Box> | ||
<Paper className={styles.options}> | ||
<Typography | ||
variant='subtitle1' | ||
sx={{ color: theme.palette.common.black }} | ||
> | ||
Contribution options | ||
</Typography> | ||
<Box className={styles.optionItem}> | ||
<Typography className={styles.optionKey}>Finality:</Typography> | ||
<FinalitySelector {...{ finality, setFinality }} /> | ||
</Box> | ||
<Box className={styles.optionItem}> | ||
<Typography className={styles.optionKey}>Payee:</Typography> | ||
<Input | ||
value={payee} | ||
onChange={(e) => setPayee(e.target.value)} | ||
fullWidth | ||
type='text' | ||
placeholder='Address of the payee' | ||
endAdornment={ | ||
<InputAdornment position='end'> | ||
<Button | ||
variant='text' | ||
color='info' | ||
onClick={() => | ||
activeAccount && setPayee(activeAccount.address) | ||
} | ||
> | ||
Me | ||
</Button> | ||
</InputAdornment> | ||
} | ||
sx={{ height: '3rem' }} | ||
error={payee.length > 0 && !isValidAddress(payee)} | ||
/> | ||
</Box> | ||
</Paper> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={onClose} variant='outlined'> | ||
Cancel | ||
</Button> | ||
|
||
<ProgressButton onClick={onPool} label='Pool' loading={working} /> | ||
</DialogActions> | ||
</Dialog> | ||
); | ||
}; |
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 |
---|---|---|
|
@@ -18,3 +18,9 @@ | |
padding: 1.25rem; | ||
gap: 1rem; | ||
} | ||
|
||
.alert { | ||
max-width: 32rem; | ||
align-items: center; | ||
margin: 0 auto; | ||
} |
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
Oops, something went wrong.