Skip to content

Commit

Permalink
refactor: add loading to ImportWallet and prevent double-tap
Browse files Browse the repository at this point in the history
  • Loading branch information
TravellerOnTheRun committed Feb 9, 2024
1 parent 3f8a5d2 commit 209534d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 76 deletions.
27 changes: 0 additions & 27 deletions src/redux/slices/settingsSlice/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@ export const selectRequests = ({ settings }: RootState) => settings.requests

export const selectTopColor = ({ settings }: RootState) => settings.topColor

export const selectWallet = ({ settings }: RootState) => {
if (!settings.wallets) {
throw new Error('No Wallets set!')
}

return settings.wallets[settings.selectedWallet]
}

export const selectWalletIsDeployed = ({ settings }: RootState) => {
if (!settings.walletsIsDeployed) {
throw new Error('WalletIsDeployed is not set!')
}
return settings.walletsIsDeployed[settings.selectedWallet]
}
export const selectSettingsIsLoading = ({ settings }: RootState) =>
settings.loading

Expand All @@ -41,16 +27,3 @@ export const selectHideBalance = ({ settings }: RootState) =>
export const selectBitcoin = ({ settings }: RootState) => settings.bitcoin

export const selectChainId = ({ settings }: RootState) => settings.chainId

export const selectWalletState = ({
settings: { wallets, walletsIsDeployed, chainId, selectedWallet },
}: RootState) => {
if (!wallets || !walletsIsDeployed) {
throw new Error('No Wallet exist in state')
}
return {
wallet: wallets[selectedWallet],
walletIsDeployed: walletsIsDeployed[selectedWallet],
chainId,
}
}
122 changes: 73 additions & 49 deletions src/screens/createKeys/import/ImportMasterKeyScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,38 @@ const headerTextMap = new Map([
[StatusActions.SUCCESS, 'header_phrase_correct'],
])

const StatusIcon = ({ status }: { status: StatusActions }) => {
const iconStyle = {
backgroundColor:
status === StatusActions.SUCCESS
? sharedColors.successLight
: sharedColors.errorBackground,
borderRadius: 50,
}
switch (status) {
case StatusActions.SUCCESS:
return (
<AntDesign
name="checkcircleo"
size={100}
style={iconStyle}
color={sharedColors.black}
/>
)
case StatusActions.ERROR:
return (
<Feather
name="x"
size={100}
style={iconStyle}
color={sharedColors.black}
/>
)
default:
return null
}
}

export const ImportMasterKeyScreen = (
_: CreateKeysScreenProps<createKeysRouteNames.ImportMasterKey>,
) => {
Expand All @@ -62,27 +94,39 @@ export const ImportMasterKeyScreen = (
const { setValue } = form
const [selectedSlide, setSelectedSlide] = useState<number>(0)
const [status, setStatus] = useState<StatusActions>(StatusActions.INITIAL)
const [isLoading, setIsLoading] = useState(false)

const handleImportMnemonic = useCallback(async () => {
// immediatly set loading to avoid double-tap
setIsLoading(true)

if (status === StatusActions.ERROR) {
setStatus(StatusActions.INITIAL)
return
}

const mnemonicError = validateMnemonic(words.current.join(' '))

if (mnemonicError) {
setStatus(StatusActions.ERROR)
// mnemonic failed set loading back to false
setIsLoading(false)
return
}

try {
await dispatch(
createWallet({
mnemonic: words.current.join(' '),
initializeWallet,
}),
)
form.reset()
setTimeout(async () => {
await dispatch(
createWallet({
mnemonic: words.current.join(' '),
initializeWallet,
}),
)
form.reset()
setIsLoading(false)
}, 500)
} catch (err) {
setIsLoading(false)
if (err instanceof Error) {
throw new Error(err.toString())
}
Expand All @@ -104,6 +148,13 @@ export const ImportMasterKeyScreen = (
[],
)

const errorTimeout = useCallback(() => {
setTimeout(() => {
setStatus(StatusActions.INITIAL)
}, 1000)
return null
}, [])

const renderItem = useCallback(
(item: CarouselRenderItemInfo<number>) => {
const groupIndex = 3 * item.index
Expand Down Expand Up @@ -178,6 +229,7 @@ export const ImportMasterKeyScreen = (

return (
<FormProvider {...form}>
{errorTimeout()}
<ScrollView style={styles.parent} keyboardShouldPersistTaps={'always'}>
<Typography
style={styles.titleText}
Expand Down Expand Up @@ -223,53 +275,25 @@ export const ImportMasterKeyScreen = (
/>
)}

<AppButton
accessibilityLabel={'OK'}
title="OK"
color="white"
textColor="black"
textType="body2"
textStyle={sharedStyles.fontBoldText}
onPress={handleImportMnemonic}
style={styles.appButtonStyleView}
/>
{status !== StatusActions.ERROR && (
<AppButton
accessibilityLabel={'OK'}
title={t('ok')}
color={sharedColors.white}
textColor={sharedColors.black}
textType={'body2'}
textStyle={sharedStyles.fontBoldText}
onPress={handleImportMnemonic}
style={styles.appButtonStyleView}
loading={isLoading}
disabled={isLoading}
/>
)}
</ScrollView>
</FormProvider>
)
}

const StatusIcon = ({ status }: { status: StatusActions }) => {
const iconStyle = {
backgroundColor:
status === StatusActions.SUCCESS
? sharedColors.successLight
: sharedColors.errorBackground,
borderRadius: 50,
}
switch (status) {
case StatusActions.SUCCESS:
return (
<AntDesign
name="checkcircleo"
size={100}
style={iconStyle}
color={sharedColors.black}
/>
)
case StatusActions.ERROR:
return (
<Feather
name="x"
size={100}
style={iconStyle}
color={sharedColors.black}
/>
)
default:
return null
}
}

const styles = StyleSheet.create({
parent: castStyle.view({
backgroundColor: sharedColors.black,
Expand Down

0 comments on commit 209534d

Please sign in to comment.