Skip to content

Commit

Permalink
signup modal changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ilya Smiyukha committed Aug 13, 2023
1 parent faced41 commit ae6057b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ export const EmailSubscriptionModal = () => {
const [, setMembersEmail] = useLocalStorage<Record<string, string>>('membersEmail')
const { wallet } = useMyAccounts()
const [state, send] = useMachine(EmailSubscriptionMachine)
const {
data,
error,
send: mutate,
} = useBackend({
mutation: SIGNUP_MUTATION,
})
const signupMutation = useBackend({ mutation: SIGNUP_MUTATION })

const signEmail = async () => {
const timestamp = Date.now()
Expand All @@ -54,12 +48,12 @@ export const EmailSubscriptionModal = () => {

const signUp = async () => {
if (state.event.type === 'SIGNED') {
await mutate({
await signupMutation.send({
memberId: parseFloat(member.id),
name: member.name,
email: state.context.email,
signature: state.event.signature,
timestamp: state.event.timestamp,
signature: state.context.signature,
timestamp: state.context.timestamp,
})
}
}
Expand All @@ -68,23 +62,23 @@ export const EmailSubscriptionModal = () => {
if (state.matches('signature')) {
signEmail()
}
if (state.matches('transaction')) {
if (state.matches('signup')) {
signUp()
}
}, [state])

useEffect(() => {
if (data) {
if (signupMutation.data) {
send('SUCCESS')
setMembersEmail((emails) => ({ ...emails, [member.id]: state.context.email || '' }))
hideModal()
}
if (error) {
if (signupMutation.error) {
send('ERROR')
}
}, [data, error])
}, [signupMutation])

if (state.matches('prepare')) {
if (state.matches('form')) {
return (
<EmailSubscriptionFormModal
onClose={hideModal}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { EmailSubscriptionForm } from './types'
interface Context extends EmailSubscriptionForm {
email: string
timestamp: number
signature: string
}

type EmailSubscriptionState =
| { value: 'prepare'; context: EmptyObject }
| { value: 'form'; context: EmptyObject }
| { value: 'signature'; context: Pick<Context, 'email'> }
| { value: 'transaction'; context: Context }
| { value: 'signup'; context: Context }
| { value: 'success'; context: Context }
| { value: 'error'; context: Context }

Expand All @@ -26,9 +27,9 @@ export type EmailSubscriptionEvent =

export const EmailSubscriptionMachine = createMachine<Partial<Context>, EmailSubscriptionEvent, EmailSubscriptionState>(
{
initial: 'prepare',
initial: 'form',
states: {
prepare: {
form: {
on: {
DONE: {
target: 'signature',
Expand All @@ -41,19 +42,18 @@ export const EmailSubscriptionMachine = createMachine<Partial<Context>, EmailSub
signature: {
on: {
SIGNED: {
target: 'transaction',
actions: (_, event) =>
assign({
signature: event.signature,
timestamp: event.timestamp,
}),
target: 'signup',
actions: assign({
signature: (_, event) => event.signature,
timestamp: (_, event) => event.timestamp,
}),
},
CANCEL: {
target: 'canceled',
},
},
},
transaction: {
signup: {
on: {
SUCCESS: {
target: 'success',
Expand All @@ -69,7 +69,7 @@ export const EmailSubscriptionMachine = createMachine<Partial<Context>, EmailSub
},
cancel: {
target: 'canceled',
action: 'prepare',
action: 'form',
},
}),
},
Expand Down

0 comments on commit ae6057b

Please sign in to comment.