diff --git a/donutshop_ecommerce/src/app/api/categories/route.js b/donutshop_ecommerce/src/app/api/categories/route.js new file mode 100644 index 0000000..a8a93a9 --- /dev/null +++ b/donutshop_ecommerce/src/app/api/categories/route.js @@ -0,0 +1,21 @@ +import { Category } from "../models/Category"; + +export async function POST(req){ + const {name} = await req.json(); + const categoryDoc = await Category.create({name}) + + return Response.json(categoryDoc); +} + +export async function PUT(req){ + const {_id, name} = await req.json(); + await Category.updateOne({_id}, {name}); + + return Response.json(true); +} + +export async function GET(req){ + return Response.json( + await Category.find() + ) +} \ No newline at end of file diff --git a/donutshop_ecommerce/src/app/api/models/Category.js b/donutshop_ecommerce/src/app/api/models/Category.js new file mode 100644 index 0000000..1818c14 --- /dev/null +++ b/donutshop_ecommerce/src/app/api/models/Category.js @@ -0,0 +1,11 @@ +import { Schema, model, models } from "mongoose"; + +const CategorySchema = new Schema({ + name: { + type: String, + required: true + }, + +}, {timestamps: true}) + +export const Category = models?.Category || model('Category', CategorySchema); \ 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 456c7ff..4fc2fb4 100644 --- a/donutshop_ecommerce/src/app/categories/page.js +++ b/donutshop_ecommerce/src/app/categories/page.js @@ -1,24 +1,96 @@ "use client" -import React from 'react' +import React, { useEffect, useState } from 'react' import UserTab from '../../components/layout/UserTab' import useProfile from '../../components/UseProfile' +import toast from 'react-hot-toast' const CategoriesPage = () => { const {loading:profileLoading, data:profileData} = useProfile(); + const [categoryName, setCategoryName] = useState(''); + const [categories, setCategories] = useState([]); + const [editCategory, setEditCategory] = useState(null); + + //create new category && toast promis functionlity + useEffect(() => { + fetchCategories(); + }, []); + + function fetchCategories(){ + fetch('api/categories').then(res => { + res.json().then(categories => { + setCategories(categories) + }) + }) + } + + + async function handleCategorySubmit(ev){ + ev.preventDefault(); + const creatCategoryPromise = new Promise(async (resolve, reject) => { + const data = {name:categoryName}; //edit category functionality + if(editCategory){ + data._id = editCategory._id + } + const response = await fetch('api/categories', { + method: editCategory ? 'PUT' : 'POST', + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify(data), + }); + setCategoryName(''); + fetchCategories(); + setEditCategory(null) + if(response.ok) + resolve(); + else + reject(); + }); + await toast.promise(creatCategoryPromise, { + 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', + }) + } + + + //loading & admin if(profileLoading){ return
Loading...
} - if(!profileData.admin){ returnPlease login as an admin
} - + return ( -Loading...
+ } + if(!data.admin){ + returnPlease login as an admin
+ } + + return ( +