From 4aa1ef84631a2f7be30de4c72351e6e12a78bf55 Mon Sep 17 00:00:00 2001 From: Riccardo Date: Mon, 8 Jul 2024 10:47:54 +0200 Subject: [PATCH] style: add styling for pages and forms --- app/api/protected/profile/route.ts | 5 +- app/customer-form/[id]/page.tsx | 19 +++++-- app/customer-form/page.tsx | 82 +++++++++++++++++++----------- app/login/page.tsx | 8 --- app/page.tsx | 69 ++++++++++++++++++++++++- app/profile/page.tsx | 24 --------- utils/types.ts | 4 ++ 7 files changed, 141 insertions(+), 70 deletions(-) delete mode 100644 app/login/page.tsx delete mode 100644 app/profile/page.tsx diff --git a/app/api/protected/profile/route.ts b/app/api/protected/profile/route.ts index 93ea1bf..31da177 100644 --- a/app/api/protected/profile/route.ts +++ b/app/api/protected/profile/route.ts @@ -5,5 +5,8 @@ import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0'; export const GET = withApiAuthRequired(async () => { const session = await getSession(); - return NextResponse.json(session?.user); + return NextResponse.json({ + success: true, + data: { email: session?.user.email } + }); }); diff --git a/app/customer-form/[id]/page.tsx b/app/customer-form/[id]/page.tsx index d7e3853..a4f7718 100644 --- a/app/customer-form/[id]/page.tsx +++ b/app/customer-form/[id]/page.tsx @@ -1,7 +1,6 @@ 'use client'; import { withPageAuthRequired } from '@auth0/nextjs-auth0/client'; -import { Button } from '@components/Button'; import { CustomerForm, CustomerFormSchema } from '@utils/types'; import axios from 'axios'; import { useRouter } from 'next/navigation'; @@ -61,9 +60,19 @@ export default withPageAuthRequired(function SingleCustomerForm({ } return ( - <> -
Form {JSON.stringify(customerForm)}
- - +
+
+ Form data +
+          {JSON.stringify(customerForm, null, 2)}
+        
+
+ +
); }); diff --git a/app/customer-form/page.tsx b/app/customer-form/page.tsx index 39b5f60..8f23c96 100644 --- a/app/customer-form/page.tsx +++ b/app/customer-form/page.tsx @@ -85,36 +85,58 @@ export default withPageAuthRequired(function CustomerForms() { } return ( - <> -

Forms

- {customerForms && - customerForms.map(customerForm => ( -
redirectToCustomerForm(customerForm.id)} - key={customerForm.id} +
+
+

Forms

+
+ {customerForms && + customerForms.map(customerForm => ( +
redirectToCustomerForm(customerForm.id)} + key={customerForm.id} + > +

{customerForm.text}

+

+ Type: {customerForm.type} +

+
+ ))} +
+
+ +
+

New form

+ +
- {customerForm.text} - of type {customerForm.type} -
- ))} -

New form

- - - ( - - - - - - - )} - /> - - - - + ( + + + + + + + )} + /> + + + +
+
); }); diff --git a/app/login/page.tsx b/app/login/page.tsx deleted file mode 100644 index 29122e4..0000000 --- a/app/login/page.tsx +++ /dev/null @@ -1,8 +0,0 @@ -export default function SignIn() { - return ( -
- Login - Logout -
- ); -} diff --git a/app/page.tsx b/app/page.tsx index 91cac2c..c9e414d 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,9 +1,74 @@ +'use client'; + +import { useUser } from '@auth0/nextjs-auth0/client'; +import { ProfileSchema } from '@utils/types'; +import axios from 'axios'; +import { useEffect, useState } from 'react'; + export default function Home() { + const { user, error, isLoading } = useUser(); + + const [profile, setProfile] = useState(''); + + useEffect(() => { + (async () => { + if (!user) return; + + const response = await axios.get('/api/protected/profile'); + + const validatedResponse = ProfileSchema.safeParse(response.data.data); + + if (!validatedResponse.success) { + console.error(validatedResponse.error); + return; + } + + setProfile(validatedResponse.data.email); + })(); + }, [user]); + + if (isLoading) return
Loading...
; + if (error) return
{error.message}
; + return ( -
-

+
+

Next.js + Auth0 Starter

+
); } diff --git a/app/profile/page.tsx b/app/profile/page.tsx deleted file mode 100644 index 6941aad..0000000 --- a/app/profile/page.tsx +++ /dev/null @@ -1,24 +0,0 @@ -'use client'; - -import { withPageAuthRequired } from '@auth0/nextjs-auth0/client'; -import axios from 'axios'; -import { useEffect, useState } from 'react'; - -export default withPageAuthRequired(function Profile() { - const [user, setUser] = useState(); - - useEffect(() => { - (async () => { - const response = await axios.get('/api/protected/profile'); - - setUser(response.data); - })(); - }, []); - - return ( - <> -

Profile (fetched from API)

- {JSON.stringify(user, null, 2)} - - ); -}); diff --git a/utils/types.ts b/utils/types.ts index efe1efe..ffebbad 100644 --- a/utils/types.ts +++ b/utils/types.ts @@ -1,5 +1,9 @@ import { z } from 'zod'; +export const ProfileSchema = z.object({ + email: z.string() +}); + export const CustomerFormCreateSchema = z.object({ type: z.string(), text: z.string()