Files
ballistic-builder/src/components/Brand/brands.tsx

52 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-11-21 15:24:37 -05:00
"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<Props> = ({ brands }) => {
2024-11-21 15:43:21 -05:00
// State to manage the list of brand items
2024-11-21 15:24:37 -05:00
const [brandItems, setBrandItems] = useState<brandType[]>(brands);
2024-11-21 15:43:21 -05:00
// Function to create a new brand item
2024-11-21 15:24:37 -05:00
const createBrand = (name: string) => {
const id = (brandItems.at(-1)?.id || 0) + 1;
addBrand(name);
setBrandItems((prev) => [...prev, { id: id, name: name }]);
};
2024-11-21 15:43:21 -05:00
// Function to change the text of a brand item
2024-11-21 15:24:37 -05:00
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 (
<main className="flex mx-auto max-w-xl w-full min-h-screen flex-col items-center p-16">
2024-11-21 15:43:21 -05:00
<div className="text-5xl font-medium">Ballistic Builder Brand</div>
2024-11-21 15:24:37 -05:00
<div className="w-full flex flex-col mt-8 gap-2">
2024-11-21 15:43:21 -05:00
{/* Mapping through brand items and rendering brand component for each */}
2024-11-21 15:24:37 -05:00
{brandItems.map((brand) => (
<Brand
key={brand.id}
brand={brand}
changeBrandName={changeBrandName}
deleteBrand={deleteBrand}
/>
))}
</div>
2024-11-21 15:43:21 -05:00
{/* Adding brand component for creating new brand */}
2024-11-21 15:24:37 -05:00
<AddBrand createBrand={createBrand} />
</main>
);
};
export default Brands;