From fc4dc5880636e6128ca2ae38cb818c1919105fde Mon Sep 17 00:00:00 2001 From: thatguyinabeanie Date: Sat, 9 Nov 2024 22:31:06 -0600 Subject: [PATCH 1/4] test direct drizzle --- apps/nextjs/src/app/api/uploadthing/core.ts | 9 ++- apps/nextjs/src/app/dashboard/layout.tsx | 8 +-- .../src/app/organizations/[org_slug]/page.tsx | 4 +- .../(tournament_page)/page.tsx | 1 + apps/nextjs/src/app/organizations/page.tsx | 10 ++- apps/nextjs/src/app/players/players-table.tsx | 3 +- .../app/server-actions/accounts/actions.ts | 64 +++++++++--------- .../server-actions/organizations/actions.ts | 65 ++++++++++++------- apps/nextjs/src/components/battle-stadium.tsx | 2 +- .../nextjs/src/components/cookies/cookies.tsx | 2 +- apps/nextjs/src/components/footer.tsx | 14 +++- .../src/components/navbar/navbar-link.tsx | 3 +- .../components/navbar/navbar-mobile-menu.tsx | 1 + .../components/navbar/navbar-right-menu.tsx | 2 +- .../navbar/user-menu/user-menu-dropdown.tsx | 10 ++- .../organizations/organization-card.tsx | 6 +- .../organizations/organization-logo.tsx | 10 +-- .../organizations/partnered-organizations.tsx | 6 +- apps/nextjs/src/trpc/react.tsx | 4 +- packages/api/src/root.ts | 2 + packages/api/src/router/organizations.ts | 18 +++++ packages/db/package.json | 3 +- packages/db/src/index.ts | 3 + pnpm-lock.yaml | 52 ++++++++------- 24 files changed, 190 insertions(+), 112 deletions(-) create mode 100644 packages/api/src/router/organizations.ts diff --git a/apps/nextjs/src/app/api/uploadthing/core.ts b/apps/nextjs/src/app/api/uploadthing/core.ts index 74ae7de1..d0fb201f 100644 --- a/apps/nextjs/src/app/api/uploadthing/core.ts +++ b/apps/nextjs/src/app/api/uploadthing/core.ts @@ -24,9 +24,12 @@ export const UploadThingRouter = { }) .onUploadComplete(({ metadata, file }) => { // This code RUNS ON YOUR SERVER after upload - console.log("Upload complete for userId:", metadata.userId); - - console.log("file url", file.url); + console.log( + "Upload complete for userId:", + metadata.userId, + "file url", + file.url, + ); // !!! Whatever is returned here is sent to the client `onClientUploadComplete` callback return { uploadedBy: metadata.userId }; diff --git a/apps/nextjs/src/app/dashboard/layout.tsx b/apps/nextjs/src/app/dashboard/layout.tsx index ee0226f6..f7e3f476 100644 --- a/apps/nextjs/src/app/dashboard/layout.tsx +++ b/apps/nextjs/src/app/dashboard/layout.tsx @@ -1,16 +1,16 @@ import type { DashboardLayoutProps } from "~/types"; import { getAccountMe } from "~/app/server-actions/accounts/actions"; -export default async function DashboardLayout({ +export default async function DashboardLayout ({ children, }: Readonly) { - const me = (await getAccountMe())?.data; + const me = (await getAccountMe()); return (

TODO: Dashboard

- {me &&

Welcome, {me.email}

} - {children} + { me &&

Welcome, { me.email }

} + { children }
); } diff --git a/apps/nextjs/src/app/organizations/[org_slug]/page.tsx b/apps/nextjs/src/app/organizations/[org_slug]/page.tsx index 703efb42..c7a5a6e7 100644 --- a/apps/nextjs/src/app/organizations/[org_slug]/page.tsx +++ b/apps/nextjs/src/app/organizations/[org_slug]/page.tsx @@ -23,9 +23,9 @@ export async function generateMetadata( } export async function generateStaticParams() { - const { partners, nonpartners } = await getOrganizations(); + const orgs = await getOrganizations(); - return [...partners, ...nonpartners].map(({ slug }) => ({ org_slug: slug })); + return orgs.map(({ slug }) => ({ org_slug: slug })); } // const columns = [ diff --git a/apps/nextjs/src/app/organizations/[org_slug]/tournaments/[tournament_id]/(tournament_page)/page.tsx b/apps/nextjs/src/app/organizations/[org_slug]/tournaments/[tournament_id]/(tournament_page)/page.tsx index f9b6b515..16a66152 100644 --- a/apps/nextjs/src/app/organizations/[org_slug]/tournaments/[tournament_id]/(tournament_page)/page.tsx +++ b/apps/nextjs/src/app/organizations/[org_slug]/tournaments/[tournament_id]/(tournament_page)/page.tsx @@ -95,6 +95,7 @@ function TournamentDetailChips(props: Readonly) { Light Flat Register diff --git a/apps/nextjs/src/app/organizations/page.tsx b/apps/nextjs/src/app/organizations/page.tsx index e75c6f85..3b7de9a5 100644 --- a/apps/nextjs/src/app/organizations/page.tsx +++ b/apps/nextjs/src/app/organizations/page.tsx @@ -2,6 +2,7 @@ import type { Metadata } from "next"; import type { Tournament } from "~/lib/api"; import PartneredOrganizations from "~/components/organizations/partnered-organizations"; +import { getOrganizations } from "../server-actions/organizations/actions"; export const metadata: Metadata = { title: "Organizations", @@ -11,7 +12,8 @@ export interface OrganizationsPageProps { orgs: Tournament[]; } -export default function OrganizationsPage() { +export default async function OrganizationsPage() { + const orgs = await getOrganizations(); return ( <>
@@ -20,6 +22,12 @@ export default function OrganizationsPage() {

TODO: Organizations Table

+ {orgs.map((org) => ( +
+

{org.name}

+

{org.description}

+
+ ))}
); diff --git a/apps/nextjs/src/app/players/players-table.tsx b/apps/nextjs/src/app/players/players-table.tsx index 616292e5..ef07545b 100644 --- a/apps/nextjs/src/app/players/players-table.tsx +++ b/apps/nextjs/src/app/players/players-table.tsx @@ -29,7 +29,8 @@ export default function PlayersTable({ // switch (columnKey) { // case "username": // return ( -// +// // {username} // // ); diff --git a/apps/nextjs/src/app/server-actions/accounts/actions.ts b/apps/nextjs/src/app/server-actions/accounts/actions.ts index 72e2cdaa..25a668e3 100644 --- a/apps/nextjs/src/app/server-actions/accounts/actions.ts +++ b/apps/nextjs/src/app/server-actions/accounts/actions.ts @@ -1,49 +1,49 @@ "use server"; -import type { FetchOptions } from "openapi-fetch"; import { auth } from "@clerk/nextjs/server"; -import type { paths } from "~/lib/api/openapi-v1"; -import { BattleStadiumApiClient, defaultConfig } from "~/lib/api"; +import { db, eq } from "@battle-stadium/db"; +import { accounts, clerkUsers, profiles } from "@battle-stadium/db/schema"; -export async function getAccounts( - options?: FetchOptions, -) { - const usersOptions = { - ...defaultConfig("listUsers"), - ...options, - }; - const skipClerkAuth = true; - - return BattleStadiumApiClient(skipClerkAuth).GET("/accounts", usersOptions); +export async function getAccounts () { + return await db.query.accounts.findMany(); } -export async function getAccount( - username: string, - options?: FetchOptions, -) { - const userOptions = { - ...defaultConfig(`getUser-${username}`), - ...options, - params: { path: { username } }, - }; - - return BattleStadiumApiClient().GET("/accounts/{username}", userOptions); +export async function getAccount (username: string) { + const profile = await db.query.profiles.findFirst({ + where: eq(profiles.username, username), + }); + + if (!profile?.accountId) { + return null; + } + + return await db.query.accounts.findFirst({ + where: eq(accounts.id, BigInt(profile.accountId)), + }); } -export async function getAccountMe( - options?: FetchOptions, -) { +export async function getAccountMe () { const { userId } = await auth(); + console.log('userId', userId); if (!userId) { return null; } - const userMeOptions = { - ...defaultConfig(`getAccountMe(${userId})`), - ...options, - }; + const clerkUser = await db.query.clerkUsers.findFirst({ + where: eq(clerkUsers.clerkUserId, userId), + }); + + console.log('clerkUser', clerkUser); + if (!clerkUser?.accountId) { + return null; + } + + const account = await db.query.accounts.findFirst({ + where: eq(accounts.id, BigInt(clerkUser.accountId)), + }); - return BattleStadiumApiClient().GET("/accounts/me", userMeOptions); + console.log('account', account); + return account; } diff --git a/apps/nextjs/src/app/server-actions/organizations/actions.ts b/apps/nextjs/src/app/server-actions/organizations/actions.ts index 8e2067f8..e9a76acb 100644 --- a/apps/nextjs/src/app/server-actions/organizations/actions.ts +++ b/apps/nextjs/src/app/server-actions/organizations/actions.ts @@ -2,38 +2,53 @@ import type { FetchOptions } from "openapi-fetch"; +import { eq } from "@battle-stadium/db"; +import { db } from "@battle-stadium/db/client"; +import { organizations } from "@battle-stadium/db/schema"; + import type { paths } from "~/lib/api/openapi-v1"; import { BattleStadiumApiClient, defaultConfig } from "~/lib/api"; -export async function getOrganizations( - options?: FetchOptions, -) { - const organizationsOptions = { - ...defaultConfig("getOrganizations"), - ...options, - params: { - query: { - page: options?.params.query.page ?? 0, - per_page: options?.params.query.per_page ?? 20, - }, - }, - }; - const skipClerkAuth = true; +// export async function getOrganizations( +// options?: FetchOptions, +// ) { +// const organizationsOptions = { +// ...defaultConfig("getOrganizations"), +// ...options, +// params: { +// query: { +// page: options?.params.query.page ?? 0, +// per_page: options?.params.query.per_page ?? 20, +// }, +// }, +// }; +// const skipClerkAuth = true; - const resp = await BattleStadiumApiClient(skipClerkAuth).GET( - "/organizations", - organizationsOptions, - ); - const allOrgs = resp.data?.data; - const partnerOrgs = (allOrgs ?? []).filter((org) => org.partner); - const nonPartnerOrgs = (allOrgs ?? []).filter((org) => !org.partner); +// const resp = await BattleStadiumApiClient(skipClerkAuth).GET( +// "/organizations", +// organizationsOptions, +// ); +// const allOrgs = resp.data?.data; +// const partnerOrgs = (allOrgs ?? []).filter((org) => org.partner); +// const nonPartnerOrgs = (allOrgs ?? []).filter((org) => !org.partner); - return { - partners: partnerOrgs, - nonpartners: nonPartnerOrgs, - }; +// return { +// partners: partnerOrgs, +// nonpartners: nonPartnerOrgs, +// }; +// } + +export async function getOrganizations() { + const orgs = await db.query.organizations.findMany(); + return orgs; } +export async function getPartneredOrganizations() { + const orgs = await db.query.organizations.findMany({ + where: eq(organizations.partner, true), + }); + return orgs; +} export async function getOrganization( slug: string, options?: FetchOptions, diff --git a/apps/nextjs/src/components/battle-stadium.tsx b/apps/nextjs/src/components/battle-stadium.tsx index 4f3ba5d6..a9990bd3 100644 --- a/apps/nextjs/src/components/battle-stadium.tsx +++ b/apps/nextjs/src/components/battle-stadium.tsx @@ -32,7 +32,7 @@ export default function BattleStadium() { className="flex flex-row gap-2 md:gap-4" >
- + We use cookies to provide the best experience. By continuing to use our site, you agree to our  - + Cookie Policy.

diff --git a/apps/nextjs/src/components/footer.tsx b/apps/nextjs/src/components/footer.tsx index 1ed5df65..cbe6cd90 100644 --- a/apps/nextjs/src/components/footer.tsx +++ b/apps/nextjs/src/components/footer.tsx @@ -54,7 +54,12 @@ export default function Footer() {
{navLinks.map((item) => ( - + {item.name} ))} @@ -62,7 +67,12 @@ export default function Footer() { {/* */}
{socialItems.map((item) => ( - + {item.name}