From ef439d8f1c4118efec0c653cb94fefae9be6e5a1 Mon Sep 17 00:00:00 2001 From: Don Starwsburg Date: Thu, 21 Nov 2024 15:24:37 -0500 Subject: [PATCH] added brands --- src/actions/brandActions.ts | 27 +++++++++ src/app/page.tsx | 9 ++- src/components/Brand/addBrand.tsx | 38 +++++++++++++ src/components/Brand/brand.tsx | 92 +++++++++++++++++++++++++++++++ src/components/Brand/brands.tsx | 52 +++++++++++++++++ src/types/brandType.ts | 4 ++ tsconfig.json | 6 +- 7 files changed, 223 insertions(+), 5 deletions(-) create mode 100644 src/actions/brandActions.ts create mode 100644 src/components/Brand/addBrand.tsx create mode 100644 src/components/Brand/brand.tsx create mode 100644 src/components/Brand/brands.tsx create mode 100644 src/types/brandType.ts diff --git a/src/actions/brandActions.ts b/src/actions/brandActions.ts new file mode 100644 index 0000000..0446835 --- /dev/null +++ b/src/actions/brandActions.ts @@ -0,0 +1,27 @@ +"use server"; +import { eq, not } from "drizzle-orm"; +import { revalidatePath } from "next/cache"; +import { db } from "../db"; +import { brand } from "../db/schema/Brand"; +export const getData = async () => { + const data = await db.select().from(brand); + return data; +}; +export const addBrand = async ( name: string) => { + await db.insert(brand).values({ + name: name, + }); +}; +export const deleteBrand = async (id: number) => { + await db.delete(brand).where(eq(brand.id, id)); + revalidatePath("/"); +}; +export const editBrand = async (id: number, name: string) => { + await db + .update(brand) + .set({ + name: name, + }) + .where(eq(brand.id, id)); + revalidatePath("/"); +}; \ No newline at end of file diff --git a/src/app/page.tsx b/src/app/page.tsx index fa2a9cd..6061353 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -5,8 +5,11 @@ import Hero from "../components/Hero"; import Contact from "../components/Contact"; import Footer from "../components/Footer "; import { ChakraProvider } from "@chakra-ui/react"; +import { getData } from "../actions/brandActions"; +import Brands from "../components/Brand/brands"; -export default function Home() { +export default async function Home() { + const data = await getData(); return (
@@ -15,7 +18,7 @@ export default function Home() {
- +
); -} \ No newline at end of file +} diff --git a/src/components/Brand/addBrand.tsx b/src/components/Brand/addBrand.tsx new file mode 100644 index 0000000..747d4eb --- /dev/null +++ b/src/components/Brand/addBrand.tsx @@ -0,0 +1,38 @@ +"use client"; +import { ChangeEvent, FC, useState } from "react"; +interface Props { + createBrand: (value: string) => void; +} +const AddBrand: FC = ({ createBrand }) => { + // State for handling input value + const [input, setInput] = useState(""); + // Event handler for input change + const handleInput = (e: ChangeEvent) => { + setInput(e.target.value); + }; + // Event handler for adding a new brand + const handleAdd = async () => { + createBrand(input); + setInput(""); + }; + // Rendering the AddBrand component + return ( +
+ {/* Input field for entering new brand text */} + + {/* Button for adding a new brand */} + +
+ ); +}; +export default AddBrand; \ No newline at end of file diff --git a/src/components/Brand/brand.tsx b/src/components/Brand/brand.tsx new file mode 100644 index 0000000..7064a5c --- /dev/null +++ b/src/components/Brand/brand.tsx @@ -0,0 +1,92 @@ +"use client"; +import { ChangeEvent, FC, useState } from "react"; +import { brandType } from "../../types/brandType"; +interface Props { + brand: brandType; + changeBrandName: (id: number, name: string) => void; + + deleteBrand: (id: number) => void; +} +const Brand: FC = ({ + brand, + changeBrandName, + deleteBrand, +}) => { + // State for handling editing mode + const [editing, setEditing] = useState(false); + // State for handling text input + const [name, setName] = useState(brand.name); + // Event handler for text input change + const handleNameChange = (e: ChangeEvent) => { + setName(e.target.value); + }; + // Event handler for initiating the edit mode + const handleEdit = () => { + setEditing(true); + }; + // Event handler for saving the edited text + const handleSave = async () => { + changeBrandName(brand.id, name); + setEditing(false); + }; + // Event handler for canceling the edit mode + const handleCancel = () => { + setEditing(false); + setName(brand.name); + }; + // Event handler for deleting a todo item + const handleDelete = () => { + if (confirm("Are you sure you want to delete this brand?")) { + deleteBrand(brand.id); + } + }; + // Rendering the Todo component + return ( +
+ {/* Checkbox for marking the todo as done */} + + {/* Input field for brand text */} + + {/* Action buttons for editing, saving, canceling, and deleting */} +
+ {editing ? ( + + ) : ( + + )} + {editing ? ( + + ) : ( + + )} +
+
+ ); +}; +export default Brand; \ No newline at end of file diff --git a/src/components/Brand/brands.tsx b/src/components/Brand/brands.tsx new file mode 100644 index 0000000..7346149 --- /dev/null +++ b/src/components/Brand/brands.tsx @@ -0,0 +1,52 @@ +"use client"; +import { FC, useState } from "react"; +import { brandType } from "../../types/brandType"; +import Brand from "./brand"; +import AddBrand from "./addBrand"; +import { addBrand, deleteBrand, editBrand } from "../../actions/brandActions"; +interface Props { + brands: brandType[]; +} +const Brands: FC = ({ brands }) => { + // State to manage the list of todo items + const [brandItems, setBrandItems] = useState(brands); + // Function to create a new todo item + const createBrand = (name: string) => { + const id = (brandItems.at(-1)?.id || 0) + 1; + addBrand(name); + setBrandItems((prev) => [...prev, { id: id, name: name }]); + }; + // Function to change the text of a todo item + const changeBrandName = (id: number, name: string) => { + setBrandItems((prev) => + prev.map((brand) => (brand.id === id ? { ...brand, name } : brand)) + ); + editBrand(id, name); + }; + + // Function to delete a brand item + const deleteTodoItem = (id: number) => { + setBrandItems((prev) => prev.filter((brand) => brand.id !== id)); + deleteBrand(id); + }; + // Rendering the brand List component + return ( +
+
To-do app
+
+ {/* Mapping through todoItems and rendering Todo component for each */} + {brandItems.map((brand) => ( + + ))} +
+ {/* Adding Todo component for creating new todos */} + +
+ ); +}; +export default Brands; \ No newline at end of file diff --git a/src/types/brandType.ts b/src/types/brandType.ts new file mode 100644 index 0000000..3b79d0a --- /dev/null +++ b/src/types/brandType.ts @@ -0,0 +1,4 @@ +export type brandType = { + id: number; + name: string; + }; \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 3868325..dcc5fc7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,8 +20,10 @@ } ], "paths": { - "@/*": ["./src/*"], - + "@/*": [ + "./src/*"], + "@/db/*": [ + "./src/db/*"], "components/*": [ "/src/app/components/*" ],