added delete item & category functionality
This commit is contained in:
parent
a35b754e86
commit
a070e9a9c2
5 changed files with 64 additions and 4 deletions
|
@ -22,4 +22,13 @@ export async function GET(){
|
||||||
return Response.json(
|
return Response.json(
|
||||||
await Category.find()
|
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)
|
||||||
}
|
}
|
|
@ -22,4 +22,13 @@ export async function GET(){
|
||||||
return Response.json(
|
return Response.json(
|
||||||
await MenuItem.find()
|
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...',
|
loading: editCategory ? 'Updating...' : 'Creating...',
|
||||||
success: editCategory ? 'A category is updated' : 'A new category is created',
|
success: editCategory ? 'A category is updated' : 'A new category is created',
|
||||||
error: editCategory ? 'Fail to update category' : 'Fail to create a new category',
|
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>
|
<label className='-mb-1 text-gray-700 font-semibold text-sm'>Categories</label>
|
||||||
{categories?.length > 0 && categories.map(c => (
|
{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'>
|
<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'>
|
<div className='flex gap-2'>
|
||||||
<button type='button' className='text-green-600 hover:opacity-60'><LiaEditSolid/></button>
|
<button type='button' onClick={() => {setEditCategory(c); setCategoryName(c.name)}} 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={() => handleDeleteCategory(c._id)} className='text-red-600 hover:opacity-60'><IoTrashOutline/></button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
|
|
@ -45,6 +45,25 @@ const EditMenuItemPage = () => {
|
||||||
|
|
||||||
setGoToItems(true)
|
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){
|
if(goToItems){
|
||||||
return redirect('/menu-items')
|
return redirect('/menu-items')
|
||||||
}
|
}
|
||||||
|
@ -63,6 +82,7 @@ const EditMenuItemPage = () => {
|
||||||
<UserTab isAdmin={true}/>
|
<UserTab isAdmin={true}/>
|
||||||
<div className='px-5 max-w-md mx-auto mt-10'>
|
<div className='px-5 max-w-md mx-auto mt-10'>
|
||||||
<MenuItemForm menuItem={menuItem} handleMenuFormSubmit={handleMenuFormSubmit}/>
|
<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'>
|
<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>
|
<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>
|
</div>
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import React, { useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
import EditImage from './EditImage';
|
import EditImage from './EditImage';
|
||||||
import MenuItemProps from './MenuItemProps'
|
import MenuItemProps from './MenuItemProps'
|
||||||
|
import toast from 'react-hot-toast';
|
||||||
|
|
||||||
|
|
||||||
const MenuItemForm = ({handleMenuFormSubmit, menuItem}) => {
|
const MenuItemForm = ({handleMenuFormSubmit, menuItem}) => {
|
||||||
|
@ -30,7 +31,7 @@ const MenuItemForm = ({handleMenuFormSubmit, menuItem}) => {
|
||||||
<input type='text' placeholder='Enter base price' value={basePrice} onChange={ev => setBasePrice(ev.target.value)}/>
|
<input type='text' placeholder='Enter base price' value={basePrice} onChange={ev => setBasePrice(ev.target.value)}/>
|
||||||
</div>
|
</div>
|
||||||
<MenuItemProps props={sizes} setProps={setSizes}/>
|
<MenuItemProps props={sizes} setProps={setSizes}/>
|
||||||
<button type='submit' className='w-full bg-pink-500 text-white p-2 rounded-md hover:opacity-80 duration-300'>Save</button>
|
<button type='submit' className='w-full bg-pink-500 text-white p-2 rounded-md hover:opacity-80 duration-300'>Save</button>
|
||||||
</form>
|
</form>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue