1 import { defineWidgetConfig } from "@medusajs/admin-sdk";
2 import { Gift, TriangleRightMini } from "@medusajs/icons";
3 import { Container, StatusBadge } from "@medusajs/ui";
4 import { useParams } from "react-router-dom";
5 import { AdminGiftCard } from "../../types";
6 import { Header } from "../components/header";
7 import { NoRecords } from "../components/no-records";
8 import { SidebarLink } from "../components/sidebar-link";
9 import { useOrder } from "../hooks/api/order";
10 import { formatAmount } from "../utils/format-amount";
11
12 const OrderGiftCardsWidget = () => {
13 const params = useParams();
14
15 const { order } = useOrder(params.id!, {
16 fields: "+*gift_cards",
17 });
18
19 if (!order?.gift_cards?.length) {
20 return;
21 }
22
23 return (
24 <Container className="divide-y p-0">
25 <Header
26 title="Gift Cards"
27 subtitle="Gift cards that have been applied to this order"
28 tooltip={`A credit line “refund” will always attempt to apply the credit to the customers store credit account first. If the customers doesn't have a store credit account, a new one will be created.`}
29 />
30
31 {order?.gift_cards?.length === 0 && (
32 <NoRecords
33 className="border-t"
34 title="No gift cards"
35 message="There are no gift cards on this order"
36 icon={null}
37 />
38 )}
39
40 {order?.gift_cards?.map((giftCard: AdminGiftCard) => {
41 const hasGiftCardExpired =
42 giftCard.expires_at && new Date(giftCard.expires_at) < new Date();
43
44 return (
45 <SidebarLink
46 icon={<Gift />}
47 key={giftCard.id}
48 labelKey={giftCard.code}
49 descriptionKey={formatAmount(
50 (giftCard.value as number) ?? 0,
51 giftCard.currency_code
52 )}
53 to={`/gift-cards/${giftCard.id}`}
54 >
55 <>
56 <StatusBadge
57 color={hasGiftCardExpired ? "orange" : "green"}
58 className="capitalize"
59 >
60 {hasGiftCardExpired ? "Expired" : "Active"}
61 </StatusBadge>
62
63 <TriangleRightMini className="text-ui-fg-muted" />
64 </>
65 </SidebarLink>
66 );
67 })}
68 </Container>
69 );
70 };
71
72 export const config = defineWidgetConfig({
73 zone: "order.details.side.after",
74 id: "medusa:order-gift-cards-widget",
75 });
76
77 export default OrderGiftCardsWidget;