11import React , { useEffect , useRef , useState } from 'react' ;
22import './home.less' ;
33import 'antd/dist/antd.css' ;
4- import { Dropdown , Layout , Menu , Spin } from 'antd' ;
4+ import { Dropdown , Layout , Menu , Spin , Button , Alert , Checkbox , Form , Input } from 'antd' ;
55import { Link , useHistory , useLocation } from 'react-router-dom' ;
66import Icon , {
77 CaretDownOutlined ,
@@ -10,6 +10,7 @@ import Icon, {
1010 PlusOutlined ,
1111 CheckOutlined ,
1212 SettingOutlined ,
13+ DeleteOutlined ,
1314} from '@ant-design/icons' ;
1415import { useRecoilState } from 'recoil' ;
1516
@@ -21,6 +22,7 @@ import IconSend from '../../svg/IconSend';
2122import IconReceive from '../../svg/IconReceive' ;
2223import IconStaking from '../../svg/IconStaking' ;
2324import IconWallet from '../../svg/IconWallet' ;
25+ import ModalPopup from '../../components/ModalPopup/ModalPopup' ;
2426import { walletService } from '../../service/WalletService' ;
2527import { Session } from '../../models/Session' ;
2628
@@ -33,13 +35,51 @@ const { SubMenu } = Menu;
3335
3436function HomeLayout ( props : HomeLayoutProps ) {
3537 const history = useHistory ( ) ;
38+ const [ confirmDeleteForm ] = Form . useForm ( ) ;
3639 const [ hasWallet , setHasWallet ] = useState ( true ) ; // Default as true. useEffect will only re-render if result of hasWalletBeenCreated === false
3740 const [ session , setSession ] = useRecoilState ( sessionState ) ;
3841 const [ userAsset , setUserAsset ] = useRecoilState ( walletAssetState ) ;
3942 const [ walletList , setWalletList ] = useRecoilState ( walletListState ) ;
4043 const [ loading , setLoading ] = useState ( false ) ;
44+ const [ isButtonDisabled , setIsButtonDisabled ] = useState ( true ) ;
45+ const [ isConfirmDeleteVisible , setIsConfirmDeleteVisible ] = useState ( false ) ;
46+ const [ isConfirmationModalVisible , setIsConfirmationModalVisible ] = useState ( false ) ;
47+ const [ isButtonLoading , setIsButtonLoading ] = useState ( false ) ;
4148 const didMountRef = useRef ( false ) ;
4249
50+ const onWalletDeleteFinish = async ( ) => {
51+ setIsButtonLoading ( true ) ;
52+ setLoading ( true ) ;
53+
54+ if ( ! session ) {
55+ return ;
56+ }
57+ await walletService . deleteWallet ( session . wallet . identifier ) ;
58+
59+ // Switch to existing default wallet
60+ const allWalletsData = await walletService . retrieveAllWallets ( ) ;
61+ setWalletList ( allWalletsData ) ;
62+ await walletService . setCurrentSession ( new Session ( walletList [ 0 ] ) ) ;
63+ const currentSession = await walletService . retrieveCurrentSession ( ) ;
64+ const currentAsset = await walletService . retrieveDefaultWalletAsset ( currentSession ) ;
65+ setSession ( currentSession ) ;
66+ setUserAsset ( currentAsset ) ;
67+ await walletService . syncAll ( currentSession ) ;
68+
69+ setIsButtonLoading ( false ) ;
70+ setIsConfirmationModalVisible ( false ) ;
71+ setLoading ( false ) ;
72+ } ;
73+
74+ const handleCancel = ( ) => {
75+ setIsConfirmationModalVisible ( false ) ;
76+ setIsConfirmDeleteVisible ( false ) ;
77+ } ;
78+
79+ const showPasswordModal = ( ) => {
80+ setIsConfirmationModalVisible ( false ) ;
81+ } ;
82+
4383 useEffect ( ( ) => {
4484 const fetchDB = async ( ) => {
4585 setLoading ( true ) ;
@@ -172,6 +212,19 @@ function HomeLayout(props: HomeLayoutProps) {
172212 ) : (
173213 ''
174214 ) }
215+ { walletList . length > 1 ? (
216+ < >
217+ < Menu . Item
218+ className = "delete-wallet-item"
219+ onClick = { ( ) => setIsConfirmationModalVisible ( true ) }
220+ >
221+ < DeleteOutlined />
222+ Delete Wallet
223+ </ Menu . Item >
224+ </ >
225+ ) : (
226+ ''
227+ ) }
175228 </ Menu >
176229 ) ;
177230 } ;
@@ -202,6 +255,97 @@ function HomeLayout(props: HomeLayoutProps) {
202255 ) : (
203256 props . children
204257 ) }
258+ < ModalPopup
259+ isModalVisible = { isConfirmationModalVisible }
260+ handleCancel = { handleCancel }
261+ handleOk = { showPasswordModal }
262+ confirmationLoading = { isButtonLoading }
263+ closable = { ! isButtonLoading }
264+ okText = "Confirm"
265+ footer = { [
266+ < Button
267+ key = "submit"
268+ type = "primary"
269+ loading = { isButtonLoading }
270+ onClick = { ( ) => setIsConfirmDeleteVisible ( true ) }
271+ disabled = { isButtonDisabled }
272+ hidden = { isConfirmDeleteVisible }
273+ danger
274+ >
275+ Confirm
276+ </ Button > ,
277+ < Button
278+ key = "submit"
279+ type = "primary"
280+ loading = { isButtonLoading }
281+ onClick = { confirmDeleteForm . submit }
282+ disabled = { isButtonDisabled }
283+ hidden = { ! isConfirmDeleteVisible }
284+ danger
285+ >
286+ Delete Wallet
287+ </ Button > ,
288+ < Button key = "back" type = "link" onClick = { handleCancel } disabled = { isButtonLoading } >
289+ Cancel
290+ </ Button > ,
291+ ] }
292+ >
293+ < >
294+ < div className = "title" > Confirm Wallet Deletion</ div >
295+ < div className = "description" > Please review the below information. </ div >
296+ < div className = "item" >
297+ < div className = "label" > Delete Wallet Address</ div >
298+ < div className = "address" > { `${ session . wallet . address } ` } </ div >
299+ </ div >
300+ { ! isConfirmDeleteVisible ? (
301+ < >
302+ < div className = "item" >
303+ < Alert
304+ type = "warning"
305+ message = "Are you sure you want to delete the wallet? If you have not backed up your wallet mnemonic phrase, this will result in losing your funds forever."
306+ showIcon
307+ />
308+ </ div >
309+ < div className = "item" >
310+ < Checkbox
311+ checked = { ! isButtonDisabled }
312+ onChange = { ( ) => setIsButtonDisabled ( ! isButtonDisabled ) }
313+ >
314+ I understand that the only way to regain access is by restoring wallet mnemonic
315+ phrase.
316+ </ Checkbox >
317+ </ div >
318+ </ >
319+ ) : (
320+ < div className = "item" >
321+ < Form
322+ layout = "vertical"
323+ form = { confirmDeleteForm }
324+ name = "control-hooks"
325+ requiredMark = "optional"
326+ onFinish = { onWalletDeleteFinish }
327+ >
328+ < Form . Item
329+ name = "delete"
330+ label = "Please enter DELETE"
331+ hasFeedback
332+ rules = { [
333+ {
334+ required : true ,
335+ } ,
336+ {
337+ pattern : / D E L E T E / ,
338+ message : 'Please enter DELETE' ,
339+ } ,
340+ ] }
341+ >
342+ < Input />
343+ </ Form . Item >
344+ </ Form >
345+ </ div >
346+ ) }
347+ </ >
348+ </ ModalPopup >
205349 </ Layout >
206350 </ main >
207351 ) ;
0 commit comments