Skip to content

Commit e794a00

Browse files
committed
Next.js tRPC CRUD App With TypeScript
1 parent 97dfb76 commit e794a00

31 files changed

+1085
-108
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DATABASE_URL="file:./dev.db"

package.json

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,36 @@
33
"scripts": {
44
"dev": "next dev",
55
"build": "next build",
6-
"start": "next start"
6+
"start": "next start",
7+
"db:migrate": "npx prisma migrate dev --name note-entity --create-only && yarn prisma generate",
8+
"db:push": "npx prisma db push"
79
},
810
"dependencies": {
11+
"@hookform/resolvers": "^2.9.10",
12+
"@prisma/client": "^4.5.0",
13+
"@tanstack/react-query": "^4.13.0",
14+
"@tanstack/react-query-devtools": "^4.13.0",
15+
"@trpc/client": "^10.0.0-proxy-beta.26",
16+
"@trpc/next": "^10.0.0-proxy-beta.26",
17+
"@trpc/react-query": "^10.0.0-proxy-beta.26",
18+
"@trpc/server": "^10.0.0-proxy-beta.26",
19+
"date-fns": "^2.29.3",
920
"next": "latest",
1021
"react": "18.2.0",
11-
"react-dom": "18.2.0"
22+
"react-dom": "18.2.0",
23+
"react-hook-form": "^7.38.0",
24+
"react-toastify": "^9.0.8",
25+
"superjson": "^1.11.0",
26+
"tailwind-merge": "^1.7.0",
27+
"zod": "^3.19.1"
1228
},
1329
"devDependencies": {
1430
"@types/node": "18.11.3",
1531
"@types/react": "18.0.21",
1632
"@types/react-dom": "18.0.6",
1733
"autoprefixer": "^10.4.12",
1834
"postcss": "^8.4.18",
35+
"prisma": "^4.5.0",
1936
"tailwindcss": "^3.2.1",
2037
"typescript": "4.8.4"
2138
}

pages/_app.tsx

Lines changed: 0 additions & 8 deletions
This file was deleted.

pages/index.tsx

Lines changed: 0 additions & 86 deletions
This file was deleted.

prisma/dev.db

24 KB
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- CreateTable
2+
CREATE TABLE "notes" (
3+
"id" TEXT NOT NULL PRIMARY KEY,
4+
"title" TEXT NOT NULL,
5+
"content" TEXT NOT NULL,
6+
"category" TEXT,
7+
"published" BOOLEAN DEFAULT false,
8+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
9+
"updatedAt" DATETIME NOT NULL
10+
);
11+
12+
-- CreateIndex
13+
CREATE UNIQUE INDEX "notes_title_key" ON "notes"("title");

prisma/migrations/migration_lock.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (i.e. Git)
3+
provider = "sqlite"

prisma/schema.prisma

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
generator client {
2+
provider = "prisma-client-js"
3+
}
4+
5+
datasource db {
6+
provider = "sqlite"
7+
url = env("DATABASE_URL")
8+
}
9+
10+
model Note {
11+
id String @id @default(uuid())
12+
title String @unique
13+
content String
14+
category String?
15+
published Boolean? @default(false)
16+
17+
createdAt DateTime @default(now())
18+
updatedAt DateTime @updatedAt
19+
20+
@@map(name: "notes")
21+
}

src/components/LoadingButton.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React from "react";
2+
import { twMerge } from "tailwind-merge";
3+
import Spinner from "./Spinner";
4+
5+
type LoadingButtonProps = {
6+
loading: boolean;
7+
btnColor?: string;
8+
textColor?: string;
9+
children: React.ReactNode;
10+
};
11+
12+
export const LoadingButton: React.FC<LoadingButtonProps> = ({
13+
textColor = "text-white",
14+
btnColor = "bg-ct-blue-700",
15+
children,
16+
loading = false,
17+
}) => {
18+
return (
19+
<button
20+
type="submit"
21+
className={twMerge(
22+
`w-full py-3 font-semibold ${btnColor} rounded-lg outline-none border-none flex justify-center`,
23+
`${loading && "bg-[#ccc]"}`
24+
)}
25+
>
26+
{loading ? (
27+
<div className="flex items-center gap-3">
28+
<Spinner />
29+
<span className="text-white inline-block">Loading...</span>
30+
</div>
31+
) : (
32+
<span className={`text-lg font-normal ${textColor}`}>{children}</span>
33+
)}
34+
</button>
35+
);
36+
};

src/components/Spinner.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
import { twMerge } from 'tailwind-merge';
3+
type SpinnerProps = {
4+
width?: number;
5+
height?: number;
6+
color?: string;
7+
bgColor?: string;
8+
};
9+
const Spinner: React.FC<SpinnerProps> = ({
10+
width = 5,
11+
height = 5,
12+
color,
13+
bgColor,
14+
}) => {
15+
return (
16+
<svg
17+
role='status'
18+
className={twMerge(
19+
'w-5 h-5 mr-2 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600',
20+
`w-${width} h-${height} ${color} ${bgColor}`
21+
)}
22+
viewBox='0 0 100 101'
23+
fill='none'
24+
xmlns='http://www.w3.org/2000/svg'
25+
>
26+
<path
27+
d='M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z'
28+
fill='currentColor'
29+
/>
30+
<path
31+
d='M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z'
32+
fill='currentFill'
33+
/>
34+
</svg>
35+
);
36+
};
37+
38+
export default Spinner;

