diff --git a/app/(main)/manage/categories/components/EditCategories.module.css b/app/(main)/manage/categories/components/EditCategories.module.css new file mode 100644 index 00000000..69d3a140 --- /dev/null +++ b/app/(main)/manage/categories/components/EditCategories.module.css @@ -0,0 +1,70 @@ +.pageContainer { + padding: 0; +} + +.title { + margin-bottom: 8px; +} + +.subtitle { + font-size: 16px; + font-weight: 600; + color: #718096; + + margin-bottom: 40px; +} + +.topButtons { + display: flex; + justify-content: flex-end; + + margin-bottom: 40px; +} + +.categoriesContainer { + display: flex; + flex-direction: column; + gap: 40px; + + width: 100%; + max-width: 640px; +} + +.categoryBlock { + display: flex; + flex-direction: column; + + gap: 16px; +} + +.categoryRow { + display: flex; + align-items: center; + + gap: 16px; +} + +.subcategoryList { + display: flex; + flex-direction: column; + + gap: 12px; + + margin-left: 20px; +} + +.subcategoryRow { + display: flex; + align-items: center; + + gap: 16px; +} + +.inputField { + width: 100%; + max-width: 320px; +} + +.addSection { + margin-top: 8px; +} \ No newline at end of file diff --git a/app/(main)/manage/categories/components/EditCategories.tsx b/app/(main)/manage/categories/components/EditCategories.tsx index fe837106..8772e259 100644 --- a/app/(main)/manage/categories/components/EditCategories.tsx +++ b/app/(main)/manage/categories/components/EditCategories.tsx @@ -1,251 +1,245 @@ 'use client'; import { useState } from 'react'; -import { - updateCategory, - updateSubcategory, - deleteCategory, - deleteSubcategory, - createCategory, - createSubcategory, -} from '@/app/actions/inventory'; +import { Button, Form } from 'react-bootstrap'; + +import styles from './EditCategories.module.css'; type Category = { - category_id: number; name: string; - subcategories: { - subcategory_id: number; - name: string; - }[]; + subcategories: string[]; }; -export default function EditCategories({ - categories, -}: { - categories: Category[]; -}) { +const initialCategories: Category[] = [ + { + name: 'Cleaning', + subcategories: ['Laundry', 'Surface Cleaners'], + }, + { + name: 'Clothing', + subcategories: ['Menswear', 'Womenswear'], + }, + { + name: 'Food', + subcategories: ['Beverages', 'Dry goods', 'Produce'], + }, + { + name: 'Home Goods', + subcategories: ['Bedding', 'Kitchenware'], + }, + { + name: 'Hygiene', + subcategories: [], + }, +]; + +export default function EditCategories() { const [editing, setEditing] = useState(false); - const [showAddCategory, setShowAddCategory] = useState(false); - const [newCategoryName, setNewCategoryName] = useState(''); + const [categories, setCategories] = + useState(initialCategories); - const [addingSubcategory, setAddingSubcategory] = useState( - null, - ); - const [newSubcategoryName, setNewSubcategoryName] = useState(''); + const updateCategoryName = ( + categoryIndex: number, + value: string + ) => { + const updated = [...categories]; + + updated[categoryIndex].name = value; + + setCategories(updated); + }; + + const updateSubcategoryName = ( + categoryIndex: number, + subcategoryIndex: number, + value: string + ) => { + const updated = [...categories]; + + updated[categoryIndex].subcategories[subcategoryIndex] = + value; + + setCategories(updated); + }; + + const removeCategory = (categoryIndex: number) => { + setCategories( + categories.filter((_, i) => i !== categoryIndex) + ); + }; + + const removeSubcategory = ( + categoryIndex: number, + subcategoryIndex: number + ) => { + const updated = [...categories]; - const handleCreateCategory = async () => { - if (!newCategoryName.trim()) return; + updated[categoryIndex].subcategories = + updated[categoryIndex].subcategories.filter( + (_, i) => i !== subcategoryIndex + ); - await createCategory({ name: newCategoryName }); - setNewCategoryName(''); + setCategories(updated); }; - const handleCreateSubcategory = async (categoryId: number) => { - if (!newSubcategoryName.trim()) return; + const addCategory = () => { + setCategories([ + ...categories, + { + name: '', + subcategories: [], + }, + ]); + }; - await createSubcategory({ - name: newSubcategoryName, - category_id: Number(categoryId), - }); + const addSubcategory = (categoryIndex: number) => { + const updated = [...categories]; - setNewSubcategoryName(''); + updated[categoryIndex].subcategories.push(''); + + setCategories(updated); }; return ( -
- +
+

- {/* Add Category */} {editing && ( -
- - - {showAddCategory && ( -
- setNewCategoryName(e.target.value)} - /> - - {newCategoryName && ( - <> - - - - - )} -
- )} -
+

+ Manage inventory categories and subcategories +

)} -
    - {categories?.map((category) => ( -
  • +
    + {editing ? ( + + ) : ( + + )} +
    + +
    + {editing && ( + + )} + + {categories.map((category, categoryIndex) => ( +
    {editing ? ( - - ) : ( - category.name - )} - - {/* Add Subcategory */} - {editing && ( - - )} + <> +
    + + updateCategoryName( + categoryIndex, + e.target.value + ) + } + className={styles.inputField} + /> + + +
    + +
    + {category.subcategories.map( + (subcategory, subcategoryIndex) => ( +
    + + updateSubcategoryName( + categoryIndex, + subcategoryIndex, + e.target.value + ) + } + className={ + styles.inputField + } + /> + + +
    + ) + )} - {editing && addingSubcategory === category.category_id && ( -
    - setNewSubcategoryName(e.target.value)} - /> - - {newSubcategoryName && ( - <> - - - - - )} -
    - )} - -
      - {category.subcategories?.map((sub) => ( -
    • - {editing ? ( - - ) : ( - sub.name + Add Subcategory + +
    +
    + + ) : ( + <> +

    {category.name}

    + +
    + {category.subcategories.map( + (subcategory, subcategoryIndex) => ( +

    + {subcategory} +

    + ) )} -
  • - ))} -
- +
+ + )} +
))} - - - ); -} - -function EditableField({ - id, - name, - type, -}: { - id: number; - name: string; - type: 'category' | 'subcategory'; -}) { - const [value, setValue] = useState(name); - const [isDirty, setIsDirty] = useState(false); - const [loading, setLoading] = useState(false); - - const handleSave = async () => { - try { - setLoading(true); - - if (type === 'category') { - await updateCategory(id, { name: value }); - } else { - await updateSubcategory(id, { name: value }); - } - - setIsDirty(false); - } catch (err) { - console.error(err); - } finally { - setLoading(false); - } - }; - - const handleDelete = async () => { - try { - setLoading(true); - - if (type === 'category') { - await deleteCategory(id); - } else { - await deleteSubcategory(id); - } - } catch (err) { - console.error(err); - } finally { - setLoading(false); - } - }; - - return ( -
- { - setValue(e.target.value); - setIsDirty(e.target.value !== name); - }} - /> - - {isDirty && ( - <> - - - - - )} - - +
); -} +} \ No newline at end of file