diff --git a/donutshop_ecommerce/src/app/api/categories/route.js b/donutshop_ecommerce/src/app/api/categories/route.js index 856a195..4e23686 100644 --- a/donutshop_ecommerce/src/app/api/categories/route.js +++ b/donutshop_ecommerce/src/app/api/categories/route.js @@ -22,4 +22,13 @@ export async function GET(){ return Response.json( await Category.find() ) +} + +export async function DELETE(req){ + mongoose.connect(process.env.MONGO_URL) + const url = new URL(req.url); + const _id = url.searchParams.get('_id'); + await Category.deleteOne({_id}) + + return Response.json(true) } \ No newline at end of file diff --git a/donutshop_ecommerce/src/app/api/menu-items/route.js b/donutshop_ecommerce/src/app/api/menu-items/route.js index 2a70373..87d7236 100644 --- a/donutshop_ecommerce/src/app/api/menu-items/route.js +++ b/donutshop_ecommerce/src/app/api/menu-items/route.js @@ -22,4 +22,13 @@ export async function GET(){ return Response.json( await MenuItem.find() ) +} + +export async function DELETE(req){ + mongoose.connect(process.env.MONGO_URL) + const url = new URL(req.url); + const _id = url.searchParams.get('_id'); + await MenuItem.deleteOne({_id}) + + return Response.json(true) } \ No newline at end of file diff --git a/donutshop_ecommerce/src/app/categories/page.js b/donutshop_ecommerce/src/app/categories/page.js index 0c976e5..83724e7 100644 --- a/donutshop_ecommerce/src/app/categories/page.js +++ b/donutshop_ecommerce/src/app/categories/page.js @@ -51,7 +51,28 @@ const CategoriesPage = () => { loading: editCategory ? 'Updating...' : 'Creating...', success: editCategory ? 'A category is updated' : 'A new category is created', error: editCategory ? 'Fail to update category' : 'Fail to create a new category', + }); + + } + + async function handleDeleteCategory(_id){ + const promise = new Promise(async (resolve, reject) => { + const response = await fetch('/api/categories?_id='+_id, { + method: 'DELETE', + }) + if(response.ok){ + resolve(); + } + else{ + reject(); + } }) + await toast.promise(promise, { + loading: 'Deleting...', + success: 'A category is deleted', + error: 'Fail to delete!', + }); + fetchCategories(); } @@ -89,10 +110,10 @@ const CategoriesPage = () => { {categories?.length > 0 && categories.map(c => (
{setEditCategory(c); setCategoryName(c.name)}} className='cursor-pointer hover:text-gray-600'>{c.name}
+{c.name}