You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.6 KiB
94 lines
2.6 KiB
3 years ago
|
import React, { useState } from 'react';
|
||
|
import { useHistory } from 'react-router-dom';
|
||
3 years ago
|
import { useDispatch } from 'react-redux';
|
||
3 years ago
|
import { util } from '@metamask/controllers';
|
||
3 years ago
|
import { useI18nContext } from '../../hooks/useI18nContext';
|
||
|
import { DEFAULT_ROUTE } from '../../helpers/constants/routes';
|
||
|
|
||
|
import Box from '../../components/ui/box';
|
||
|
import TextField from '../../components/ui/text-field';
|
||
|
import PageContainer from '../../components/ui/page-container';
|
||
3 years ago
|
import {
|
||
|
addCollectibleVerifyOwnership,
|
||
|
setNewCollectibleAddedMessage,
|
||
|
} from '../../store/actions';
|
||
3 years ago
|
|
||
|
export default function AddCollectible() {
|
||
|
const t = useI18nContext();
|
||
|
const history = useHistory();
|
||
3 years ago
|
const dispatch = useDispatch();
|
||
3 years ago
|
|
||
|
const [address, setAddress] = useState('');
|
||
|
const [tokenId, setTokenId] = useState('');
|
||
3 years ago
|
const [disabled, setDisabled] = useState(true);
|
||
3 years ago
|
|
||
3 years ago
|
const handleAddCollectible = async () => {
|
||
|
try {
|
||
|
await dispatch(addCollectibleVerifyOwnership(address, tokenId));
|
||
|
} catch (error) {
|
||
|
const { message } = error;
|
||
|
dispatch(setNewCollectibleAddedMessage(message));
|
||
|
history.push(DEFAULT_ROUTE);
|
||
|
return;
|
||
|
}
|
||
|
dispatch(setNewCollectibleAddedMessage('success'));
|
||
|
history.push(DEFAULT_ROUTE);
|
||
|
};
|
||
|
|
||
3 years ago
|
const validateAndSetAddress = (val) => {
|
||
|
setDisabled(!util.isValidHexAddress(val) || !tokenId);
|
||
|
setAddress(val);
|
||
|
};
|
||
|
|
||
|
const validateAndSetTokenId = (val) => {
|
||
|
setDisabled(!util.isValidHexAddress(address) || !val);
|
||
|
setTokenId(val);
|
||
|
};
|
||
|
|
||
3 years ago
|
return (
|
||
|
<PageContainer
|
||
|
title={t('addNFT')}
|
||
|
onSubmit={() => {
|
||
3 years ago
|
handleAddCollectible();
|
||
3 years ago
|
}}
|
||
|
submitText={t('add')}
|
||
|
onCancel={() => {
|
||
|
history.push(DEFAULT_ROUTE);
|
||
|
}}
|
||
|
onClose={() => {
|
||
|
history.push(DEFAULT_ROUTE);
|
||
|
}}
|
||
3 years ago
|
disabled={disabled}
|
||
3 years ago
|
contentComponent={
|
||
|
<Box padding={4}>
|
||
|
<Box>
|
||
|
<TextField
|
||
|
id="address"
|
||
|
label={t('address')}
|
||
|
placeholder="0x..."
|
||
|
type="text"
|
||
|
value={address}
|
||
3 years ago
|
onChange={(e) => validateAndSetAddress(e.target.value)}
|
||
3 years ago
|
fullWidth
|
||
|
autoFocus
|
||
|
margin="normal"
|
||
|
/>
|
||
|
</Box>
|
||
|
<Box>
|
||
|
<TextField
|
||
|
id="token-id"
|
||
|
label={t('id')}
|
||
|
placeholder={t('nftTokenIdPlaceholder')}
|
||
|
type="number"
|
||
|
value={tokenId}
|
||
3 years ago
|
onChange={(e) => validateAndSetTokenId(e.target.value)}
|
||
3 years ago
|
fullWidth
|
||
|
margin="normal"
|
||
|
/>
|
||
|
</Box>
|
||
|
</Box>
|
||
|
}
|
||
|
/>
|
||
|
);
|
||
|
}
|