forked from FuelLabs/fuels-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): add Unlock dialog (FuelLabs#140)
feat(app): add UnlockDialog component
- Loading branch information
1 parent
0b62d72
commit 63badce
Showing
16 changed files
with
313 additions
and
28 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
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
27 changes: 27 additions & 0 deletions
27
packages/app/src/systems/Account/components/UnlockDialog/UnlockDialog.stories.tsx
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,27 @@ | ||
import type { Story } from '@storybook/react'; | ||
|
||
import { createMockAccount } from '../../__mocks__'; | ||
|
||
import { UnlockDialog } from './UnlockDialog'; | ||
|
||
export default { | ||
component: UnlockDialog, | ||
title: 'Account/Components/UnlockDialog', | ||
parameters: { | ||
layout: 'fullscreen', | ||
}, | ||
}; | ||
|
||
export const Usage: Story<never> = () => { | ||
return <UnlockDialog isOpen />; | ||
}; | ||
|
||
Usage.loaders = [ | ||
async () => { | ||
await createMockAccount('123123123'); | ||
return {}; | ||
}, | ||
]; | ||
Usage.parameters = { | ||
layout: 'centered', | ||
}; |
85 changes: 85 additions & 0 deletions
85
packages/app/src/systems/Account/components/UnlockDialog/UnlockDialog.tsx
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,85 @@ | ||
import { cssObj } from '@fuel-ui/css'; | ||
import { Alert, Button, Dialog, Flex, Icon, Stack } from '@fuel-ui/react'; | ||
|
||
import type { UnlockFormValues } from '../../hooks'; | ||
import { useAccount, useUnlockForm } from '../../hooks'; | ||
import { UnlockForm } from '../UnlockForm'; | ||
|
||
export type UnlockDialogProps = { | ||
isOpen?: boolean; | ||
onOpenChange?: (open: boolean) => void; | ||
}; | ||
|
||
export function UnlockDialog({ isOpen, onOpenChange }: UnlockDialogProps) { | ||
const form = useUnlockForm(); | ||
const { handlers, isLoading } = useAccount(); | ||
const { formState } = form; | ||
|
||
function onSubmit(values: UnlockFormValues) { | ||
handlers.unlock(values.password); | ||
} | ||
|
||
return ( | ||
<Dialog open={isOpen} onOpenChange={onOpenChange}> | ||
<form onSubmit={form.handleSubmit(onSubmit)}> | ||
<Dialog.Content css={styles.content}> | ||
<Dialog.Heading> | ||
<Flex css={{ alignItems: 'center' }}> | ||
<Icon | ||
color="gray8" | ||
icon={Icon.is('LockKeyOpen')} | ||
css={styles.headingIcon} | ||
/> | ||
Unlock Wallet | ||
</Flex> | ||
</Dialog.Heading> | ||
<Dialog.Description> | ||
<Stack gap="$3"> | ||
<Alert status="info" css={styles.alert}> | ||
You need to unlock your wallet to be able to make transactions | ||
and more-sensitive actions. | ||
</Alert> | ||
<UnlockForm form={form} /> | ||
</Stack> | ||
</Dialog.Description> | ||
<Dialog.Footer> | ||
<Dialog.Close> | ||
<Button | ||
type="submit" | ||
color="accent" | ||
isDisabled={!formState.isValid} | ||
isLoading={isLoading} | ||
leftIcon={Icon.is('LockKeyOpen')} | ||
css={styles.button} | ||
> | ||
Unlock | ||
</Button> | ||
</Dialog.Close> | ||
</Dialog.Footer> | ||
</Dialog.Content> | ||
</form> | ||
</Dialog> | ||
); | ||
} | ||
|
||
const styles = { | ||
headingIcon: cssObj({ | ||
marginRight: '$3', | ||
}), | ||
alert: cssObj({ | ||
py: '$2', | ||
pr: '$2', | ||
background: '$gray2', | ||
}), | ||
button: cssObj({ | ||
width: '100%', | ||
}), | ||
content: cssObj({ | ||
maxWidth: 312, | ||
|
||
/** This is temporary until have this option on @fuel-ui */ | ||
'button[aria-label="Close"]': { | ||
display: 'none', | ||
}, | ||
}), | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/app/src/systems/Account/components/UnlockDialog/index.tsx
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 @@ | ||
export * from './UnlockDialog'; |
29 changes: 29 additions & 0 deletions
29
packages/app/src/systems/Account/components/UnlockForm/UnlockForm.tsx
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,29 @@ | ||
import { InputPassword, Stack } from '@fuel-ui/react'; | ||
|
||
import type { UseUnlockFormReturn } from '../../hooks/useUnlockForm'; | ||
|
||
import { ControlledField } from '~/systems/Core'; | ||
|
||
type UnlockFormProps = { | ||
form: UseUnlockFormReturn; | ||
}; | ||
|
||
export function UnlockForm({ form }: UnlockFormProps) { | ||
const { control } = form; | ||
return ( | ||
<Stack gap="$4"> | ||
<ControlledField | ||
control={control} | ||
name="password" | ||
label="Password" | ||
render={({ field }) => ( | ||
<InputPassword | ||
{...field} | ||
placeholder="Type your password" | ||
aria-label="Your Password" | ||
/> | ||
)} | ||
/> | ||
</Stack> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
packages/app/src/systems/Account/components/UnlockForm/index.tsx
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 @@ | ||
export * from './UnlockForm'; |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './AccountItem'; | ||
export * from './AccountList'; | ||
export * from './BalanceWidget'; | ||
export * from './UnlockDialog'; | ||
export * from './UnlockForm'; |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useAccount'; | ||
export * from './useUnlockForm'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { yupResolver } from '@hookform/resolvers/yup'; | ||
import { useForm } from 'react-hook-form'; | ||
import { useNavigate, useLocation } from 'react-router-dom'; | ||
import * as yup from 'yup'; | ||
|
||
import { store, Services } from '~/store'; | ||
import { Pages } from '~/systems/Core'; | ||
|
||
const schema = yup | ||
.object({ | ||
password: yup.string().min(8).required('Password is required'), | ||
}) | ||
.required(); | ||
|
||
export type UseUnlockFormReturn = ReturnType<typeof useUnlockForm>; | ||
|
||
export type UnlockFormValues = { | ||
password: string; | ||
}; | ||
|
||
export function useUnlockForm() { | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
const form = useForm<UnlockFormValues>({ | ||
resolver: yupResolver(schema), | ||
reValidateMode: 'onChange', | ||
mode: 'onChange', | ||
defaultValues: { | ||
password: '', | ||
}, | ||
}); | ||
|
||
store.useSetMachineConfig(Services.account, { | ||
actions: { | ||
redirectToStatePath() { | ||
navigate(location.state?.lastPage ?? Pages.wallet()); | ||
}, | ||
}, | ||
}); | ||
|
||
return form; | ||
} |
Oops, something went wrong.