Skip to content

Update create-your-first-crud.mdx #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions pages/tutorials/create-your-first-crud.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1654,25 +1654,27 @@ You can test the form at [localhost:3000/admin/projects](http://localhost:3000/a
And the final code of `PageAdminProjectCreate.tsx` file should look like this.

```tsx filename="src/features/projects/PageAdminProjectCreate.tsx" showLineNumbers
import { Button, Heading } from "@chakra-ui/react";
import { zodResolver } from "@hookform/resolvers/zod";
import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form";
import { Button, Heading } from '@chakra-ui/react';
import { zodResolver } from '@hookform/resolvers/zod';
import { useRouter } from 'next/navigation';
import { useForm } from 'react-hook-form';

import { toastCustom } from "@/components/Toast";
import { AdminBackButton } from "@/features/admin/AdminBackButton";
import { AdminCancelButton } from "@/features/admin/AdminCancelButton";
import { Form } from '@/components/Form';
import { toastCustom } from '@/components/Toast';
import { AdminBackButton } from '@/features/admin/AdminBackButton';
import { AdminCancelButton } from '@/features/admin/AdminCancelButton';
import {
AdminLayoutPage,
AdminLayoutPageContent,
AdminLayoutPageTopBar,
} from "@/features/admin/AdminLayoutPage";
} from '@/features/admin/AdminLayoutPage';
import { ProjectForm } from '@/features/projects/ProjectForm';
import {
ProjectForm,
ProjectFormFields,
} from "@/features/projects/ProjectForm";
import { trpc } from "@/lib/trpc/client";
import { isErrorDatabaseConflict } from "@/lib/trpc/errors";
FormFieldsProject,
zFormFieldsProject,
} from '@/features/projects/schemas';
import { trpc } from '@/lib/trpc/client';
import { isErrorDatabaseConflict } from '@/lib/trpc/errors';

export default function PageAdminProjectCreate() {
const trpcUtils = trpc.useUtils();
Expand All @@ -1682,28 +1684,28 @@ export default function PageAdminProjectCreate() {
onSuccess: async () => {
await trpcUtils.projects.getAll.invalidate();
toastCustom({
status: "success",
title: "Project created with success",
status: 'success',
title: 'Project created with success',
});
router.back();
},
onError: (error) => {
if (isErrorDatabaseConflict(error, "name")) {
form.setError("name", { message: "Name already used" });
if (isErrorDatabaseConflict(error, 'name')) {
form.setError('name', { message: 'Name already used' });
return;
}
toastCustom({
status: "error",
title: "Failed to create the project",
status: 'error',
title: 'Failed to create the project',
});
},
});

const form = useForm<FormFieldsProject>({
resolver: zodResolver(zFormFieldsProject()),
defaultValues: {
name: "",
description: "",
name: '',
description: '',
},
});

Expand Down