From 067b1f2511c5ef0e36cc4e9fbd48425149a39723 Mon Sep 17 00:00:00 2001 From: jawndiego Date: Sat, 20 Apr 2024 15:20:22 -0500 Subject: [PATCH] new route --- apps/site/context/UserContext.tsx | 22 +++++----------- apps/site/lib/username/getUserDataByOwner.ts | 27 ++++++++++++++++++++ apps/site/lib/username/index.ts | 1 + 3 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 apps/site/lib/username/getUserDataByOwner.ts diff --git a/apps/site/context/UserContext.tsx b/apps/site/context/UserContext.tsx index 71490488..744e5a87 100644 --- a/apps/site/context/UserContext.tsx +++ b/apps/site/context/UserContext.tsx @@ -1,7 +1,6 @@ 'use client' -import { getUserId } from '@/gql' -import { getUsername, getUserChannels } from '@/lib' +import { getUserChannels } from '@/lib' import { type ConnectedWallet, type SignMessageModalUIOptions, @@ -15,7 +14,7 @@ import React, { useEffect, useState, } from 'react' -import type { Address } from 'viem' +import { getUserDataByOwner } from '@/lib' const UserContext = createContext<{ embeddedWallet?: ConnectedWallet @@ -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) } diff --git a/apps/site/lib/username/getUserDataByOwner.ts b/apps/site/lib/username/getUserDataByOwner.ts new file mode 100644 index 00000000..64d8fd39 --- /dev/null +++ b/apps/site/lib/username/getUserDataByOwner.ts @@ -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) + } +} diff --git a/apps/site/lib/username/index.ts b/apps/site/lib/username/index.ts index cb554040..ab503528 100644 --- a/apps/site/lib/username/index.ts +++ b/apps/site/lib/username/index.ts @@ -4,3 +4,4 @@ export * from './getUsername' export * from './setUsername' export * from './signForUsername' export * from './getAllFields' +export * from './getUserDataByOwner'