-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added order and payment functionality to user dashboard
- Loading branch information
Showing
13 changed files
with
1,568 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { NextResponse } from "next/server"; | ||
import { PrismaClient, Order } from "@prisma/client"; | ||
const prisma = new PrismaClient(); | ||
|
||
export const PATCH = async ( | ||
req: Request, | ||
{ params }: { params: { id: string } } | ||
) => { | ||
const body: Order = await req.json(); | ||
const order = await prisma.order.update({ | ||
where: { | ||
id: Number(params.id), | ||
}, | ||
data: { | ||
isPaid: body.isPaid, | ||
}, | ||
}); | ||
return NextResponse.json(order, { status: 200 }); | ||
}; | ||
|
||
export const DELETE = async ( | ||
req: Request, | ||
{ params }: { params: { id: string } } | ||
) => { | ||
const recipe = await prisma.order.delete({ | ||
where: { | ||
id: Number(params.id), | ||
}, | ||
}); | ||
return NextResponse.json(recipe, { status: 200 }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import prisma from "@/lib/prisma"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export async function GET(req: Request) { | ||
const recipes = await prisma.order.findFirst({ | ||
select: { | ||
id: true, | ||
totalPrice: true, | ||
foodId: true, | ||
userId: true, | ||
isPaid: true, | ||
}, | ||
take: -1, | ||
}); | ||
|
||
return NextResponse.json(recipes); | ||
} | ||
|
||
export async function POST(req: Request) { | ||
const { totalPrice, foodId, userId, isPaid } = await req.json(); | ||
|
||
console.log( | ||
"totalPrice: ", | ||
totalPrice, | ||
"foorID: ", | ||
foodId, | ||
"userId: ", | ||
userId, | ||
"isPaid: ", | ||
isPaid | ||
); | ||
|
||
const order = await prisma.order.create({ | ||
data: { | ||
totalPrice, | ||
foodId, | ||
userId, | ||
isPaid, | ||
}, | ||
}); | ||
|
||
return NextResponse.json(order); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
"use client"; | ||
import { SyntheticEvent, useEffect, useState } from "react"; | ||
import { CheckCircle2 } from "lucide-react"; | ||
import axios from "axios"; | ||
import { Prisma } from "@prisma/client"; | ||
import toast from "react-hot-toast"; | ||
import { useSession } from "next-auth/react"; | ||
import { fetchData } from "next-auth/client/_utils"; | ||
|
||
type Order = Prisma.OrderGetPayload<{}>; | ||
type Recipe = Prisma.RecipeGetPayload<{}>; | ||
|
||
const AddOrder = ({ | ||
price, | ||
name, | ||
foodId, | ||
}: { | ||
price: number; | ||
name: string; | ||
foodId: number; | ||
}) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [orders, setOrders] = useState<Order | null>(null); | ||
|
||
const fetcher = (url: string) => axios.get(url).then((res) => res.data); | ||
|
||
const { data: session } = useSession(); | ||
|
||
const [newOrder, setNewOrder] = useState({ | ||
userId: session?.user.id, | ||
foodId: foodId, | ||
totalPrice: Number(price), | ||
isPaid: false, | ||
}); | ||
|
||
const [paid, setPaid] = useState(false); | ||
|
||
const createOrder = async () => { | ||
await axios | ||
.post("/api/order", { | ||
userId: newOrder.userId, | ||
foodId: newOrder.foodId, | ||
totalPrice: newOrder.totalPrice, | ||
isPaid: newOrder.isPaid, | ||
}) | ||
.then(() => toast.success("Creating order...")) | ||
.catch(() => toast.error("Something went wrong!")); | ||
|
||
setIsOpen(!isOpen); | ||
|
||
const fetchData = async () => { | ||
try { | ||
const [orderResponse] = await Promise.all([fetcher("/api/order")]); | ||
setOrders(orderResponse); | ||
} catch (error) { | ||
console.error("Error:", error); | ||
} | ||
}; | ||
|
||
fetchData(); | ||
}; | ||
|
||
const updatePayment = async (e: SyntheticEvent) => { | ||
e.preventDefault(); | ||
|
||
!paid | ||
? toast.error("Payment incomplete") | ||
: await axios | ||
.patch(`/api/order/${orders?.id}`, { isPaid: true }) | ||
.then(() => toast.success("Order success")) | ||
.then(() => setIsOpen(!isOpen)); | ||
}; | ||
|
||
const deleteOrder = async () => { | ||
await axios | ||
.delete(`/api/order/${orders?.id}`) | ||
.then(() => toast.error("Order cancelled")); | ||
setIsOpen(!isOpen); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button onClick={createOrder} className="btn btn-primary"> | ||
Order Now | ||
</button> | ||
<div className={isOpen ? "modal modal-open" : "modal"}> | ||
<div className="modal-box "> | ||
<div className="text-black"> | ||
<p className="text-2xl pb-4">Buy {name}</p> | ||
<div className="flex flex-row justify-between"> | ||
<p className="text-lg"> | ||
Make payment of ${price.toFixed(2)} to 082111442634 | ||
</p> | ||
<button onClick={() => setPaid(!paid)}> | ||
<CheckCircle2 color={!paid ? "red" : "green"} /> | ||
</button> | ||
</div> | ||
</div> | ||
<div className="modal-action"> | ||
<div className="btn" onClick={deleteOrder}> | ||
Cancel | ||
</div> | ||
<div className="btn btn-primary" onClick={updatePayment}> | ||
Confirm | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default AddOrder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
5b1ac44
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
pantry-pilot-2 – ./
pantry-pilot-2-git-main-wtanardi.vercel.app
pantry-pilot-2-wtanardi.vercel.app
pantry-pilot-2.vercel.app