This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
-
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.
- Loading branch information
1 parent
c7e2ad5
commit 92f31e2
Showing
11 changed files
with
297 additions
and
29 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
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,41 @@ | ||
"use client" | ||
|
||
import useGetOrder from "@/features/order/useGetOrder" | ||
import { Separator } from "@radix-ui/react-dropdown-menu" | ||
|
||
import BreadCrumb from "@/components/ui/breadcrumb" | ||
import { Heading } from "@/components/ui/heading" | ||
import { OrderForm } from "@/components/forms/order-form" | ||
import OrderDetailTable from "@/components/tables/order_details/table" | ||
|
||
export default function EditOrder({ | ||
params, | ||
}: Readonly<{ params: { id: string } }>) { | ||
const breadcrumbItems = [ | ||
{ title: "Order", link: "/dashboard/order" }, | ||
{ title: "Edit", link: `/dashboard/order/${params.id}` }, | ||
] | ||
|
||
const { data: GetOrder } = useGetOrder(params.id) | ||
|
||
console.log(GetOrder) | ||
|
||
return ( | ||
<div className="flex-1 space-y-4 p-8"> | ||
<BreadCrumb items={breadcrumbItems} /> | ||
{GetOrder && ( | ||
<OrderForm | ||
initialData={{ | ||
id: GetOrder.id, | ||
orderStatus: GetOrder.orderStatus, | ||
}} | ||
/> | ||
)} | ||
<Separator /> | ||
<div className="flex items-start justify-between"> | ||
<Heading title={`Order detail`} description="Manage order item" /> | ||
</div> | ||
<OrderDetailTable data={GetOrder?.items || []} /> | ||
</div> | ||
) | ||
} |
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,160 @@ | ||
"use client" | ||
|
||
import { FC, useEffect } from "react" | ||
import { useRouter } from "next/navigation" | ||
import { OrderStatus, UpdateOrderRequest } from "@/features/order/order.type" | ||
import useUpdateOrder from "@/features/order/useUpdateOrder" | ||
import { zodResolver } from "@hookform/resolvers/zod" | ||
import { useForm } from "react-hook-form" | ||
import { z } from "zod" | ||
|
||
import { orderSchema } from "@/lib/validations/order" | ||
|
||
import { Button } from "../ui/button" | ||
import { | ||
Form, | ||
FormControl, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
} from "../ui/form" | ||
import { Heading } from "../ui/heading" | ||
import { Input } from "../ui/input" | ||
import { | ||
Select, | ||
SelectContent, | ||
SelectGroup, | ||
SelectItem, | ||
SelectTrigger, | ||
SelectValue, | ||
} from "../ui/select" | ||
import { Separator } from "../ui/separator" | ||
import { useToast } from "../ui/use-toast" | ||
|
||
type OrderFormValues = z.infer<typeof orderSchema> | ||
|
||
type OrderFormProps = { | ||
initialData: UpdateOrderRequest | ||
} | ||
|
||
export const OrderForm: FC<OrderFormProps> = ({ initialData }) => { | ||
const router = useRouter() | ||
const { toast } = useToast() | ||
const { | ||
mutate: updateOrder, | ||
isSuccess: updateOrderSuccess, | ||
isPending: updateOrderPending, | ||
} = useUpdateOrder() | ||
|
||
const isDisabled = updateOrderSuccess || updateOrderPending | ||
|
||
const title = "Edit order" | ||
const description = "Edit an order." | ||
const toastMessage = "Order updated." | ||
const action = "Save changes" | ||
|
||
const defaultValues = initialData | ||
|
||
const form = useForm<OrderFormValues>({ | ||
resolver: zodResolver(orderSchema), | ||
defaultValues, | ||
}) | ||
|
||
const onSubmit = async (data: OrderFormValues) => { | ||
try { | ||
updateOrder(data) | ||
|
||
console.log("Order data: ", data) | ||
} catch (error: any) { | ||
toast({ | ||
variant: "destructive", | ||
title: "Uh oh! Something went wrong.", | ||
description: "There was a problem with your request.", | ||
}) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (updateOrderSuccess) { | ||
toast({ | ||
title: "Success!", | ||
description: toastMessage, | ||
}) | ||
|
||
setTimeout(() => { | ||
router.replace(`/dashboard/order`) | ||
}, 2000) | ||
} | ||
}, [updateOrderSuccess]) | ||
|
||
return ( | ||
<> | ||
<div className="flex items-center justify-between"> | ||
<Heading title={title} description={description} /> | ||
</div> | ||
<Separator /> | ||
<Form {...form}> | ||
<form | ||
onSubmit={form.handleSubmit(onSubmit)} | ||
className="w-full space-y-8" | ||
> | ||
<div className="gap-8 md:grid md:grid-cols-2"> | ||
<FormField | ||
control={form.control} | ||
name="id" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Name</FormLabel> | ||
<FormControl> | ||
<Input readOnly={true} placeholder="Order ID" {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name="orderStatus" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Status</FormLabel> | ||
<Select | ||
onValueChange={field.onChange} | ||
defaultValue={field.value} | ||
> | ||
<FormControl> | ||
<SelectTrigger> | ||
<SelectValue placeholder="Select status" /> | ||
</SelectTrigger> | ||
</FormControl> | ||
<SelectContent> | ||
<SelectGroup> | ||
<SelectItem value={OrderStatus.Pending}> | ||
Pending | ||
</SelectItem> | ||
<SelectItem value={OrderStatus.Shipping}> | ||
Shipping | ||
</SelectItem> | ||
<SelectItem value={OrderStatus.Completed}> | ||
Completed | ||
</SelectItem> | ||
<SelectItem value={OrderStatus.Cancelled}> | ||
Cancelled | ||
</SelectItem> | ||
</SelectGroup> | ||
</SelectContent> | ||
</Select> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
</div> | ||
<Button disabled={isDisabled} className="ml-auto" type="submit"> | ||
{action} | ||
</Button> | ||
</form> | ||
</Form> | ||
</> | ||
) | ||
} |
Empty file.
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,36 @@ | ||
"use client" | ||
|
||
import { OrderItem } from "@/features/order/order.type" | ||
import { ColumnDef } from "@tanstack/react-table" | ||
|
||
export const columns: ColumnDef<OrderItem>[] = [ | ||
{ | ||
accessorKey: "id", | ||
header: "PRODUCT ID", | ||
}, | ||
{ | ||
accessorKey: "price", | ||
header: "PRICE", | ||
cell: (props) => { | ||
const price = props.getValue() as number | ||
return new Intl.NumberFormat("en-US", { | ||
style: "currency", | ||
currency: "USD", | ||
}).format(price) | ||
}, | ||
}, | ||
{ | ||
accessorKey: "quantity", | ||
header: "QUANTITY", | ||
}, | ||
{ | ||
header: "TOTAL", | ||
cell: (props) => { | ||
const { price, quantity } = props.row.original | ||
return new Intl.NumberFormat("en-US", { | ||
style: "currency", | ||
currency: "USD", | ||
}).format(price * quantity) | ||
}, | ||
}, | ||
] |
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,24 @@ | ||
"use client" | ||
|
||
import { OrderItem } from "@/features/order/order.type" | ||
|
||
import FilterTable from "@/components/custom/filter-table" | ||
|
||
import { columns } from "./columns" | ||
|
||
export default function OrderDetailTable({ | ||
data, | ||
}: Readonly<{ | ||
data: OrderItem[] | ||
}>) { | ||
return ( | ||
<FilterTable | ||
searchKey="" | ||
pageNo={0} | ||
columns={columns} | ||
totalRecords={0} | ||
data={data} | ||
pageCount={0} | ||
/> | ||
) | ||
} |
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,7 @@ | ||
import { OrderStatus } from "@/features/order/order.type" | ||
import { z } from "zod" | ||
|
||
export const orderSchema = z.object({ | ||
id: z.string(), | ||
orderStatus: z.nativeEnum(OrderStatus), | ||
}) |