-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
183 additions
and
126 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
80 changes: 80 additions & 0 deletions
80
explorer/src/components/Delegations/components/DelegationModal.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,80 @@ | ||
import React from 'react'; | ||
import { Typography, SxProps, Stack } from '@mui/material'; | ||
import { Link } from '@nymproject/react/link/Link'; | ||
import { LoadingModal } from './LoadingModal'; | ||
import { ConfirmationModal } from './ConfirmationModal'; | ||
import { ErrorModal } from './ErrorModal'; | ||
|
||
export type ActionType = 'delegate' | 'undelegate' | 'redeem' | 'redeem-all' | 'compound'; | ||
|
||
const actionToHeader = (action: ActionType): string => { | ||
// eslint-disable-next-line default-case | ||
switch (action) { | ||
case 'redeem': | ||
return 'Rewards redeemed successfully'; | ||
case 'redeem-all': | ||
return 'All rewards redeemed successfully'; | ||
case 'delegate': | ||
return 'Delegation successful'; | ||
case 'undelegate': | ||
return 'Undelegation successful'; | ||
case 'compound': | ||
return 'Rewards compounded successfully'; | ||
default: | ||
throw new Error('Unknown type'); | ||
} | ||
}; | ||
|
||
export type DelegationModalProps = { | ||
status: 'loading' | 'success' | 'error'; | ||
action: ActionType; | ||
message?: string; | ||
transactions?: { | ||
url: string; | ||
hash: string; | ||
}[]; | ||
}; | ||
|
||
export const DelegationModal: FCWithChildren< | ||
DelegationModalProps & { | ||
open: boolean; | ||
onClose: () => void; | ||
sx?: SxProps; | ||
backdropProps?: object; | ||
children?: React.ReactNode; | ||
} | ||
> = ({ status, action, message, transactions, open, onClose, children, sx, backdropProps }) => { | ||
if (status === 'loading') return <LoadingModal sx={sx} backdropProps={backdropProps} />; | ||
|
||
if (status === 'error') { | ||
return ( | ||
<ErrorModal message={message} sx={sx} open={open} onClose={onClose}> | ||
{children} | ||
</ErrorModal> | ||
); | ||
} | ||
|
||
return ( | ||
<ConfirmationModal | ||
open={open} | ||
onConfirm={onClose || (() => {})} | ||
title={actionToHeader(action)} | ||
confirmButton="Done" | ||
> | ||
<Stack alignItems="center" spacing={2} mb={0}> | ||
{message && <Typography>{message}</Typography>} | ||
{transactions?.length === 1 && ( | ||
<Link href={transactions[0].url} target="_blank" sx={{ ml: 1 }} text="View on blockchain" noIcon /> | ||
)} | ||
{transactions && transactions.length > 1 && ( | ||
<Stack alignItems="center" spacing={1}> | ||
<Typography>View the transactions on blockchain:</Typography> | ||
{transactions.map(({ url, hash }) => ( | ||
<Link href={url} target="_blank" sx={{ ml: 1 }} text={hash.slice(0, 6)} key={hash} noIcon /> | ||
))} | ||
</Stack> | ||
)} | ||
</Stack> | ||
</ConfirmationModal> | ||
); | ||
}; |
29 changes: 29 additions & 0 deletions
29
explorer/src/components/Delegations/components/LoadingModal.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,29 @@ | ||
import React from 'react'; | ||
import { Box, CircularProgress, Modal, Stack, Typography, SxProps } from '@mui/material'; | ||
|
||
const modalStyle: SxProps = { | ||
position: 'absolute', | ||
top: '50%', | ||
left: '50%', | ||
transform: 'translate(-50%, -50%)', | ||
width: 500, | ||
bgcolor: 'background.paper', | ||
boxShadow: 24, | ||
borderRadius: '16px', | ||
p: 4, | ||
}; | ||
|
||
export const LoadingModal: FCWithChildren<{ | ||
text?: string; | ||
sx?: SxProps; | ||
backdropProps?: object; | ||
}> = ({ sx, backdropProps, text = 'Please wait...' }) => ( | ||
<Modal open BackdropProps={backdropProps}> | ||
<Box sx={{ border: (t) => `1px solid grey`, ...modalStyle, ...sx }} textAlign="center"> | ||
<Stack spacing={4} direction="row" alignItems="center"> | ||
<CircularProgress /> | ||
<Typography sx={{ color: 'text.primary' }}>{text}</Typography> | ||
</Stack> | ||
</Box> | ||
</Modal> | ||
); |
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.