chore: add all the code from original repo

This commit is contained in:
Riccardo
2024-05-23 16:55:29 +02:00
parent d8f9a215eb
commit 85d66215a7
66 changed files with 16668 additions and 122 deletions

View File

@@ -0,0 +1,44 @@
import { CreateButton } from '@/components/UI/CreateButton';
import { Checkbox, FormControlLabel, Stack, TextField } from '@mui/material';
import { nanoid } from 'nanoid';
import { useEffect } from 'react';
import { useFormState } from 'react-dom';
import { CreateCommentAction } from './actions/CreateCommentAction';
interface CreateCommentProps {
itemId: string;
setFormKey: (key: string) => void;
}
export default function CreateComment({
itemId,
setFormKey
}: CreateCommentProps) {
const [formState, formAction] = useFormState(CreateCommentAction, {
itemId,
clear: false
});
useEffect(() => {
if (formState.clear) {
setFormKey(nanoid());
}
}, [setFormKey, formState]);
return (
<form action={formAction}>
<Stack spacing={2} marginY={2}>
<TextField name="body" label="Comment" variant="outlined" required />
<TextField
name="score"
label="Score"
variant="outlined"
type="number"
inputProps={{ step: 1, min: 0, max: 10 }}
/>
<FormControlLabel name="regret" control={<Checkbox />} label="Regret" />
<CreateButton />
</Stack>
</form>
);
}

View File

@@ -0,0 +1,44 @@
'use server';
import { CreateCommentFormSchema } from '../../../../../../data/types';
import prisma from '../../../../../../prisma/prisma';
interface ItemDialogActionProps {
clear: boolean;
itemId: string;
error?: string;
}
export async function CreateCommentAction(
{ itemId }: ItemDialogActionProps,
formData: FormData
) {
const formDataObj = Object.fromEntries(formData.entries());
const validatedBody = CreateCommentFormSchema.safeParse(formDataObj);
if (!validatedBody.success) {
throw new Error('Bad request');
}
const { body, score, regret } = validatedBody.data;
try {
await prisma.itemComment.create({
data: {
body,
score,
regret,
Item: {
connect: {
id: itemId
}
}
}
});
} catch (error) {
throw new Error(`Failed to create comment`);
}
return { clear: true, itemId };
}