2021-11-15 18:28:35 +01:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
2022-01-19 19:42:41 +01:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2021-12-14 00:41:10 +01:00
|
|
|
import { util } from '@metamask/controllers';
|
2021-11-15 18:28:35 +01:00
|
|
|
import { useI18nContext } from '../../hooks/useI18nContext';
|
|
|
|
import { DEFAULT_ROUTE } from '../../helpers/constants/routes';
|
|
|
|
|
2022-02-26 02:17:42 +01:00
|
|
|
import {
|
|
|
|
DISPLAY,
|
|
|
|
FONT_WEIGHT,
|
|
|
|
TYPOGRAPHY,
|
|
|
|
} from '../../helpers/constants/design-system';
|
|
|
|
|
2021-11-15 18:28:35 +01:00
|
|
|
import Box from '../../components/ui/box';
|
2022-02-26 02:17:42 +01:00
|
|
|
import Typography from '../../components/ui/typography';
|
|
|
|
import ActionableMessage from '../../components/ui/actionable-message';
|
2021-11-15 18:28:35 +01:00
|
|
|
import PageContainer from '../../components/ui/page-container';
|
2021-11-26 21:03:35 +01:00
|
|
|
import {
|
|
|
|
addCollectibleVerifyOwnership,
|
2022-01-19 19:42:41 +01:00
|
|
|
removeToken,
|
2021-11-26 21:03:35 +01:00
|
|
|
setNewCollectibleAddedMessage,
|
|
|
|
} from '../../store/actions';
|
2022-01-19 19:42:41 +01:00
|
|
|
import FormField from '../../components/ui/form-field';
|
|
|
|
import { getIsMainnet, getUseCollectibleDetection } from '../../selectors';
|
|
|
|
import { getCollectiblesDetectionNoticeDismissed } from '../../ducks/metamask/metamask';
|
|
|
|
import CollectiblesDetectionNotice from '../../components/app/collectibles-detection-notice';
|
2021-11-15 18:28:35 +01:00
|
|
|
|
|
|
|
export default function AddCollectible() {
|
|
|
|
const t = useI18nContext();
|
|
|
|
const history = useHistory();
|
2021-11-26 21:03:35 +01:00
|
|
|
const dispatch = useDispatch();
|
2022-01-19 19:42:41 +01:00
|
|
|
const useCollectibleDetection = useSelector(getUseCollectibleDetection);
|
|
|
|
const isMainnet = useSelector(getIsMainnet);
|
|
|
|
const collectibleDetectionNoticeDismissed = useSelector(
|
|
|
|
getCollectiblesDetectionNoticeDismissed,
|
|
|
|
);
|
2022-01-19 15:38:33 +01:00
|
|
|
const addressEnteredOnImportTokensPage =
|
|
|
|
history?.location?.state?.addressEnteredOnImportTokensPage;
|
2022-01-19 19:42:41 +01:00
|
|
|
const contractAddressToConvertFromTokenToCollectible =
|
|
|
|
history?.location?.state?.tokenAddress;
|
2022-01-19 15:38:33 +01:00
|
|
|
|
|
|
|
const [address, setAddress] = useState(
|
2022-01-19 19:42:41 +01:00
|
|
|
addressEnteredOnImportTokensPage ??
|
|
|
|
contractAddressToConvertFromTokenToCollectible ??
|
|
|
|
'',
|
2022-01-19 15:38:33 +01:00
|
|
|
);
|
2021-11-15 18:28:35 +01:00
|
|
|
const [tokenId, setTokenId] = useState('');
|
2021-12-14 00:41:10 +01:00
|
|
|
const [disabled, setDisabled] = useState(true);
|
2022-02-26 02:17:42 +01:00
|
|
|
const [collectibleAddFailed, setCollectibleAddFailed] = useState(false);
|
2021-11-15 18:28:35 +01:00
|
|
|
|
2021-11-26 21:03:35 +01:00
|
|
|
const handleAddCollectible = async () => {
|
|
|
|
try {
|
2022-01-19 19:42:41 +01:00
|
|
|
await dispatch(
|
|
|
|
addCollectibleVerifyOwnership(address, tokenId.toString()),
|
|
|
|
);
|
2021-11-26 21:03:35 +01:00
|
|
|
} catch (error) {
|
|
|
|
const { message } = error;
|
|
|
|
dispatch(setNewCollectibleAddedMessage(message));
|
2022-02-26 02:17:42 +01:00
|
|
|
setCollectibleAddFailed(true);
|
2021-11-26 21:03:35 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-01-19 19:42:41 +01:00
|
|
|
if (contractAddressToConvertFromTokenToCollectible) {
|
|
|
|
await dispatch(
|
|
|
|
removeToken(contractAddressToConvertFromTokenToCollectible),
|
|
|
|
);
|
|
|
|
}
|
2021-11-26 21:03:35 +01:00
|
|
|
dispatch(setNewCollectibleAddedMessage('success'));
|
|
|
|
history.push(DEFAULT_ROUTE);
|
|
|
|
};
|
|
|
|
|
2021-12-14 00:41:10 +01:00
|
|
|
const validateAndSetAddress = (val) => {
|
|
|
|
setDisabled(!util.isValidHexAddress(val) || !tokenId);
|
|
|
|
setAddress(val);
|
|
|
|
};
|
|
|
|
|
|
|
|
const validateAndSetTokenId = (val) => {
|
|
|
|
setDisabled(!util.isValidHexAddress(address) || !val);
|
|
|
|
setTokenId(val);
|
|
|
|
};
|
|
|
|
|
2021-11-15 18:28:35 +01:00
|
|
|
return (
|
|
|
|
<PageContainer
|
2022-01-19 15:38:33 +01:00
|
|
|
title={t('importNFT')}
|
2021-11-15 18:28:35 +01:00
|
|
|
onSubmit={() => {
|
2021-11-26 21:03:35 +01:00
|
|
|
handleAddCollectible();
|
2021-11-15 18:28:35 +01:00
|
|
|
}}
|
|
|
|
submitText={t('add')}
|
|
|
|
onCancel={() => {
|
|
|
|
history.push(DEFAULT_ROUTE);
|
|
|
|
}}
|
|
|
|
onClose={() => {
|
|
|
|
history.push(DEFAULT_ROUTE);
|
|
|
|
}}
|
2021-12-14 00:41:10 +01:00
|
|
|
disabled={disabled}
|
2021-11-15 18:28:35 +01:00
|
|
|
contentComponent={
|
2022-03-31 16:02:47 +02:00
|
|
|
<Box>
|
2022-01-19 19:42:41 +01:00
|
|
|
{isMainnet &&
|
|
|
|
!useCollectibleDetection &&
|
|
|
|
!collectibleDetectionNoticeDismissed ? (
|
|
|
|
<CollectiblesDetectionNotice />
|
|
|
|
) : null}
|
2022-02-26 02:17:42 +01:00
|
|
|
{collectibleAddFailed && (
|
|
|
|
<ActionableMessage
|
|
|
|
type="danger"
|
2022-03-07 17:42:22 +01:00
|
|
|
useIcon
|
2022-03-25 23:15:31 +01:00
|
|
|
iconFillColor="var(--color-error-default)"
|
2022-02-26 02:17:42 +01:00
|
|
|
message={
|
|
|
|
<Box display={DISPLAY.INLINE_FLEX}>
|
|
|
|
<Typography
|
|
|
|
variant={TYPOGRAPHY.H7}
|
|
|
|
fontWeight={FONT_WEIGHT.NORMAL}
|
|
|
|
margin={0}
|
|
|
|
>
|
|
|
|
{t('collectibleAddFailedMessage')}
|
|
|
|
</Typography>
|
|
|
|
<button
|
|
|
|
className="fas fa-times add-collectible__close"
|
|
|
|
title={t('close')}
|
|
|
|
onClick={() => setCollectibleAddFailed(false)}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
2022-03-31 16:02:47 +02:00
|
|
|
<Box margin={4}>
|
2022-01-19 19:42:41 +01:00
|
|
|
<FormField
|
2021-11-15 18:28:35 +01:00
|
|
|
id="address"
|
2022-01-19 19:42:41 +01:00
|
|
|
titleText={t('address')}
|
2021-11-15 18:28:35 +01:00
|
|
|
placeholder="0x..."
|
|
|
|
value={address}
|
2022-02-26 02:17:42 +01:00
|
|
|
onChange={(val) => {
|
|
|
|
validateAndSetAddress(val);
|
|
|
|
setCollectibleAddFailed(false);
|
|
|
|
}}
|
2022-01-19 19:42:41 +01:00
|
|
|
tooltipText={t('importNFTAddressToolTip')}
|
2021-11-15 18:28:35 +01:00
|
|
|
autoFocus
|
|
|
|
/>
|
2022-01-19 19:42:41 +01:00
|
|
|
<FormField
|
2021-11-15 18:28:35 +01:00
|
|
|
id="token-id"
|
2022-01-19 19:42:41 +01:00
|
|
|
titleText={t('tokenId')}
|
2021-11-15 18:28:35 +01:00
|
|
|
placeholder={t('nftTokenIdPlaceholder')}
|
|
|
|
value={tokenId}
|
2022-01-19 19:42:41 +01:00
|
|
|
onChange={(val) => {
|
|
|
|
validateAndSetTokenId(val);
|
2022-02-26 02:17:42 +01:00
|
|
|
setCollectibleAddFailed(false);
|
2022-01-19 19:42:41 +01:00
|
|
|
}}
|
|
|
|
tooltipText={t('importNFTTokenIdToolTip')}
|
|
|
|
numeric
|
2021-11-15 18:28:35 +01:00
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|