-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
signature for email modal #4474
Changes from 17 commits
91a3bd3
c25671c
8bed72d
23e8695
fc5116b
c16f0b5
4c22583
f5a32b1
ea7da3c
a5c8fb1
e52fd4b
ed18904
2e69753
1dd7a75
ab5399a
ff7f370
41f2b1b
e9f21d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ type Args = { | |
hasAccounts: boolean | ||
hasWallet: boolean | ||
isRPCNodeConnected: boolean | ||
hasRegisteredEmail: boolean | ||
onBuyMembership: CallableFunction | ||
onTransfer: CallableFunction | ||
} | ||
|
@@ -66,6 +67,7 @@ export default { | |
hasFunds: true, | ||
hasWallet: true, | ||
isRPCNodeConnected: true, | ||
hasRegisteredEmail: true, | ||
}, | ||
|
||
parameters: { | ||
|
@@ -76,7 +78,6 @@ export default { | |
balances: args.hasFunds ? parameters.totalBalance : 0, | ||
...(args.hasMemberships ? { member } : { account: { name: member.handle, address: member.controllerAccount } }), | ||
}) | ||
|
||
return { | ||
accounts: { | ||
active: args.isLoggedIn ? 'alice' : undefined, | ||
|
@@ -117,6 +118,10 @@ export default { | |
data: { membershipByUniqueInput: { ...bob, ...MEMBER_DATA, invitees: [] } }, | ||
}, | ||
], | ||
|
||
localStorage: { | ||
membersEmail: args.hasRegisteredEmail ? JSON.stringify({ 0: 'alice@example.com' }) : '', | ||
}, | ||
} | ||
}, | ||
}, | ||
|
@@ -408,3 +413,65 @@ export const BuyMembershipTxFailure: Story = { | |
expect(await modal.findByText('Some error message')) | ||
}, | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Test Emil Subsciption Modal | ||
// ---------------------------------------------------------------------------- | ||
export const EmailSubscriptionModalDecline: Story = { | ||
args: { | ||
isLoggedIn: true, | ||
hasMemberships: true, | ||
hasAccounts: true, | ||
hasFunds: true, | ||
hasWallet: true, | ||
isRPCNodeConnected: true, | ||
hasRegisteredEmail: false, | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const modal = withinModal(canvasElement) | ||
const element = await modal.getByText('Sign up to email notifications') | ||
expect(element) | ||
await userEvent.click(modal.getByText('Not now')) | ||
expect(element).not.toBeInTheDocument() | ||
}, | ||
} | ||
|
||
export const EmailSubscriptionModalWrongEmail: Story = { | ||
args: { | ||
isLoggedIn: true, | ||
hasMemberships: true, | ||
hasAccounts: true, | ||
hasFunds: true, | ||
hasWallet: true, | ||
isRPCNodeConnected: true, | ||
hasRegisteredEmail: false, | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const modal = withinModal(canvasElement) | ||
const button = modal.getByText(/^Sign and Authorize Email/i).closest('button') | ||
expect(button).toBeDisabled() | ||
await userEvent.type(modal.getByPlaceholderText('Add email for notifications here'), 'test@email') | ||
expect(button).toBeDisabled() | ||
}, | ||
} | ||
|
||
export const EmailSubscriptionModalSubscribe: Story = { | ||
args: { | ||
isLoggedIn: true, | ||
hasMemberships: true, | ||
hasAccounts: true, | ||
hasFunds: true, | ||
hasWallet: true, | ||
isRPCNodeConnected: true, | ||
hasRegisteredEmail: false, | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const modal = withinModal(canvasElement) | ||
const button = modal.getByText(/^Sign and Authorize Email/i) | ||
expect(button.closest('button')).toBeDisabled() | ||
await userEvent.type(modal.getByPlaceholderText('Add email for notifications here'), 'test@email.com') | ||
await waitFor(() => expect(button.closest('button')).toBeEnabled()) | ||
await userEvent.click(button) | ||
IlyaSmiyukha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(modal.getByText('Transaction was canceled.')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point the machine gets in the cancel state because |
||
}, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import React, { useEffect } from 'react' | ||
|
||
// import { useApi } from '@/api/hooks/useApi' | ||
import { useMyAccounts } from '@/accounts/hooks/useMyAccounts' | ||
import { FailureModal } from '@/common/components/FailureModal' | ||
import { ResultText } from '@/common/components/Modal' | ||
import { WaitModal } from '@/common/components/WaitModal' | ||
|
@@ -13,18 +13,32 @@ import { EmailSubscriptionMachine } from './machine' | |
import { EmailSubscriptionForm } from './types' | ||
|
||
export const EmailSubscriptionModal = () => { | ||
// const { api } = useApi() | ||
const { | ||
hideModal, | ||
modalData: { member }, | ||
} = useModal<EmailSubscriptionModalCall>() | ||
|
||
const { wallet } = useMyAccounts() | ||
const [state, send] = useMachine(EmailSubscriptionMachine) | ||
|
||
const signEmail = async () => { | ||
const timestamp = Date.now() | ||
try { | ||
const signature = await wallet?.signer.signPayload({ | ||
address: member.controllerAccount, | ||
data: `${member.id}:${timestamp}`, | ||
}) | ||
if (signature) { | ||
send('SIGNED', { signature: signature.signature, timestamp }) | ||
} | ||
} catch (error) { | ||
send('CANCEL') | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (state.matches('signature')) { | ||
// const timestamp = new Date() | ||
// api?.sign(member.controllerAccount, `${member.id}:${timestamp}`) | ||
signEmail() | ||
} | ||
}, [state]) | ||
|
||
|
@@ -40,11 +54,17 @@ export const EmailSubscriptionModal = () => { | |
) | ||
} | ||
|
||
if (state.matches('error')) { | ||
if (state.matches('error') || state.matches('canceled')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also if the "cancel" state just goes back to the "prepare" state it shouldn't show the failure modal. |
||
return ( | ||
<FailureModal onClose={hideModal}> | ||
There was a problem registering your email. | ||
<ResultText>We could not register your email at the moment! Please, try again later!</ResultText> | ||
{state.matches('canceled') ? ( | ||
<ResultText>Transaction was canceled.</ResultText> | ||
) : ( | ||
<> | ||
There was a problem registering your email. | ||
<ResultText>We could not register your email at the moment! Please, try again later!</ResultText> | ||
</> | ||
)} | ||
</FailureModal> | ||
) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ type EmailSubscriptionState = | |
| { value: 'transaction'; context: Context } | ||
| { value: 'success'; context: Context } | ||
| { value: 'error'; context: Context } | ||
| { value: 'canceled'; context: EmptyObject } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The state is "cancel" but I don't think it needs to be defined since it just goes back to the "prepare" state |
||
|
||
export type EmailSubscriptionEvent = | ||
| { type: 'DONE'; email: string } | ||
|
@@ -67,6 +68,10 @@ export const EmailSubscriptionMachine = createMachine<Partial<Context>, EmailSub | |
metaMessages: { | ||
error: 'There was a problem during the email subscription.', | ||
}, | ||
cancel: { | ||
target: 'canceled', | ||
action: 'BACK', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this to work the |
||
}, | ||
}), | ||
}, | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one 👍