-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7761868
commit c085ffe
Showing
14 changed files
with
480 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/app/(dashboard)/categories/_sections/CategorySection.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"use client"; | ||
import HStack from "@/components/shared/layout/HStack"; | ||
import useQueryParams from "@/hooks/useQueryParam"; | ||
import { Category } from "@/types/category"; | ||
import { Pagination } from "@nextui-org/react"; | ||
import CategoryTable from "./CategoryTable"; | ||
|
||
interface Props { | ||
categories: Category[]; | ||
} | ||
const PAGE_SIZE = 10; | ||
|
||
export default function CategorySection({ categories }: Props) { | ||
const { add } = useQueryParams(); | ||
|
||
const totalCategories = categories.length; | ||
const totalPages = Math.ceil(totalCategories / PAGE_SIZE); | ||
|
||
return ( | ||
<section className='px-5 pt-5'> | ||
<CategoryTable | ||
categories={categories} | ||
totalCategories={totalCategories} | ||
/> | ||
{totalPages > 1 && ( | ||
<HStack className='justify-end mt-3'> | ||
<Pagination | ||
total={totalPages} | ||
initialPage={1} | ||
variant='bordered' | ||
showControls | ||
onChange={(page) => add("page", page.toString())} | ||
/> | ||
</HStack> | ||
)} | ||
</section> | ||
); | ||
} |
172 changes: 172 additions & 0 deletions
172
src/app/(dashboard)/categories/_sections/CategoryTable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import EmptyContent from "@/components/shared/EmptyContent"; | ||
import HStack from "@/components/shared/layout/HStack"; | ||
import { Category } from "@/types/category"; | ||
import { | ||
Button, | ||
Table, | ||
TableBody, | ||
TableCell, | ||
TableColumn, | ||
TableHeader, | ||
TableRow, | ||
useDisclosure, | ||
} from "@nextui-org/react"; | ||
import { PencilIcon, Trash2Icon } from "lucide-react"; | ||
import { useEffect, useState } from "react"; | ||
import NewCategoryModal from "./NewCategoryModal"; | ||
import Modal from "@/components/shared/modal"; | ||
import { ERRORS } from "@/config/constants/errors"; | ||
import { useServerAction } from "@/hooks/useServerAction"; | ||
import { deleteCategory } from "@/actions/category"; | ||
import { toast } from "sonner"; | ||
import { getErrorMessage } from "@/utils/helpers"; | ||
|
||
interface CategoryTableProp { | ||
categories: Category[]; | ||
totalCategories: number; | ||
} | ||
export default function CategoryTable(props: CategoryTableProp) { | ||
const { categories, totalCategories } = props; | ||
|
||
const { isOpen, onOpen, onClose } = useDisclosure(); | ||
const [selectedCategory, setSelectedCategory] = useState<Category | null>( | ||
null, | ||
); | ||
|
||
const [runDeleteCategory, { loading }] = useServerAction< | ||
any, | ||
typeof deleteCategory | ||
>(deleteCategory); | ||
|
||
const removeCategory = async () => { | ||
try { | ||
if (!selectedCategory?.id) { | ||
toast.error("Operation not allowed"); | ||
return; | ||
} | ||
|
||
await runDeleteCategory(selectedCategory.id); | ||
toast.success("Category deleted"); | ||
setSelectedCategory(null); | ||
} catch (error) { | ||
toast.error(getErrorMessage(error)); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (!isOpen) { | ||
setSelectedCategory(null); | ||
} | ||
}, [isOpen]); | ||
|
||
return ( | ||
<> | ||
<HStack className='justify-between items-center'> | ||
<div> | ||
<h3 className='font-semibold text-xl'> | ||
{totalCategories} {totalCategories > 1 ? "Category" : "Categories"} | ||
</h3> | ||
<p className='text-gray-400'>List of all categories</p> | ||
</div> | ||
<Button | ||
radius='sm' | ||
size='md' | ||
color='primary' | ||
disableRipple | ||
className='font-semibold' | ||
onClick={onOpen} | ||
> | ||
Add new category | ||
</Button> | ||
</HStack> | ||
<div className='p-3 mt-4 border rounded-md'> | ||
<Table aria-label='list of categories' shadow='none' radius='none'> | ||
<TableHeader> | ||
<TableColumn className='w-1/2'>Name</TableColumn> | ||
<TableColumn>{null}</TableColumn> | ||
</TableHeader> | ||
<TableBody | ||
emptyContent={ | ||
<EmptyContent | ||
// img={illustration_empty_content} | ||
title='' | ||
description='No places added' | ||
/> | ||
} | ||
> | ||
{categories.map((category) => ( | ||
<TableRow key={category?.id}> | ||
<TableCell className='capitalize'>{category?.name}</TableCell> | ||
<TableCell> | ||
<HStack className='items-center justify-end'> | ||
<Button | ||
radius='sm' | ||
size='sm' | ||
disableRipple | ||
color='danger' | ||
onClick={() => setSelectedCategory(category)} | ||
isIconOnly | ||
> | ||
<Trash2Icon className='text-white' size={15} /> | ||
</Button> | ||
<Button | ||
radius='sm' | ||
size='sm' | ||
disableRipple | ||
color='success' | ||
onClick={() => { | ||
setSelectedCategory(category); | ||
onOpen(); | ||
}} | ||
isIconOnly | ||
> | ||
<PencilIcon className='text-white' size={20} /> | ||
</Button> | ||
</HStack> | ||
</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
<NewCategoryModal | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
mode={selectedCategory !== null ? "edit" : "create"} | ||
category={selectedCategory} | ||
/> | ||
<Modal | ||
isOpen={selectedCategory !== null && !isOpen} | ||
onClose={() => setSelectedCategory(null)} | ||
title='Delete Category' | ||
description={ERRORS.MESSAGE.DELETE_PROMPT} | ||
content={ | ||
<HStack className='justify-end'> | ||
<Button | ||
radius='sm' | ||
size='md' | ||
variant='bordered' | ||
disableRipple | ||
className='font-semibold' | ||
onClick={() => setSelectedCategory(null)} | ||
isDisabled={loading} | ||
> | ||
Cancel | ||
</Button> | ||
<Button | ||
radius='sm' | ||
size='md' | ||
color='primary' | ||
disableRipple | ||
className='font-semibold' | ||
onClick={removeCategory} | ||
isLoading={loading} | ||
> | ||
Continue | ||
</Button> | ||
</HStack> | ||
} | ||
/> | ||
</div> | ||
</> | ||
); | ||
} |
Oops, something went wrong.