-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from udohjeremiah/dev
Add /orders route.
- Loading branch information
Showing
6 changed files
with
266 additions
and
11 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,30 @@ | ||
-- CreateTable | ||
CREATE TABLE "Order" ( | ||
"id" TEXT NOT NULL, | ||
"storeId" TEXT NOT NULL, | ||
"isPaid" BOOLEAN NOT NULL DEFAULT false, | ||
"phone" TEXT NOT NULL DEFAULT '', | ||
"address" TEXT NOT NULL DEFAULT '', | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "Order_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "OrderItem" ( | ||
"id" TEXT NOT NULL, | ||
"orderId" TEXT NOT NULL, | ||
"productId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "OrderItem_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Order" ADD CONSTRAINT "Order_storeId_fkey" FOREIGN KEY ("storeId") REFERENCES "Store"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "OrderItem" ADD CONSTRAINT "OrderItem_orderId_fkey" FOREIGN KEY ("orderId") REFERENCES "Order"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "OrderItem" ADD CONSTRAINT "OrderItem_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
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,105 @@ | ||
import type { Metadata } from "next"; | ||
|
||
import { redirect } from "next/navigation"; | ||
|
||
import { auth } from "@clerk/nextjs"; | ||
import { format } from "date-fns"; | ||
|
||
import DataTable from "@/components/DataTable"; | ||
import Heading from "@/components/Heading"; | ||
import { OrderColumn, columns } from "@/components/columns/OrderColumns"; | ||
import { Separator } from "@/components/ui/separator"; | ||
|
||
import { cn } from "@/lib/utils"; | ||
import prisma from "@/lib/prisma"; | ||
|
||
interface OrdersPageProps { | ||
params: { storeId: string }; | ||
} | ||
|
||
export async function generateMetadata({ | ||
params, | ||
}: { | ||
params: { storeId: string }; | ||
}): Promise<Metadata> { | ||
const { userId } = auth(); | ||
|
||
if (!userId) { | ||
return {}; | ||
} | ||
|
||
const store = await prisma.store.findUnique({ | ||
where: { id: params.storeId, userId }, | ||
}); | ||
|
||
return { | ||
title: `${store?.name} Store Orders | E-Commerce CMS`, | ||
description: `Manage the orders for your ${store?.name} store.`, | ||
}; | ||
} | ||
|
||
export default async function OrdersPage({ params }: OrdersPageProps) { | ||
const { userId } = auth(); | ||
|
||
if (!userId) { | ||
redirect("/login"); | ||
} | ||
|
||
const store = await prisma.store.findFirst({ | ||
where: { id: params.storeId, userId }, | ||
}); | ||
|
||
if (!store) { | ||
redirect("/"); | ||
} | ||
|
||
const orders = await prisma.order.findMany({ | ||
where: { storeId: store.id }, | ||
include: { | ||
OrderItem: { | ||
include: { Product: true }, | ||
}, | ||
}, | ||
orderBy: { createdAt: "desc" }, | ||
}); | ||
|
||
const formattedOrders: OrderColumn[] = orders.map((order) => ({ | ||
id: order.id, | ||
isPaid: order.isPaid, | ||
phone: order.phone, | ||
address: order.address, | ||
products: order.OrderItem.map((orderItem) => orderItem.Product.name).join( | ||
", ", | ||
), | ||
totalPrice: new Intl.NumberFormat("en-US", { | ||
style: "currency", | ||
currency: "USD", | ||
}).format( | ||
order.OrderItem.reduce( | ||
(total, orderItem) => total + Number(orderItem.Product.price), | ||
0, | ||
), | ||
), | ||
createdAt: format(order.createdAt, "MMMM do, yyyy"), | ||
})); | ||
|
||
return ( | ||
<main | ||
className={cn( | ||
"container flex flex-1 flex-col gap-4 py-4", | ||
"md:gap-8 md:py-8", | ||
)} | ||
> | ||
<Heading | ||
title={`Orders (${orders.length})`} | ||
description={`Manage the orders for your ${store.name} store.`} | ||
/> | ||
<Separator /> | ||
<DataTable | ||
columns={columns} | ||
filterColumn="products" | ||
data={formattedOrders} | ||
/> | ||
</main> | ||
); | ||
} |
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,93 @@ | ||
"use client"; | ||
|
||
import { ColumnDef } from "@tanstack/react-table"; | ||
import { ArrowUpDown } from "lucide-react"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
|
||
export type OrderColumn = { | ||
id: string; | ||
isPaid: boolean; | ||
phone: string; | ||
address: string; | ||
products: string; | ||
totalPrice: string; | ||
createdAt: string; | ||
}; | ||
|
||
export const columns: ColumnDef<OrderColumn>[] = [ | ||
{ | ||
accessorKey: "products", | ||
header: ({ column }) => { | ||
return ( | ||
<Button | ||
variant="ghost" | ||
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} | ||
> | ||
Products | ||
<ArrowUpDown className="ml-2 h-4 w-4" /> | ||
</Button> | ||
); | ||
}, | ||
}, | ||
{ | ||
accessorKey: "phone", | ||
header: ({ column }) => { | ||
return ( | ||
<Button | ||
variant="ghost" | ||
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} | ||
> | ||
Phone | ||
<ArrowUpDown className="ml-2 h-4 w-4" /> | ||
</Button> | ||
); | ||
}, | ||
}, | ||
{ | ||
accessorKey: "address", | ||
header: ({ column }) => { | ||
return ( | ||
<Button | ||
variant="ghost" | ||
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} | ||
> | ||
Address | ||
<ArrowUpDown className="ml-2 h-4 w-4" /> | ||
</Button> | ||
); | ||
}, | ||
}, | ||
{ | ||
accessorKey: "totalPrice", | ||
header: ({ column }) => { | ||
return ( | ||
<Button | ||
variant="ghost" | ||
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} | ||
> | ||
Total Price | ||
<ArrowUpDown className="ml-2 h-4 w-4" /> | ||
</Button> | ||
); | ||
}, | ||
}, | ||
{ | ||
accessorKey: "isPaid", | ||
header: "Paid", | ||
}, | ||
{ | ||
accessorKey: "createdAt", | ||
header: ({ column }) => { | ||
return ( | ||
<Button | ||
variant="ghost" | ||
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")} | ||
> | ||
Date | ||
<ArrowUpDown className="ml-2 h-4 w-4" /> | ||
</Button> | ||
); | ||
}, | ||
}, | ||
]; |
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