Skip to content
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

🐣 Send address for scoring when recalculating #64

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/frontend/src/backend/getPassportScore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isApiErrorResponse } from '@/types/api/error'
import {
GetPassportScorerNonceResponseSchema,
GetResponseSchema,
GetScoreResponse,
SubmitAddressForScoringRequest,
SubmitAddressForScoringResponseSchema,
} from '@/types/api/scorer'
Expand All @@ -11,19 +12,21 @@ import { Hex } from 'viem'
import { useAccount, useChainId, useSignMessage } from 'wagmi'
import { handleBackendRequest } from './handleBackendRequest'

export const useRequestScore = () => {
export const useRequestScore = (onSuccess: (data: GetScoreResponse | undefined) => void) => {
const { address } = useAccount()
const chainId = useChainId()
const { signMessageAsync } = useSignMessage()

const { mutateAsync, isSuccess, isError } = useMutation({
mutationFn: async () => {
const { mutateAsync, isSuccess, isError, reset } = useMutation({
mutationFn: async (isRecalculating: boolean) => {
if (!address) {
throw new Error('No address')
}

try {
return await getGitcoinScore(address, chainId)
if (!isRecalculating) {
return await getGitcoinScore(address, chainId)
}
} catch (error) {
if (!(error instanceof ErrorWithStatus) || error.status != 404) {
throw error
Expand All @@ -34,9 +37,13 @@ export const useRequestScore = () => {
const signature = await signMessageAsync({ message: nonceData.message })
await sendForScoring({ userAddress: address as Hex, signature, nonce: nonceData.nonce })
},
onSuccess,
})
const requestScore = useCallback(() => handleBackendRequest(mutateAsync()), [mutateAsync])
return { requestScore, isSuccess, isError }
const requestScore = useCallback(
(isRecalculating: boolean = false) => handleBackendRequest(mutateAsync(isRecalculating)),
[mutateAsync],
)
return { requestScore, isSuccess, isError, reset }
}

class ErrorWithStatus extends Error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ export const CheckGitcoinScore = ({
const step = gitcoinRequestSettled ? 2 : 1

useEffect(() => {
if (data && data.status == 'done') {
if (queryEnabled && data && data.status == 'done') {
setGitcoinCredentials(data)
}
}, [data, setGitcoinCredentials])
}, [data, setGitcoinCredentials, queryEnabled])

return (
<Wrapper>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useState } from 'react'
import { useCallback, useState } from 'react'
import { CheckGitcoinPassport } from './CheckGitcoinPassport'
import { CheckGitcoinScore } from './CheckingGitcoinScore'
import { UserGitcoinScore } from '@/components/userActious/gitcoin/UserGitcoinScore'
import { MissingGitcoinPassport } from './MissingGitcoinPassport'
import { useRequestScore } from '@/backend/getPassportScore'
import { GitcoinCredentials } from '@/types/passport/GticoinCredentials'
import { GetScoreResponseSuccess } from '@/types/api/scorer'
import { GetScoreResponse } from '@/types/api/scorer'
import { useQueryClient } from '@tanstack/react-query'

enum GitcoinState {
INITIAL_PAGE,
Expand All @@ -18,59 +19,69 @@ const ScoreDecimals = 8
const ScoreMultiplier = 10 ** ScoreDecimals

interface Props {
setGitcoinCredentials: (credentials: GitcoinCredentials) => void
setGitcoinCredentials: (credentials: GitcoinCredentials | undefined) => void
gitcoinCredentials: GitcoinCredentials | undefined
gitcoinSettled: () => void
}

export const GitcoinFlow = ({ gitcoinCredentials, setGitcoinCredentials, gitcoinSettled }: Props) => {
const [gitcoinState, setGitcoinState] = useState<GitcoinState>(GitcoinState.INITIAL_PAGE)
const { requestScore, isSuccess, isError } = useRequestScore()

const setCredentials = (credentials: GetScoreResponseSuccess) => {
setGitcoinCredentials({
score: BigInt(credentials?.score),
proof: credentials.signature,
})
setGitcoinState(GitcoinState.YOUR_SCORE)
}
const setCredentials = useCallback(
(data: GetScoreResponse | undefined) => {
if (data?.status !== 'done') {
return
}

setGitcoinCredentials({
score: BigInt(data.score),
proof: data.signature,
})
setGitcoinState(GitcoinState.YOUR_SCORE)
},
[setGitcoinCredentials, setGitcoinState],
)

const sendForScoring = async () => {
const data = await requestScore()
if (data?.status === 'done') {
setCredentials(data)
}
const { requestScore, isSuccess, isError, reset } = useRequestScore(setCredentials)

const onInitialCheckScore = async () => {
setGitcoinState(GitcoinState.CHECKING_SCORE)
await requestScore(false)
}
const queryClient = useQueryClient()

const onCheckScoreClick = () => {
const onRecalculate = async () => {
await queryClient.invalidateQueries({ queryKey: ['gitcoinScore'] })
reset()
setGitcoinCredentials(undefined)
setGitcoinState(GitcoinState.CHECKING_SCORE)
sendForScoring()
await requestScore(true)
}

switch (gitcoinState) {
case GitcoinState.INITIAL_PAGE:
return <CheckGitcoinPassport onCheckScoreClick={onCheckScoreClick} />
return <CheckGitcoinPassport onCheckScoreClick={onInitialCheckScore} />
case GitcoinState.CHECKING_SCORE:
return (
<CheckGitcoinScore
setGitcoinCredentials={setCredentials}
gitcoinRequestSettled={isSuccess}
gitcoinRequestError={isError}
onSignAgainClick={sendForScoring}
onSignAgainClick={() => requestScore(true)}
/>
)
case GitcoinState.YOUR_SCORE:
return (
<UserGitcoinScore
userScore={Number(gitcoinCredentials?.score) / ScoreMultiplier}
gitcoinSettled={gitcoinSettled}
getBackToScoring={onCheckScoreClick}
getBackToScoring={onRecalculate}
/>
)
case GitcoinState.MISSING_PASSPORT:
return <MissingGitcoinPassport afterCreateClick={() => setGitcoinState(GitcoinState.INITIAL_PAGE)} />

default:
return <CheckGitcoinPassport onCheckScoreClick={onCheckScoreClick} />
return <CheckGitcoinPassport onCheckScoreClick={onInitialCheckScore} />
}
}