Skip to content

Commit

Permalink
waasRevalidation onVerify
Browse files Browse the repository at this point in the history
  • Loading branch information
SamueleA committed May 8, 2024
1 parent 4bf1f03 commit 7bfd3b5
Show file tree
Hide file tree
Showing 6 changed files with 9,287 additions and 6,628 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { useState, useEffect } from 'react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { Box, Modal, ThemeProvider } from '@0xsequence/design-system'
import { Box, Modal, Text, ThemeProvider } from '@0xsequence/design-system'
import { AnimatePresence } from 'framer-motion'

import { getModalPositionCss, useTheme } from '@0xsequence/kit'
import { getModalPositionCss, useTheme, WaasCodeInputContent } from '@0xsequence/kit'

import { PendingTransaction, TransactionError, TransactionSuccess, CheckoutSelection } from '../../views'
import { History, Navigation, NavigationContextProvider, CheckoutModalContextProvider, CheckoutSettings } from '../../contexts'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { PINCodeInput } from './PINCodeInput'

export interface WaasCodeInputContentProps {
isLoading: boolean
onVerify: () => {}
onVerify: (code: string) => void
}

export const WaasCodeInputContent = ({
Expand All @@ -19,7 +19,6 @@ export const WaasCodeInputContent = ({
}: WaasCodeInputContentProps) => {
const [waasEmailPinCode, setWaasEmailPinCode] = useState<string[]>([])


return (
<>
<Box paddingY="6" alignItems="center" justifyContent="center" flexDirection="column">
Expand All @@ -36,7 +35,7 @@ export const WaasCodeInputContent = ({
variant="primary"
disabled={waasEmailPinCode.includes('')}
label="Verify"
onClick={() => onVerify()}
onClick={() => onVerify(waasEmailPinCode.join(''))}
data-id="verifyButton"
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { appleAuthHelpers, useScript } from 'react-apple-signin-auth'

import { ExtendedWalletList } from './ExtendedWalletList'
import { Banner } from './Banner'
import { WaasCodeInputContent } from './WaasCodeInputContent'

import { KitConfig } from '../../index'
import { LocalStorageKey, defaultSignInOptions } from '../../../constants'
Expand All @@ -31,7 +32,6 @@ import { ExtendedConnector } from '../../../utils/getKitConnectWallets'

import * as styles from '../../styles.css'
import { useEmailAuth } from '../../../hooks/useWaasEmailAuth'
import { PINCodeInput } from './PINCodeInput'
import { getStorageItem } from '../../../utils/storage'

interface ConnectWalletContentProps extends KitConnectProviderProps {
Expand All @@ -57,7 +57,6 @@ export const ConnectWalletContent = (props: ConnectWalletContentProps) => {

const [email, setEmail] = useState('')
const [showEmailWaasPinInput, setShowEmailWaasPinInput] = useState(false)
const [waasEmailPinCode, setWaasEmailPinCode] = useState<string[]>([])
const { connectors: baseConnectors, connect } = useConnect()

// EIP-6963 connectors will not have the _wallet property
Expand Down Expand Up @@ -192,28 +191,10 @@ export const ConnectWalletContent = (props: ConnectWalletContentProps) => {

if (showEmailWaasPinInput) {
return (
<>
<Box paddingY="6" alignItems="center" justifyContent="center" flexDirection="column">
<Text marginTop="5" marginBottom="4" variant="normal" color="text80">
Enter code received in email.
</Text>
<PINCodeInput value={waasEmailPinCode} digits={6} onChange={setWaasEmailPinCode} />

<Box gap="2" marginY="4" alignItems="center" justifyContent="center" style={{ height: '44px' }}>
{emailAuthLoading ? (
<Spinner />
) : (
<Button
variant="primary"
disabled={waasEmailPinCode.includes('')}
label="Verify"
onClick={() => sendChallengeAnswer?.(waasEmailPinCode.join(''))}
data-id="verifyButton"
/>
)}
</Box>
</Box>
</>
<WaasCodeInputContent
isLoading={emailAuthLoading}
onVerify={(code) => { sendChallengeAnswer?.(code) }}
/>
)
}

Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './KitProvider'
export * from './KitProvider/ConnectWalletContent/WaasCodeInputContent'
54 changes: 43 additions & 11 deletions packages/kit/src/hooks/useWaasRevalidation.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
import { SequenceWaaS } from '@0xsequence/waas'
import { commons } from '@0xsequence/core'
import { ethers } from 'ethers'
import { useState, useEffect } from 'react'
import { Deferred } from '../utils/deferred'
import { useAccount } from 'wagmi'
import type { SequenceWaasConnector } from '@0xsequence/kit-connectors'


export function useWaasRevalidation(
) {
interface UseWaasRevalidation {
openWaasRevalidationModal: boolean,
setOpenWaasRevalidationModal: React.Dispatch<React.SetStateAction<boolean>>
onVerify: (code: string) => void
onVerifyIsLoading: boolean
setOnVerifyIsLoading: React.Dispatch<React.SetStateAction<boolean>>
}

export function useWaasRevalidation(): UseWaasRevalidation {
const [openWaasRevalidationModal, setOpenWaasRevalidationModal] = useState(false)
const [onVerifyIsLoading, setOnVerifyIsLoading] = useState(false)
const { connector } = useAccount()

const waasConnector = connector?.type === 'sequence-waas' ? connector : undefined

const onVerify = async (code: string) => {
try {
// @ts-ignore-next-line
const sequenceWaas: SequenceWaaS = waasConnector.sequenceWaas

if (!sequenceWaas) {
return
}

setOnVerifyIsLoading(true)
await sequenceWaas.finishValidateSession(code)
} catch(e) {
console.error(e)
}
setOnVerifyIsLoading(false)
setOpenWaasRevalidationModal(false)
}

useEffect(() => {
if (!openWaasRevalidationModal) {
setOnVerifyIsLoading(false)
}
}, [openWaasRevalidationModal])

useEffect(() => {
async function setup() {
Expand All @@ -28,14 +56,18 @@ export function useWaasRevalidation(
}

sequenceWaas.onValidationRequired(() => {
console.log('TODO: requires validation!')
// Trigger modal + input callback

// sequenceWaas.finishValidateSession('string')
console.log('onValidationRequire')
setOpenWaasRevalidationModal(true)
})
}
setup()
}, [waasConnector])

return
return ({
openWaasRevalidationModal,
setOpenWaasRevalidationModal,
onVerifyIsLoading,
setOnVerifyIsLoading,
onVerify
})
}
Loading

0 comments on commit 7bfd3b5

Please sign in to comment.