src/components/note.modal.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import ReactDom from "react-dom";
2+
import React, { FC } from "react";
3+
4+
type INoteModal = {
5+
openNoteModal: boolean;
6+
setOpenNoteModal: (open: boolean) => void;
7+
children: React.ReactNode;
8+
};
9+
10+
const NoteModal: FC<INoteModal> = ({
11+
openNoteModal,
12+
setOpenNoteModal,
13+
children,
14+
}) => {
15+
if (!openNoteModal) return null;
16+
return ReactDom.createPortal(
17+
<>
18+
<div
19+
className="fixed inset-0 bg-[rgba(0,0,0,.5)] z-[1000]"
20+
onClick={() => setOpenNoteModal(false)}
21+
></div>
22+
<div className="max-w-lg w-full rounded-md fixed top-[5%] xl:top-[10%] left-1/2 -translate-x-1/2 bg-white z-[1001] p-6">
23+
{children}
24+
</div>
25+
</>,
26+
document.getElementById("note-modal") as HTMLElement
27+
);
28+
};
29+
30+
export default NoteModal;

src/components/notes/create.note.tsx

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { FC } from "react";
2+
import { SubmitHandler, useForm } from "react-hook-form";
3+
import { twMerge } from "tailwind-merge";
4+
import { object, string, TypeOf } from "zod";
5+
import { zodResolver } from "@hookform/resolvers/zod";
6+
import { LoadingButton } from "../LoadingButton";
7+
import { toast } from "react-toastify";
8+
import { trpc } from "~/utils/trpc";
9+
import { useQueryClient } from "@tanstack/react-query";
10+
11+
type ICreateNoteProps = {
12+
setOpenNoteModal: (open: boolean) => void;
13+
};
14+
15+
const createNoteSchema = object({
16+
title: string().min(1, "Title is required"),
17+
content: string().min(1, "Content is required"),
18+
});
19+
20+
type CreateNoteInput = TypeOf<typeof createNoteSchema>;
21+
22+
const CreateNote: FC<ICreateNoteProps> = ({ setOpenNoteModal }) => {
23+
const queryClient = useQueryClient();
24+
const { isLoading, mutate: createNote } = trpc.createNote.useMutation({
25+
onSuccess() {
26+
queryClient.invalidateQueries([["getNotes"], { limit: 10, page: 1 }]);
27+
setOpenNoteModal(false);
28+
toast("Note created successfully", {
29+
type: "success",
30+
position: "top-right",
31+
});
32+
},
33+
onError(error) {
34+
setOpenNoteModal(false);
35+
toast(error.message, {
36+
type: "error",
37+
position: "top-right",
38+
});
39+
},
40+
});
41+
const methods = useForm<CreateNoteInput>({
42+
resolver: zodResolver(createNoteSchema),
43+
});
44+
45+
const {
46+
register,
47+
handleSubmit,
48+
formState: { errors },
49+
} = methods;
50+
51+
const onSubmitHandler: SubmitHandler<CreateNoteInput> = async (data) => {
52+
createNote(data);
53+
};
54+
return (
55+
<section>
56+
<div className="flex justify-between items-center mb-3 pb-3 border-b border-gray-200">
57+
<h2 className="text-2xl text-ct-dark-600 font-semibold">Create Note</h2>
58+
<div
59+
onClick={() => setOpenNoteModal(false)}
60+
className="text-2xl text-gray-400 hover:bg-gray-200 hover:text-gray-900 rounded-lg p-1.5 ml-auto inline-flex items-center cursor-pointer"
61+
>
62+
<i className="bx bx-x"></i>
63+
</div>
64+
</div>
65+
<form className="w-full" onSubmit={handleSubmit(onSubmitHandler)}>
66+
<div className="mb-2">
67+
<label className="block text-gray-700 text-lg mb-2" htmlFor="title">
68+
Title
69+
</label>
70+
<input
71+
className={twMerge(
72+
`appearance-none border border-gray-400 rounded w-full py-3 px-3 text-gray-700 mb-2 leading-tight focus:outline-none`,
73+
`${errors["title"] && "border-red-500"}`
74+
)}
75+
{...methods.register("title")}
76+
/>
77+
<p
78+
className={twMerge(
79+
`text-red-500 text-xs italic mb-2 invisible`,
80+
`${errors["title"] && "visible"}`
81+
)}
82+
>
83+
{errors["title"]?.message as string}
84+
</p>
85+
</div>
86+
<div className="mb-2">
87+
<label className="block text-gray-700 text-lg mb-2" htmlFor="title">
88+
Content
89+
</label>
90+
<textarea
91+
className={twMerge(
92+
`appearance-none border border-gray-400 rounded w-full py-3 px-3 text-gray-700 mb-2 leading-tight focus:outline-none`,
93+
`${errors.content && "border-red-500"}`
94+
)}
95+
rows={6}
96+
{...register("content")}
97+
/>
98+
<p
99+
className={twMerge(
100+
`text-red-500 text-xs italic mb-2`,
101+
`${errors.content ? "visible" : "invisible"}`
102+
)}
103+
>
104+
{errors.content && errors.content.message}
105+
</p>
106+
</div>
107+
<LoadingButton loading={isLoading}>Create Note</LoadingButton>
108+
</form>
109+
</section>
110+
);
111+
};
112+
113+
export default CreateNote;

0 commit comments

Comments
 (0)