-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Finalize wallet setup for funded zero balance account
- Loading branch information
1 parent
759ba61
commit 98db72c
Showing
7 changed files
with
241 additions
and
6 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
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
57 changes: 57 additions & 0 deletions
57
packages/frontend/src/components/profile/zero_balance/AddLedgerKeyModal.js
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,57 @@ | ||
import React from 'react'; | ||
import { Translate } from 'react-localize-redux'; | ||
import styled from 'styled-components'; | ||
|
||
import FormButton from '../../common/FormButton'; | ||
import Modal from '../../common/modal/Modal'; | ||
|
||
const Container = styled.div` | ||
&&&&& { | ||
padding: 15px 0 10px 0; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
text-align: center; | ||
h3 { | ||
margin: 15px 0; | ||
} | ||
> button { | ||
margin-top: 25px; | ||
width: 100%; | ||
} | ||
} | ||
`; | ||
|
||
export function AddLedgerKeyModal({ | ||
isOpen, | ||
onClickAddLedgerKey, | ||
onClose, | ||
finishingLedgerKeySetup | ||
}) { | ||
return ( | ||
<Modal | ||
id='add-ledger-key-modal' | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
modalSize='sm' | ||
> | ||
<Container> | ||
<h3><Translate id='zeroBalance.ledgerModal.title' /></h3> | ||
{ | ||
finishingLedgerKeySetup | ||
? <p><Translate id='zeroBalance.ledgerModal.confirmOnLedger' /></p> | ||
: <p><Translate id='zeroBalance.ledgerModal.desc' /></p> | ||
} | ||
<FormButton | ||
onClick={onClickAddLedgerKey} | ||
sending={finishingLedgerKeySetup} | ||
disabled={finishingLedgerKeySetup} | ||
> | ||
<Translate id='zeroBalance.ledgerModal.addLedgerKey' /> | ||
</FormButton> | ||
</Container> | ||
</Modal> | ||
); | ||
}; |
109 changes: 109 additions & 0 deletions
109
packages/frontend/src/components/profile/zero_balance/ZeroBalanceAccountWrapper.js
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,109 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { useSelector, useDispatch } from 'react-redux'; | ||
|
||
import { refreshAccount } from '../../../redux/actions/account'; | ||
import { showCustomAlert } from '../../../redux/actions/status'; | ||
import { selectAccountExists, selectAccountFullAccessKeys, selectAccountId } from '../../../redux/slices/account'; | ||
import { finishLocalSetupForZeroBalanceAccount } from '../../../redux/slices/account/createAccountThunks'; | ||
import { actions as ledgerActions, selectLedgerConnectionAvailable } from '../../../redux/slices/ledger'; | ||
import { wallet } from '../../../utils/wallet'; | ||
import { AddLedgerKeyModal } from './AddLedgerKeyModal'; | ||
|
||
|
||
const { handleShowConnectModal } = ledgerActions; | ||
|
||
export function ZeroBalanceAccountWrapper() { | ||
const dispatch = useDispatch(); | ||
|
||
const [showAddLedgerKeyModal, setShowAddLedgerKeyModal] = useState(false); | ||
const [finishingLedgerKeySetup, setFinishingLedgerKeySetup] = useState(false); | ||
|
||
const ledgerConnectionAvailable = useSelector(selectLedgerConnectionAvailable); | ||
const accountId = useSelector(selectAccountId); | ||
const accountExists = useSelector(selectAccountExists); | ||
const accountFullAccessKeys = useSelector(selectAccountFullAccessKeys); | ||
|
||
const isLedgerKey = accountFullAccessKeys[0]?.meta.type === 'ledger'; | ||
|
||
useEffect(() => { | ||
if (accountExists && accountFullAccessKeys.length === 1) { | ||
if (isLedgerKey) { | ||
handleCheckLedgerStatus(); | ||
} else { | ||
handleAddLocalAccessKeyPhrase(); | ||
} | ||
} | ||
}, [accountExists]); | ||
|
||
const handleCheckLedgerStatus = async () => { | ||
const localKey = await wallet.getLocalSecretKey(accountId); | ||
if (!localKey) { | ||
setShowAddLedgerKeyModal(true); | ||
} | ||
}; | ||
|
||
const handleAddLocalAccessKeyLedger = async () => { | ||
try { | ||
await dispatch(finishLocalSetupForZeroBalanceAccount({ | ||
implicitAccountId: accountId, | ||
recoveryMethod: 'ledger', | ||
})); | ||
dispatch(showCustomAlert({ | ||
success: true, | ||
messageCodeHeader: 'zeroBalance.addLedgerKey.success.header', | ||
messageCode: 'zeroBalance.addLedgerKey.success.message' | ||
})); | ||
} catch (e) { | ||
dispatch(showCustomAlert({ | ||
success: false, | ||
messageCodeHeader: 'zeroBalance.addLedgerKey.error.header', | ||
messageCode: 'zeroBalance.addLedgerKey.error.message', | ||
})); | ||
} | ||
|
||
dispatch(refreshAccount()); | ||
}; | ||
|
||
const handleAddLocalAccessKeyPhrase = async () => { | ||
try { | ||
await dispatch(finishLocalSetupForZeroBalanceAccount({ | ||
implicitAccountId: accountId, | ||
recoveryMethod: 'phrase', | ||
})); | ||
dispatch(showCustomAlert({ | ||
success: true, | ||
messageCodeHeader: 'zeroBalance.addPhraseKey.success.header', | ||
messageCode: 'zeroBalance.addPhraseKey.success.message' | ||
})); | ||
} catch (e) { | ||
dispatch(showCustomAlert({ | ||
success: false, | ||
messageCodeHeader: 'zeroBalance.addPhraseKey.error.header', | ||
messageCode: 'zeroBalance.addPhraseKey.error.message', | ||
})); | ||
} | ||
|
||
dispatch(refreshAccount()); | ||
}; | ||
|
||
if (showAddLedgerKeyModal) { | ||
return ( | ||
<AddLedgerKeyModal | ||
isOpen={showAddLedgerKeyModal} | ||
onClose={() => setShowAddLedgerKeyModal(false)} | ||
finishingLedgerKeySetup={finishingLedgerKeySetup} | ||
onClickAddLedgerKey={async () => { | ||
if (!ledgerConnectionAvailable) { | ||
dispatch(handleShowConnectModal()); | ||
} else { | ||
setFinishingLedgerKeySetup(true); | ||
await handleAddLocalAccessKeyLedger(); | ||
setShowAddLedgerKeyModal(false); | ||
setFinishingLedgerKeySetup(false); | ||
} | ||
}} | ||
/> | ||
); | ||
} | ||
return null; | ||
}; |
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
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
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