diff --git a/donutshop_ecommerce/src/app/api/categories/route.js b/donutshop_ecommerce/src/app/api/categories/route.js index a8a93a9..856a195 100644 --- a/donutshop_ecommerce/src/app/api/categories/route.js +++ b/donutshop_ecommerce/src/app/api/categories/route.js @@ -1,6 +1,8 @@ +import mongoose from "mongoose"; import { Category } from "../models/Category"; export async function POST(req){ + mongoose.connect(process.env.MONGO_URL) const {name} = await req.json(); const categoryDoc = await Category.create({name}) @@ -8,13 +10,15 @@ export async function POST(req){ } export async function PUT(req){ + mongoose.connect(process.env.MONGO_URL) const {_id, name} = await req.json(); await Category.updateOne({_id}, {name}); return Response.json(true); } -export async function GET(req){ +export async function GET(){ + mongoose.connect(process.env.MONGO_URL) return Response.json( await Category.find() ) diff --git a/donutshop_ecommerce/src/app/api/menu-items/route.js b/donutshop_ecommerce/src/app/api/menu-items/route.js new file mode 100644 index 0000000..0b98acb --- /dev/null +++ b/donutshop_ecommerce/src/app/api/menu-items/route.js @@ -0,0 +1,10 @@ +import mongoose from "mongoose"; +import { MenuItem } from "../models/MenuItem"; + +export async function POST(req){ + mongoose.connect(process.env.MONGO_URL) + const data = await req.json(); + const menuItemDoc = await MenuItem.create(data); + + return Response.json(menuItemDoc) +} \ No newline at end of file diff --git a/donutshop_ecommerce/src/app/api/models/MenuItem.js b/donutshop_ecommerce/src/app/api/models/MenuItem.js new file mode 100644 index 0000000..02d02ea --- /dev/null +++ b/donutshop_ecommerce/src/app/api/models/MenuItem.js @@ -0,0 +1,19 @@ +import { Schema, model, models } from "mongoose"; + +const MenuItemSchema = new Schema({ + itemName:{ + type: String + }, + menuImg:{ + type: String + }, + description:{ + type: String + }, + basePrice:{ + type: Number + }, + +}, {timestamps: true}) + +export const MenuItem = models?.MenuItem || model('MenuItem', MenuItemSchema) \ No newline at end of file diff --git a/donutshop_ecommerce/src/app/menu-items/page.js b/donutshop_ecommerce/src/app/menu-items/page.js index 28cb8b3..b54a8ea 100644 --- a/donutshop_ecommerce/src/app/menu-items/page.js +++ b/donutshop_ecommerce/src/app/menu-items/page.js @@ -1,13 +1,40 @@ "use client" -import React from 'react' +import React, { useState } from 'react' import UserTab from '../../components/layout/UserTab' import useProfile from '../../components/UseProfile' +import EditImage from '../../components/layout/EditImage' +import toast from 'react-hot-toast' const MenuItemsPage = () => { const {loading, data} = useProfile(); + const [menuImg, setMenuImg] = useState(''); + const [itemName, setItemName] = useState(''); + const [description, setDescription] = useState(''); + const [basePrice, setBasePrice] = useState(''); + async function handleMenuFormSubmit(ev){ + ev.preventDefault(); + const data = {menuImg, itemName, description, basePrice,}; + const menuSavingPromise = new Promise(async (resolve, reject) => { + const response = await fetch('/api/menu-items', { + method: 'POST', + body: JSON.stringify(data), + headers: {'Content-Type': 'application/json'} + }); + if(response.ok) + resolve(); + else + reject(); + }); + await toast.promise(menuSavingPromise, { + loading: 'Saving...', + success: 'Menu is saved', + error: 'Fail to save menu!', + }) + } + if(loading){ return

Loading...

@@ -16,22 +43,30 @@ const MenuItemsPage = () => { return

Please login as an admin

} + return ( -
+
-
-
- -
-
- -
-
- -
+
+ +
+
-
- +
+ + setItemName(ev.target.value)}/> +
+
+ + setDescription(ev.target.value)}/> +
+
+ + setBasePrice(ev.target.value)}/> +
+ + +
) } diff --git a/donutshop_ecommerce/src/app/profile/page.js b/donutshop_ecommerce/src/app/profile/page.js index ba0b6da..8a7e2fa 100644 --- a/donutshop_ecommerce/src/app/profile/page.js +++ b/donutshop_ecommerce/src/app/profile/page.js @@ -5,7 +5,7 @@ import { redirect } from 'next/navigation'; import React, { useEffect, useState } from 'react' import toast from 'react-hot-toast'; import UserTabs from "../../components/layout/UserTab" - +import EditImage from "../../components/layout/EditImage" const ProfilePage = () => { //use session @@ -65,31 +65,6 @@ const ProfilePage = () => { }) } - async function handleImgchange(ev){ - const files = ev.target.files; - if(files?.length === 1){ - const data = new FormData; - data.set('file', files[0]); - - - const uploadPromise = fetch('/api/upload', { - method: 'POST', - body: data, - }).then(response => { - if(response.ok){ - return response.json().then(link => { - setImage(link) - }); - } - throw new Error('Something went wrong!') - }); - await toast.promise(uploadPromise, { - loading: 'Uploading', - success: 'An image is uploaded', - error: 'Fail to upload image!', - }) - } - } if(status === 'loading' || !profileFetchd){ return

Loading...

@@ -105,13 +80,7 @@ const ProfilePage = () => {
- {image && ( - {'user-image'} - )} - +
@@ -148,7 +117,7 @@ const ProfilePage = () => { setCountry(ev.target.value)}/>
- +
diff --git a/donutshop_ecommerce/src/components/layout/EditImage.js b/donutshop_ecommerce/src/components/layout/EditImage.js new file mode 100644 index 0000000..be921d0 --- /dev/null +++ b/donutshop_ecommerce/src/components/layout/EditImage.js @@ -0,0 +1,48 @@ +import Image from 'next/image'; +import React from 'react' +import toast from 'react-hot-toast'; + +const EditImage = ({link, setLink}) => { + async function handleImgchange(ev){ + const files = ev.target.files; + if(files?.length === 1){ + const data = new FormData; + data.set('file', files[0]); + + const uploadPromise = fetch('/api/upload', { + method: 'POST', + body: data, + }).then(response => { + if(response.ok){ + return response.json().then(link => { + setLink(link) + }); + } + throw new Error('Something went wrong!') + }); + await toast.promise(uploadPromise, { + loading: 'Uploading', + success: 'An image is uploaded', + error: 'Fail to upload image!', + }) + } + } + + + return ( + <> + {link && ( + user-image + )} + {!link && ( +
No image
+ )} + + + ) +} + +export default EditImage \ No newline at end of file diff --git a/donutshop_ecommerce/src/components/menu/MenuItem.js b/donutshop_ecommerce/src/components/menu/MenuItem.js index 1aaf3ad..eeebb1b 100644 --- a/donutshop_ecommerce/src/components/menu/MenuItem.js +++ b/donutshop_ecommerce/src/components/menu/MenuItem.js @@ -8,6 +8,7 @@ const MenuItem = () => {
menu-donut

Chocolate Beurre noisette Walnut Cream

+

Desciption

$5.99