feat: add index and create customer form

This commit is contained in:
Riccardo
2024-07-07 22:52:00 +02:00
parent c570cc4d87
commit 330247b3b7
8 changed files with 149 additions and 50 deletions

View File

@@ -7,7 +7,7 @@
- [ ] Add user profile and settings (i.e. language) - [ ] Add user profile and settings (i.e. language)
- [ ] Add user roles - [ ] Add user roles
- [ ] Add user permissions - [ ] Add user permissions
- [ ] Add module CRUD with protected routes - [ ] Add customer form CRUD with protected routes
- [ ] Customize Auth0 login page - [ ] Customize Auth0 login page
- [ ] Add user notifications and emails - [ ] Add user notifications and emails
- [ ] Add user invoices - [ ] Add user invoices

View File

@@ -1,22 +1,83 @@
import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';
import { CustomerFormType } from '@prisma/client';
import prisma from '@prisma/prisma';
import { CustomerForm } from '@utils/types';
import { NextRequest, NextResponse } from 'next/server'; import { NextRequest, NextResponse } from 'next/server';
export async function GET( export const GET = withApiAuthRequired(async request => {
request: NextRequest, const session = await getSession();
{ params }: { params: { id: number } }
) { const x = console.log('request', request);
return NextResponse.json(`GET ${params.id}`);
} const result = await prisma.customerForm.findUnique({
where: {
id: 'params.id',
createdBy: {
email: session?.user.email
}
}
});
if (!result) {
return NextResponse.json(
{ success: false, message: 'Something went wrong.' },
{ status: 500 }
);
}
return NextResponse.json({ success: true, data: [] });
});
export async function PUT( export async function PUT(
request: NextRequest, request: NextRequest,
{ params }: { params: { id: number } } { params }: { params: CustomerForm }
) { ) {
return NextResponse.json(`PUT ${params.id}`); const session = await getSession();
const result = await prisma.customerForm.update({
where: {
id: params.id,
createdBy: {
email: session?.user.email
}
},
data: {
type: params.type as CustomerFormType,
text: params.text
}
});
if (!result) {
return NextResponse.json(
{ success: false, message: 'Something went wrong.' },
{ status: 500 }
);
}
return NextResponse.json({ success: true, data: result });
} }
export async function DELETE( export async function DELETE(
request: NextRequest, request: NextRequest,
{ params }: { params: { id: number } } { params }: { params: { id: string } }
) { ) {
return NextResponse.json(`DELETE ${params.id}`); const session = await getSession();
const result = await prisma.customerForm.delete({
where: {
id: params.id,
createdBy: {
email: session?.user.email
}
}
});
if (!result) {
return NextResponse.json(
{ success: false, message: 'Something went wrong.' },
{ status: 500 }
);
}
return NextResponse.json({ success: true });
} }

View File

@@ -1,14 +1,13 @@
import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0'; import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';
import { CustomerForm } from '@prisma/client'; import { CustomerForm } from '@prisma/client';
import prisma from '@prisma/prisma'; import prisma from '@prisma/prisma';
import { randomUUID } from 'crypto';
import { NextResponse } from 'next/server'; import { NextResponse } from 'next/server';
export const GET = withApiAuthRequired(async () => { export const GET = withApiAuthRequired(async () => {
const session = await getSession(); const session = await getSession();
try { try {
const userModules = await prisma.user.findUniqueOrThrow({ const userCustomerForms = await prisma.user.findUniqueOrThrow({
where: { where: {
email: session?.user.email email: session?.user.email
}, },
@@ -17,7 +16,7 @@ export const GET = withApiAuthRequired(async () => {
} }
}); });
const customerForms: CustomerForm[] = userModules.CustomerForm; const customerForms: CustomerForm[] = userCustomerForms.CustomerForm;
return NextResponse.json({ success: true, data: customerForms }); return NextResponse.json({ success: true, data: customerForms });
} catch (error) { } catch (error) {
@@ -40,10 +39,16 @@ export const POST = withApiAuthRequired(async request => {
text: body.text, text: body.text,
createdBy: { createdBy: {
connect: { connect: {
id: randomUUID(),
email: session?.user.email email: session?.user.email
} }
} }
},
select: {
id: true,
type: true,
text: true,
createdAt: true,
updatedAt: true
} }
}); });

View File

@@ -1,9 +1,42 @@
'use client'; 'use client';
import { CustomerForm, CustomerFormSchema } from '@utils/types';
import axios from 'axios';
import { useEffect, useState } from 'react';
export default function SingleCustomerForm({ export default function SingleCustomerForm({
params params
}: { }: {
params: { id: string }; params: { id: string };
}) { }) {
return <div>Module {params.id}</div>; const [customerForm, setCustomerForm] = useState<CustomerForm | null>(null);
useEffect(() => {
(async () => {
const response = await axios.get(
`/api/protected/customer-form/${params.id}`
);
const validatedResponse = CustomerFormSchema.safeParse(
response.data.data
);
if (!validatedResponse.success) {
console.error(validatedResponse.error);
return;
}
setCustomerForm(validatedResponse.data);
})();
}, [params.id]);
if (!customerForm) {
return <div>Loading...</div>;
}
return (
<div>
Form {params.id} {JSON.stringify(customerForm)}
</div>
);
} }

View File

@@ -10,6 +10,8 @@ import { zodResolver } from '@hookform/resolvers/zod';
import { CustomerFormType } from '@prisma/client'; import { CustomerFormType } from '@prisma/client';
import { import {
CustomerForm, CustomerForm,
CustomerFormCreate,
CustomerFormCreateSchema,
CustomerFormListSchema, CustomerFormListSchema,
CustomerFormSchema CustomerFormSchema
} from '@utils/types'; } from '@utils/types';
@@ -17,11 +19,11 @@ import axios from 'axios';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { FormProvider, useForm } from 'react-hook-form'; import { FormProvider, useForm } from 'react-hook-form';
export default function Modules() { export default function CustomerForms() {
const [modules, setModules] = useState<CustomerForm[]>([]); const [customerForms, setCustomerForms] = useState<CustomerForm[]>([]);
const form = useForm<CustomerForm>({ const form = useForm<CustomerFormCreate>({
resolver: zodResolver(CustomerFormSchema), resolver: zodResolver(CustomerFormCreateSchema),
defaultValues: { defaultValues: {
type: CustomerFormType.TYPE1, type: CustomerFormType.TYPE1,
text: '' text: ''
@@ -41,11 +43,11 @@ export default function Modules() {
return; return;
} }
setModules(validatedResponse.data); setCustomerForms(validatedResponse.data);
})(); })();
}, []); }, []);
async function handleSubmit(values: CustomerForm) { async function handleSubmit(values: CustomerFormCreate) {
console.log('values', values); console.log('values', values);
try { try {
@@ -62,6 +64,8 @@ export default function Modules() {
} }
); );
console.log('response', response.data.data);
const validatedResponse = CustomerFormSchema.safeParse( const validatedResponse = CustomerFormSchema.safeParse(
response.data.data response.data.data
); );
@@ -71,7 +75,7 @@ export default function Modules() {
return; return;
} }
setModules([...modules, validatedResponse.data]); setCustomerForms([...customerForms, validatedResponse.data]);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
@@ -79,15 +83,15 @@ export default function Modules() {
return ( return (
<> <>
<h1>Modules</h1> <h1>Forms</h1>
{modules && {customerForms &&
modules.map(module => ( customerForms.map(customerForm => (
<div key={module.id}> <div key={customerForm.id}>
<h2>{module.type}</h2> <h2>{customerForm.type}</h2>
<p>{module.text}</p> <p>{customerForm.text}</p>
</div> </div>
))} ))}
<h1>Create Module</h1> <h1>New form</h1>
<FormProvider {...form}> <FormProvider {...form}>
<form onSubmit={form.handleSubmit(handleSubmit)}> <form onSubmit={form.handleSubmit(handleSubmit)}>
<FormField <FormField
@@ -102,7 +106,7 @@ export default function Modules() {
</FormItem> </FormItem>
)} )}
/> />
<Button type='submit'>Submit</Button> <Button type='submit'>Create</Button>
</form> </form>
</FormProvider> </FormProvider>
</> </>

View File

@@ -16,7 +16,7 @@
} }
} }
body { /* body {
color: rgb(var(--foreground-rgb)); color: rgb(var(--foreground-rgb));
background: linear-gradient( background: linear-gradient(
to bottom, to bottom,
@@ -24,7 +24,7 @@ body {
rgb(var(--background-end-rgb)) rgb(var(--background-end-rgb))
) )
rgb(var(--background-start-rgb)); rgb(var(--background-start-rgb));
} } */
@layer utilities { @layer utilities {
.text-balance { .text-balance {

View File

@@ -1,21 +1,10 @@
import { cn } from '@utils/cn';
import * as React from 'react'; import * as React from 'react';
const Input = React.forwardRef< const Input = React.forwardRef<
HTMLInputElement, HTMLInputElement,
React.InputHTMLAttributes<HTMLInputElement> React.InputHTMLAttributes<HTMLInputElement>
>(({ className, type, ...props }, ref) => { >(({ type, ...props }, ref) => {
return ( return <input type={type} ref={ref} {...props} />;
<input
type={type}
className={cn(
'border-input bg-background ring-offset-background placeholder:text-muted-foreground focus-visible:ring-ring flex h-10 w-full rounded-md border px-3 py-2 text-sm file:border-0 file:bg-transparent file:text-sm file:font-medium focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50',
className
)}
ref={ref}
{...props}
/>
);
}); });
Input.displayName = 'Input'; Input.displayName = 'Input';

View File

@@ -5,18 +5,25 @@ export const UserSchema = z.object({
name: z.string(), name: z.string(),
email: z.string().email(), email: z.string().email(),
deleted: z.boolean(), deleted: z.boolean(),
createdAt: z.date().transform(date => date.toISOString()), createdAt: z.string().transform(arg => new Date(arg)),
updatedAt: z.date().transform(date => date.toISOString()) updatedAt: z.string().transform(arg => new Date(arg))
}); });
export type User = z.infer<typeof UserSchema>; export type User = z.infer<typeof UserSchema>;
export const CustomerFormCreateSchema = z.object({
type: z.string(),
text: z.string()
});
export type CustomerFormCreate = z.infer<typeof CustomerFormCreateSchema>;
export const CustomerFormSchema = z.object({ export const CustomerFormSchema = z.object({
id: z.string(), id: z.string(),
type: z.string(), type: z.string(),
text: z.string(), text: z.string(),
createdAt: z.date().transform(date => date.toISOString()), createdAt: z.string().transform(arg => new Date(arg)),
updatedAt: z.date().transform(date => date.toISOString()) updatedAt: z.string().transform(arg => new Date(arg))
}); });
export const CustomerFormListSchema = z.array(CustomerFormSchema); export const CustomerFormListSchema = z.array(CustomerFormSchema);