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.

1.9 KB
Raw Blame
import { Children, ReactNode } from "react";
import { useLoaderData, useParams } from "react-router-dom";

import { SingleColumnPageSkeleton } from "@components/common/skeleton";
import { SingleColumnPage } from "@components/layout/pages";
import {
  PermissionGuard,
  useLinkQuery,
  WidgetZone,
} from "@mercurjs/dashboard-shared";
import { useCustomerGroup } from "@hooks/api/customer-groups";

import { CustomerGroupCustomerSection } from "./components/customer-group-customer-section";
import { CustomerGroupGeneralSection } from "./components/customer-group-general-section";
import { CUSTOMER_GROUP_DETAIL_FIELDS } from "./constants";
import { customerGroupLoader } from "./loader";

const Root = ({ children }: { children?: ReactNode }) => {
  const initialData = useLoaderData() as Awaited<
    ReturnType<typeof customerGroupLoader>
  >;

  const { id } = useParams();
  const query = useLinkQuery("customer_group", CUSTOMER_GROUP_DETAIL_FIELDS);
  const { customer_group, isLoading, isError, error } = useCustomerGroup(
    id!,
    query,
    { initialData },
  );

  if (isLoading || !customer_group) {
    return <SingleColumnPageSkeleton sections={2} showJSON />;
  }

  if (isError) {
    throw error;
  }

  return Children.count(children) > 0 ? (
    <SingleColumnPage showJSON data={customer_group} hasOutlet>
      {children}
    </SingleColumnPage>
  ) : (
    <SingleColumnPage showJSON data={customer_group} hasOutlet>
      <WidgetZone id="customer-groups.detail.main" data={customer_group}>
        <CustomerGroupGeneralSection group={customer_group} />
        <PermissionGuard permission="customers:view">
          <CustomerGroupCustomerSection group={customer_group} />
        </PermissionGuard>
      </WidgetZone>
    </SingleColumnPage>
  );
};

export const CustomerGroupDetailPage = Object.assign(Root, {
  GeneralSection: CustomerGroupGeneralSection,
  CustomerSection: CustomerGroupCustomerSection,
});