Skip to content

Commit 82e9d74

Browse files
authored
Merge pull request #314 from crypto-com/feature/wallet-deletion
Problem (Fix #267): Missing delete wallet button
2 parents 80c1d2e + 44001e6 commit 82e9d74

File tree

4 files changed

+151
-1
lines changed

4 files changed

+151
-1
lines changed

src/components/ModalPopup/ModalPopup.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
text-align: left !important;
2424
.ant-btn {
2525
width: 40%;
26+
margin: 10px !important;
2627
}
2728
}

src/components/ModalPopup/ModalPopup.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface ModalPopupProps {
1616
footer?: Array<React.ReactNode>;
1717
confirmationLoading?: boolean;
1818
okText?: string;
19+
closable?: boolean;
1920
}
2021

2122
const ModalPopup: React.FC<ModalPopupProps> = props => {
@@ -30,6 +31,7 @@ const ModalPopup: React.FC<ModalPopupProps> = props => {
3031
footer={props.footer}
3132
confirmLoading={props.confirmationLoading}
3233
okText={props.okText}
34+
closable={props.closable}
3335
>
3436
{props.children}
3537
</Modal>

src/layouts/home/home.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@
134134
color: @azure;
135135
}
136136
}
137+
.delete-wallet-item {
138+
color: #f27474 !important;
139+
}
137140
}
138141
.ant-menu-submenu-popup ul {
139142
margin-top: -5px !important;

src/layouts/home/home.tsx

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useRef, useState } from 'react';
22
import './home.less';
33
import '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';
55
import { Link, useHistory, useLocation } from 'react-router-dom';
66
import Icon, {
77
CaretDownOutlined,
@@ -10,6 +10,7 @@ import Icon, {
1010
PlusOutlined,
1111
CheckOutlined,
1212
SettingOutlined,
13+
DeleteOutlined,
1314
} from '@ant-design/icons';
1415
import { useRecoilState } from 'recoil';
1516

@@ -21,6 +22,7 @@ import IconSend from '../../svg/IconSend';
2122
import IconReceive from '../../svg/IconReceive';
2223
import IconStaking from '../../svg/IconStaking';
2324
import IconWallet from '../../svg/IconWallet';
25+
import ModalPopup from '../../components/ModalPopup/ModalPopup';
2426
import { walletService } from '../../service/WalletService';
2527
import { Session } from '../../models/Session';
2628

@@ -33,13 +35,51 @@ const { SubMenu } = Menu;
3335

3436
function 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: /DELETE/,
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

Comments
 (0)