Skip to content

Commit

Permalink
Merge pull request #600 from 1ifeworld/jawn/straightfromdb
Browse files Browse the repository at this point in the history
new route
  • Loading branch information
jawndiego authored Apr 20, 2024
2 parents c65c71a + 067b1f2 commit 62e9625
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
22 changes: 7 additions & 15 deletions apps/site/context/UserContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use client'

import { getUserId } from '@/gql'
import { getUsername, getUserChannels } from '@/lib'
import { getUserChannels } from '@/lib'
import {
type ConnectedWallet,
type SignMessageModalUIOptions,
Expand All @@ -15,7 +14,7 @@ import React, {
useEffect,
useState,
} from 'react'
import type { Address } from 'viem'
import { getUserDataByOwner } from '@/lib'

const UserContext = createContext<{
embeddedWallet?: ConnectedWallet
Expand Down Expand Up @@ -47,21 +46,14 @@ export function UserContextComponent({ children }: { children: ReactNode }) {
async function fetchUserData() {
if (!embeddedWallet) return

const fetchedUserId = await getUserId({
custodyAddress: embeddedWallet.address as Address,
})
const data = await getUserDataByOwner({ owner: embeddedWallet.address })

if (!fetchedUserId.userId) return
if (!data) return

setUserId(fetchedUserId.userId)
setUserId(data.records.id)
setUsername(data.records.name)

const fetchedUsername = await getUsername({
id: BigInt(fetchedUserId.userId),
})

setUsername(fetchedUsername)

const userChannels = await getUserChannels(fetchedUserId.userId)
const userChannels = await getUserChannels(data.records.id)
if (userChannels) setUserChannels(userChannels)
}

Expand Down
27 changes: 27 additions & 0 deletions apps/site/lib/username/getUserDataByOwner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export async function getUserDataByOwner({ owner }: { owner: string }) {
if (!owner) {
throw new Error('owner is required')
}

try {
const response = await fetch(
`${process.env.NEXT_PUBLIC_USERNAME_SERVICE}/getDataByOwner`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ owner }),
},
)

if (!response.ok) {
throw new Error('Network response was not ok')
}

const data = await response.json()
return data
} catch (error) {
console.error('Fetch error:', error)
}
}
1 change: 1 addition & 0 deletions apps/site/lib/username/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './getUsername'
export * from './setUsername'
export * from './signForUsername'
export * from './getAllFields'
export * from './getUserDataByOwner'

0 comments on commit 62e9625

Please sign in to comment.