├── .eslintrc.json ├── public ├── favicon.ico └── vercel.svg ├── postcss.config.js ├── src ├── styles │ └── globals.css ├── utils │ └── trpc.ts ├── pages │ ├── api │ │ └── trpc │ │ │ └── [trpc].ts │ ├── _app.tsx │ └── index.tsx ├── server │ ├── context.ts │ └── router.ts └── components │ └── Card.tsx ├── next.config.js ├── prisma ├── migrations │ ├── migration_lock.toml │ └── 20220605160819_init │ │ └── migration.sql └── schema.prisma ├── README.md ├── next-env.d.ts ├── tailwind.config.js ├── .gitignore ├── tsconfig.json └── package.json /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "next/core-web-vitals" 3 | } 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FranciscoMendes10866/next-tailwind-trpc-prisma/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | } 7 | -------------------------------------------------------------------------------- /src/styles/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | 5 | * { 6 | font-family: "Poppins"; 7 | } -------------------------------------------------------------------------------- /next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = { 3 | reactStrictMode: true, 4 | } 5 | 6 | module.exports = nextConfig 7 | -------------------------------------------------------------------------------- /prisma/migrations/migration_lock.toml: -------------------------------------------------------------------------------- 1 | # Please do not edit this file manually 2 | # It should be added in your version-control system (i.e. Git) 3 | provider = "sqlite" -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # next-tailwind-trpc-prisma 2 | Simple Grocery List 🛍 3 | 4 | ![image](https://res.cloudinary.com/dj5iihhqv/image/upload/v1654636896/Kapture_2022-06-07_at_22.19.07-min_vsi8p5.gif) -------------------------------------------------------------------------------- /src/utils/trpc.ts: -------------------------------------------------------------------------------- 1 | import type { ServerRouter } from "@/server/router"; 2 | import { createReactQueryHooks } from "@trpc/react"; 3 | 4 | export const trpc = createReactQueryHooks(); 5 | -------------------------------------------------------------------------------- /next-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | 4 | // NOTE: This file should not be edited 5 | // see https://nextjs.org/docs/basic-features/typescript for more information. 6 | -------------------------------------------------------------------------------- /prisma/migrations/20220605160819_init/migration.sql: -------------------------------------------------------------------------------- 1 | -- CreateTable 2 | CREATE TABLE "GroceryList" ( 3 | "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 4 | "title" TEXT NOT NULL, 5 | "checked" BOOLEAN DEFAULT false 6 | ); 7 | -------------------------------------------------------------------------------- /tailwind.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | content: [ 3 | "./src/pages/**/*.{js,ts,jsx,tsx}", 4 | "./src/components/**/*.{js,ts,jsx,tsx}", 5 | ], 6 | theme: { 7 | extend: {}, 8 | }, 9 | plugins: [], 10 | } 11 | -------------------------------------------------------------------------------- /src/pages/api/trpc/[trpc].ts: -------------------------------------------------------------------------------- 1 | import * as trpcNext from "@trpc/server/adapters/next"; 2 | 3 | import { serverRouter } from "@/server/router"; 4 | import { createContext } from "@/server/context"; 5 | 6 | export default trpcNext.createNextApiHandler({ 7 | router: serverRouter, 8 | createContext, 9 | }); 10 | -------------------------------------------------------------------------------- /prisma/schema.prisma: -------------------------------------------------------------------------------- 1 | generator client { 2 | provider = "prisma-client-js" 3 | } 4 | 5 | datasource db { 6 | provider = "sqlite" 7 | url = "file:./dev.db" 8 | } 9 | 10 | model GroceryList { 11 | id Int @id @default(autoincrement()) 12 | title String 13 | checked Boolean? @default(false) 14 | } 15 | -------------------------------------------------------------------------------- /src/server/context.ts: -------------------------------------------------------------------------------- 1 | import * as trpc from "@trpc/server"; 2 | import * as trpcNext from "@trpc/server/adapters/next"; 3 | import { PrismaClient } from "@prisma/client"; 4 | 5 | export async function createContext(opts?: trpcNext.CreateNextContextOptions) { 6 | const prisma = new PrismaClient(); 7 | 8 | return { prisma }; 9 | } 10 | 11 | export type Context = trpc.inferAsyncReturnType; 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | .pnpm-debug.log* 27 | 28 | # local env files 29 | .env*.local 30 | 31 | # vercel 32 | .vercel 33 | 34 | # typescript 35 | *.tsbuildinfo 36 | 37 | #prisma 38 | prisma/dev.db -------------------------------------------------------------------------------- /src/pages/_app.tsx: -------------------------------------------------------------------------------- 1 | import "../styles/globals.css"; 2 | import "@fontsource/poppins"; 3 | import { withTRPC } from "@trpc/next"; 4 | import { AppType } from "next/dist/shared/lib/utils"; 5 | import type { ServerRouter } from "@/server/router"; 6 | 7 | const App: AppType = ({ Component, pageProps }) => { 8 | return ; 9 | }; 10 | 11 | export default withTRPC({ 12 | config({ ctx }) { 13 | const url = process.env.VERCEL_URL 14 | ? `https://${process.env.VERCEL_URL}/api/trpc` 15 | : "http://localhost:3000/api/trpc"; 16 | 17 | return { url }; 18 | }, 19 | ssr: true, 20 | })(App); 21 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "forceConsistentCasingInFileNames": true, 9 | "noEmit": true, 10 | "esModuleInterop": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "jsx": "preserve", 16 | "incremental": true, 17 | "baseUrl": ".", 18 | "paths": { 19 | "@/*": [ 20 | "src/*" 21 | ], 22 | } 23 | }, 24 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], 25 | "exclude": ["node_modules"] 26 | } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grocery", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "next dev", 7 | "build": "next build", 8 | "start": "next start", 9 | "lint": "next lint" 10 | }, 11 | "dependencies": { 12 | "@fontsource/poppins": "^4.5.8", 13 | "@prisma/client": "^3.14.0", 14 | "@tailwindcss/forms": "^0.5.2", 15 | "@trpc/client": "^9.24.0", 16 | "@trpc/next": "^9.24.0", 17 | "@trpc/react": "^9.24.0", 18 | "@trpc/server": "^9.24.0", 19 | "next": "12.1.6", 20 | "react": "18.1.0", 21 | "react-dom": "18.1.0", 22 | "react-query": "^3.39.1", 23 | "zod": "^3.17.3" 24 | }, 25 | "devDependencies": { 26 | "@types/node": "17.0.40", 27 | "@types/react": "18.0.11", 28 | "@types/react-dom": "18.0.5", 29 | "autoprefixer": "^10.4.7", 30 | "eslint": "8.17.0", 31 | "eslint-config-next": "12.1.6", 32 | "postcss": "^8.4.14", 33 | "prisma": "^3.14.0", 34 | "tailwindcss": "^3.0.24", 35 | "typescript": "4.7.3" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /public/vercel.svg: -------------------------------------------------------------------------------- 1 | 3 | 4 | -------------------------------------------------------------------------------- /src/server/router.ts: -------------------------------------------------------------------------------- 1 | import * as trpc from "@trpc/server"; 2 | import { z } from "zod"; 3 | 4 | import { Context } from "./context"; 5 | 6 | export const serverRouter = trpc 7 | .router() 8 | .query("findAll", { 9 | resolve: async ({ ctx }) => { 10 | return await ctx.prisma.groceryList.findMany(); 11 | }, 12 | }) 13 | .mutation("insertOne", { 14 | input: z.object({ 15 | title: z.string(), 16 | }), 17 | resolve: async ({ input, ctx }) => { 18 | return await ctx.prisma.groceryList.create({ 19 | data: { title: input.title }, 20 | }); 21 | }, 22 | }) 23 | .mutation("updateOne", { 24 | input: z.object({ 25 | id: z.number(), 26 | title: z.string(), 27 | checked: z.boolean(), 28 | }), 29 | resolve: async ({ input, ctx }) => { 30 | const { id, ...rest } = input; 31 | 32 | return await ctx.prisma.groceryList.update({ 33 | where: { id }, 34 | data: { ...rest }, 35 | }); 36 | }, 37 | }) 38 | .mutation("deleteAll", { 39 | input: z.object({ 40 | ids: z.number().array(), 41 | }), 42 | resolve: async ({ input, ctx }) => { 43 | const { ids } = input; 44 | 45 | return await ctx.prisma.groceryList.deleteMany({ 46 | where: { 47 | id: { in: ids }, 48 | }, 49 | }); 50 | }, 51 | }); 52 | 53 | export type ServerRouter = typeof serverRouter; 54 | -------------------------------------------------------------------------------- /src/pages/index.tsx: -------------------------------------------------------------------------------- 1 | import type { NextPage } from "next"; 2 | import Head from "next/head"; 3 | import { useCallback, useState } from "react"; 4 | import { trpc } from "@/utils/trpc"; 5 | 6 | import { 7 | Card, 8 | CardContent, 9 | CardForm, 10 | CardHeader, 11 | List, 12 | ListItem, 13 | } from "../components/Card"; 14 | import { GroceryList } from "@prisma/client"; 15 | 16 | const Home: NextPage = () => { 17 | const [itemName, setItemName] = useState(""); 18 | 19 | const { data: list, refetch } = trpc.useQuery(["findAll"]); 20 | const insertMutation = trpc.useMutation(["insertOne"], { 21 | onSuccess: () => refetch(), 22 | }); 23 | const deleteAllMutation = trpc.useMutation(["deleteAll"], { 24 | onSuccess: () => refetch(), 25 | }); 26 | const updateOneMutation = trpc.useMutation(["updateOne"], { 27 | onSuccess: () => refetch(), 28 | }); 29 | 30 | const insertOne = useCallback(() => { 31 | if (itemName === "") return; 32 | 33 | insertMutation.mutate({ 34 | title: itemName, 35 | }); 36 | 37 | setItemName(""); 38 | }, [itemName, insertMutation]); 39 | 40 | const clearAll = useCallback(() => { 41 | if (list?.length) { 42 | deleteAllMutation.mutate({ 43 | ids: list.map((item) => item.id), 44 | }); 45 | } 46 | }, [list, deleteAllMutation]); 47 | 48 | const updateOne = useCallback( 49 | (item: GroceryList) => { 50 | updateOneMutation.mutate({ 51 | ...item, 52 | checked: !item.checked, 53 | }); 54 | }, 55 | [updateOneMutation] 56 | ); 57 | 58 | return ( 59 | <> 60 | 61 | Grocery List 62 | 63 | 64 | 65 | 66 |
67 | 68 | 69 | 74 | 75 | {list?.map((item) => ( 76 | 77 | ))} 78 | 79 | 80 | setItemName(e.target.value)} 83 | submit={insertOne} 84 | /> 85 | 86 |
87 | 88 | ); 89 | }; 90 | 91 | export default Home; 92 | -------------------------------------------------------------------------------- /src/components/Card.tsx: -------------------------------------------------------------------------------- 1 | import React, { memo } from "react"; 2 | import type { NextPage } from "next"; 3 | import { GroceryList } from "@prisma/client"; 4 | 5 | interface CardProps { 6 | children: React.ReactNode; 7 | } 8 | 9 | export const Card: NextPage = ({ children }) => { 10 | return ( 11 |
12 | {children} 13 |
14 | ); 15 | }; 16 | 17 | export const CardContent: NextPage = ({ children }) => { 18 | return ( 19 |
20 | {children} 21 |
22 | ); 23 | }; 24 | 25 | interface CardHeaderProps { 26 | title: string; 27 | listLength: number; 28 | clearAllFn?: () => void; 29 | } 30 | 31 | export const CardHeader: NextPage = ({ 32 | title, 33 | listLength, 34 | clearAllFn, 35 | }) => { 36 | return ( 37 |
38 |
39 |

40 | {title} 41 |

42 | 43 | {listLength} 44 | 45 |
46 | 53 |
54 | ); 55 | }; 56 | 57 | export const List: NextPage = ({ children }) => { 58 | return
{children}
; 59 | }; 60 | 61 | interface ListItemProps { 62 | item: GroceryList; 63 | onUpdate?: (item: GroceryList) => void; 64 | } 65 | 66 | const ListItemComponent: NextPage = ({ item, onUpdate }) => { 67 | return ( 68 |
69 | onUpdate?.(item)} 74 | /> 75 |

{item.title}

76 |
77 | ); 78 | }; 79 | 80 | export const ListItem = memo(ListItemComponent); 81 | 82 | interface CardFormProps { 83 | value: string; 84 | onChange: (e: React.ChangeEvent) => void; 85 | submit: () => void; 86 | } 87 | 88 | export const CardForm: NextPage = ({ 89 | value, 90 | onChange, 91 | submit, 92 | }) => { 93 | return ( 94 |
95 |
96 | 103 | 123 |
124 |
125 | ); 126 | }; 127 | --------------------------------------------------------------------------------