added delete item & category functionality
This commit is contained in:
parent
a35b754e86
commit
a070e9a9c2
5 changed files with 64 additions and 4 deletions
|
@ -23,3 +23,12 @@ export async function GET(){
|
|||
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)
|
||||
}
|
|
@ -23,3 +23,12 @@ export async function GET(){
|
|||
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)
|
||||
}
|
|
@ -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 = () => {
|
|||
<label className='-mb-1 text-gray-700 font-semibold text-sm'>Categories</label>
|
||||
{categories?.length > 0 && categories.map(c => (
|
||||
<div className='w-full p-2 flex justify-between my-2 rounded-md outline-none border border-[#DCA0AE] duration-300'>
|
||||
<p onClick={() => {setEditCategory(c); setCategoryName(c.name)}} className='cursor-pointer hover:text-gray-600'>{c.name}</p>
|
||||
<p>{c.name}</p>
|
||||
<div className='flex gap-2'>
|
||||
<button type='button' className='text-green-600 hover:opacity-60'><LiaEditSolid/></button>
|
||||
<button type='button' className='text-red-600 hover:opacity-60'><IoTrashOutline/></button>
|
||||
<button type='button' onClick={() => {setEditCategory(c); setCategoryName(c.name)}} className='text-green-600 hover:opacity-60'><LiaEditSolid/></button>
|
||||
<button type='button' onClick={() => handleDeleteCategory(c._id)} className='text-red-600 hover:opacity-60'><IoTrashOutline/></button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
|
|
@ -45,6 +45,25 @@ const EditMenuItemPage = () => {
|
|||
|
||||
setGoToItems(true)
|
||||
}
|
||||
async function handleDeleteMenuItem(){
|
||||
const promise = new Promise(async (resolve, reject) => {
|
||||
const response = await fetch('/api/menu-items?_id='+id, {
|
||||
method: 'DELETE',
|
||||
});
|
||||
if(response.ok)
|
||||
resolve();
|
||||
else
|
||||
reject();
|
||||
});
|
||||
await toast.promise(promise, {
|
||||
loading: 'Deleting...',
|
||||
success: 'An item is deleted',
|
||||
error: 'Fail to delete an item!',
|
||||
});
|
||||
setGoToItems(true)
|
||||
}
|
||||
|
||||
|
||||
if(goToItems){
|
||||
return redirect('/menu-items')
|
||||
}
|
||||
|
@ -63,6 +82,7 @@ const EditMenuItemPage = () => {
|
|||
<UserTab isAdmin={true}/>
|
||||
<div className='px-5 max-w-md mx-auto mt-10'>
|
||||
<MenuItemForm menuItem={menuItem} handleMenuFormSubmit={handleMenuFormSubmit}/>
|
||||
<button onClick={handleDeleteMenuItem} className='w-full bg-pink-700 text-white p-2 rounded-md hover:opacity-80 duration-300 mt-2'>Delete</button>
|
||||
<div className='max-w-md mx-auto mt-2'>
|
||||
<Link href={'/menu-items'} className='w-full p-2 flex justify-center items-center my-2 rounded-md outline-none border border-[#DCA0AE] hover:opacity-60 duration-300'>Show all menu items</Link>
|
||||
</div>
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import React, { useState } from 'react'
|
||||
import EditImage from './EditImage';
|
||||
import MenuItemProps from './MenuItemProps'
|
||||
import toast from 'react-hot-toast';
|
||||
|
||||
|
||||
const MenuItemForm = ({handleMenuFormSubmit, menuItem}) => {
|
||||
|
|
Loading…
Add table
Reference in a new issue