Skip to content

Commit

Permalink
feat: affiliate
Browse files Browse the repository at this point in the history
  • Loading branch information
viet-nv committed Jan 22, 2025
1 parent 0e1ade7 commit 5088964
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/hooks/useAffiliate.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import { useEffect } from 'react'
import { useEffect, useRef } from 'react'
import { useSearchParams } from 'react-router-dom'

import { AFFILIATE_SERVICE_URL } from 'constants/env'
import { useWeb3React } from 'hooks'
import { useWalletModalToggle } from 'state/application/hooks'

import { useAuth } from './useAuth'

export const useAffiliate = () => {
const [searchParams] = useSearchParams()
const refCode = searchParams.get('refCode')

const { account } = useWeb3React()
const toggleWalletModal = useWalletModalToggle()

const { accessToken, login } = useAuth()

const ref = useRef(false)

useEffect(() => {
if (!refCode) return

// connect wallet first
if (!account) {
toggleWalletModal()
return
}

if (!accessToken) {
login()
} else {
} else if (!ref.current) {
ref.current = true
fetch(`${AFFILIATE_SERVICE_URL}/v1/public/affiliates/session`, {
method: 'POST',
headers: {
Expand All @@ -27,5 +42,6 @@ export const useAffiliate = () => {
.then(res => res.json())
.then(console.log)
}
}, [refCode, login, accessToken])
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [refCode, accessToken, account, login])
}

0 comments on commit 5088964

Please sign in to comment.