Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

3.0 KB
Raw Blame
import { Component, PencilSquare, Trash } from "@medusajs/icons";
import { Container, Heading } from "@medusajs/ui";
import { useTranslation } from "react-i18next";

import { ActionMenu } from "../../../../components/common/action-menu";
import { SectionRow } from "../../../../components/common/section";
import { useDeleteOfferAction } from "../../common/hooks/use-delete-offer-action";
import { OfferDetail } from "../../common/types";

type Props = { offer: OfferDetail };

const formatDate = (value?: string | null) => {
  if (!value) return "-";
  try {
    return new Intl.DateTimeFormat(undefined, {
      dateStyle: "medium",
      timeStyle: "short",
    }).format(new Date(value));
  } catch {
    return "-";
  }
};

export const OfferGeneralSection = ({ offer }: Props) => {
  const { t } = useTranslation();
  const handleDelete = useDeleteOfferAction({
    id: offer.id,
    sku: offer.sku ?? "",
    redirectOnSuccess: true,
  });

  const hasKit = (offer.inventory_item_link?.length ?? 0) > 1;
  const variantTitle = offer.product_variant?.title ?? "";
  return (
    <Container
      className="divide-y p-0"
      data-testid="offer-detail-general-section"
    >
      <div className="flex items-center justify-between px-6 py-4">
        <div className="flex items-center gap-2">
          <Heading title={variantTitle}>{variantTitle}</Heading>
          {hasKit && (
            <span className="text-ui-fg-muted font-normal">
              <Component />
            </span>
          )}
        </div>
        <ActionMenu
          groups={[
            {
              actions: [
                {
                  icon: <PencilSquare />,
                  permission: "offers:edit",
                  label: t("actions.edit"),
                  to: "edit",
                },
              ],
            },
            {
              actions: [
                {
                  icon: <Trash />,
                  permission: "offers:manage",
                  label: t("actions.delete"),
                  onClick: handleDelete,
                },
              ],
            },
          ]}
        />
      </div>
      <SectionRow title={t("offers.fields.sku")} value={offer.sku ?? "-"} />
      <SectionRow title={t("offers.fields.ean")} value={offer.ean ?? "-"} />
      <SectionRow title={t("offers.fields.upc")} value={offer.upc ?? "-"} />
      <SectionRow
        title={t("offers.fields.leadtime")}
        value={
          offer.leadtime_to_ship === null || offer.leadtime_to_ship === undefined
            ? t("offers.fields.storeDefault")
            : t("store.leadtime.days", { count: offer.leadtime_to_ship })
        }
      />
      <SectionRow
        title={t("shippingProfile.domain")}
        value={offer.shipping_profile?.name ?? "-"}
      />
      <SectionRow
        title={t("fields.createdAt")}
        value={formatDate(offer.created_at)}
      />
      <SectionRow
        title={t("fields.updatedAt")}
        value={formatDate(offer.updated_at)}
      />
    </Container>
  );
};