From c5f4efd44f473a3b18ea5059ecf6d213c217a23d Mon Sep 17 00:00:00 2001 From: Fergal Moran Date: Tue, 17 Oct 2023 00:22:51 +0100 Subject: [PATCH] Basic adding child done --- src/app/api/child/create/route.ts | 24 +++ src/app/api/child/route.ts | 6 +- src/app/layout.tsx | 2 + .../children/add-child-component.tsx | 28 +--- src/components/children/child-select-list.tsx | 2 + src/components/forms/add-child-form.tsx | 153 +++++++++--------- src/components/icons.tsx | 4 + src/lib/services/auth/config.ts | 9 +- 8 files changed, 115 insertions(+), 113 deletions(-) create mode 100644 src/app/api/child/create/route.ts diff --git a/src/app/api/child/create/route.ts b/src/app/api/child/create/route.ts new file mode 100644 index 0000000..94f1aaa --- /dev/null +++ b/src/app/api/child/create/route.ts @@ -0,0 +1,24 @@ +import { newChildSchema } from '@/lib/validations/child'; +import { getServerAuthSession } from '@/lib/services/auth/config'; +import { NextResponse } from 'next/server'; +import { StatusCodes, getReasonPhrase } from 'http-status-codes'; +import db from '@/db/schema'; +import { children } from '@/db/schema/children'; + +export async function POST(req: Request) { + const session = await getServerAuthSession(); + if (!session) + return NextResponse.json( + { error: getReasonPhrase(StatusCodes.UNAUTHORIZED) }, + { status: StatusCodes.UNAUTHORIZED } + ); + const body = await req.json(); + + const { name } = newChildSchema.parse(body); + + const child = await db.insert(children).values({ + name, + }); + + return NextResponse.json({ status: 'success', pin: 'fartle' }); +} diff --git a/src/app/api/child/route.ts b/src/app/api/child/route.ts index 39c30e4..e30d8b2 100644 --- a/src/app/api/child/route.ts +++ b/src/app/api/child/route.ts @@ -1,12 +1,12 @@ import db from '@/db/schema'; import { children } from '@/db/schema/children'; -import { getServerSession } from 'next-auth'; import { StatusCodes, getReasonPhrase } from 'http-status-codes'; import { NextResponse } from 'next/server'; -import authOptions from '@/lib/services/auth/config'; +import { getServerAuthSession } from '@/lib/services/auth/config'; +//TODO: create-t3-app supports app router now export async function GET(request: Request) { - const session = await getServerSession(authOptions); + const session = await getServerAuthSession(); if (!session) return NextResponse.json( { error: getReasonPhrase(StatusCodes.UNAUTHORIZED) }, diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 49d44c4..85bc76b 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -9,6 +9,7 @@ import NextAuthProvider from '@/lib/services/auth/provider'; import { ThemeProvider } from '@/components/theme-provider'; import { cn } from '@/lib/utils'; import TanstackProvider from '@/components/providers/tanstack-provider'; +import { Toaster } from '@/components/ui/toaster'; const font = Sanchez({ subsets: ['latin'], weight: '400' }); @@ -46,6 +47,7 @@ export default function RootLayout({ enableSystem > {children} + diff --git a/src/components/children/add-child-component.tsx b/src/components/children/add-child-component.tsx index e4024df..39876cc 100644 --- a/src/components/children/add-child-component.tsx +++ b/src/components/children/add-child-component.tsx @@ -12,6 +12,7 @@ import { DialogTitle, DialogTrigger, } from '@/components/ui/dialog'; +import { AddChildForm } from '@/components/forms/add-child-form'; const AddChildComponent = () => { return ( @@ -24,32 +25,7 @@ const AddChildComponent = () => { - - Add Child - - { - "Enter your child's details below and press save, then use the displayed PIN to register their device." - } - - -
-
- - -
-
- - - +
); diff --git a/src/components/children/child-select-list.tsx b/src/components/children/child-select-list.tsx index eac8754..525c055 100644 --- a/src/components/children/child-select-list.tsx +++ b/src/components/children/child-select-list.tsx @@ -25,6 +25,8 @@ const ChildSelectList = () => { return data as ChildModel[]; }, }); + if (isLoading) return
Loading....
; + if (isError) return
Error loading
; return ( - {errors?.email && ( -

- {errors.email.message} -

+ {errors?.name && ( +

{errors.name.message}

)} - + +
+ + +
-
-
- -
-
- - Or continue with - -
-
- ); } diff --git a/src/components/icons.tsx b/src/components/icons.tsx index 3505181..561bced 100644 --- a/src/components/icons.tsx +++ b/src/components/icons.tsx @@ -14,6 +14,8 @@ import { PlusCircleIcon, PlusIcon, LogIn, + Save, + Copy, } from 'lucide-react'; export type Icon = LucideIcon; @@ -22,11 +24,13 @@ export const Icons = { add: PlusIcon, chevronLeft: ChevronLeft, chevronRight: ChevronRight, + copy: Copy, sun: SunMedium, login: LogIn, mobile: TabletSmartphone, moon: Moon, rocket: Rocket, + save: Save, spinner: Loader2, twitter: Twitter, user: User, diff --git a/src/lib/services/auth/config.ts b/src/lib/services/auth/config.ts index b56796a..be90497 100644 --- a/src/lib/services/auth/config.ts +++ b/src/lib/services/auth/config.ts @@ -1,7 +1,7 @@ -import { type AuthOptions } from "next-auth"; -import GoogleProvider from "next-auth/providers/google"; -import { DrizzleAdapter } from "@auth/drizzle-adapter"; -import db from "@/db/schema"; +import { getServerSession, type AuthOptions } from 'next-auth'; +import GoogleProvider from 'next-auth/providers/google'; +import { DrizzleAdapter } from '@auth/drizzle-adapter'; +import db from '@/db/schema'; const authOptions: AuthOptions = { adapter: DrizzleAdapter(db), @@ -12,5 +12,6 @@ const authOptions: AuthOptions = { }), ], }; +export const getServerAuthSession = () => getServerSession(authOptions); export default authOptions;