Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

3.8 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { ValidatedForm } from "@carbon/form";
import { useAction } from "@carbon/query";
import {
  HStack,
  ModalDrawer,
  ModalDrawerBody,
  ModalDrawerContent,
  ModalDrawerFooter,
  ModalDrawerHeader,
  ModalDrawerProvider,
  ModalDrawerTitle,
  toast,
  VStack
} from "@carbon/react";
import { Trans, useLingui } from "@lingui/react/macro";
import type { z } from "zod";
import {
  Account,
  CustomFormFields,
  Hidden,
  Input,
  Select,
  Submit
} from "~/components/Form";
import { usePermissions } from "~/hooks";
import {
  shippingCarrierType,
  shippingMethodValidator
} from "~/modules/inventory/inventory.models";
import { path } from "~/utils/path";

type ShippingMethodFormProps = {
  initialValues: z.infer<typeof shippingMethodValidator>;
  type?: "modal" | "drawer";
  open?: boolean;
  onClose: (data?: { id: string; name: string }) => void;
};

const ShippingMethodForm = ({
  initialValues,
  open = true,
  type = "drawer",
  onClose
}: ShippingMethodFormProps) => {
  const permissions = usePermissions();
  const { t } = useLingui();
  // A modal closes on the new record; an error is toasted and the form stays.
  const fetcher = useAction({
    onSuccess: () => {
      if (type !== "modal") return;
      onClose?.();
      toast.success(t`Created shipping method`);
    }
  });

  const isEditing = initialValues.id !== undefined;
  const isDisabled = isEditing
    ? !permissions.can("update", "inventory")
    : !permissions.can("create", "inventory");

  const shippingCarrierOptions = shippingCarrierType.map((v) => ({
    label: v,
    value: v
  }));

  return (
    <ModalDrawerProvider type={type}>
      <ModalDrawer
        open={open}
        onOpenChange={(open) => {
          if (!open) onClose?.();
        }}
      >
        <ModalDrawerContent>
          <ValidatedForm
            validator={shippingMethodValidator}
            method="post"
            action={
              isEditing
                ? path.to.shippingMethod(initialValues.id!)
                : path.to.newShippingMethod
            }
            defaultValues={initialValues}
            fetcher={fetcher}
            className="flex flex-col h-full"
          >
            <ModalDrawerHeader>
              <ModalDrawerTitle>
                {isEditing ? t`Edit Shipping Method` : t`New Shipping Method`}
              </ModalDrawerTitle>
            </ModalDrawerHeader>
            <ModalDrawerBody>
              <Hidden name="id" />
              <Hidden name="type" value={type} />
              <VStack spacing={4}>
                <Input name="name" label={t`Name`} />
                <Select
                  name="carrier"
                  label={t`Carrier`}
                  options={shippingCarrierOptions}
                />
                <Account
                  classes={["Expense"]}
                  name="carrierAccountId"
                  label={t`Carrier Account`}
                  termId="shipping-method-carrier-account"
                />
                <Input
                  name="trackingUrl"
                  label={t`Tracking URL`}
                  termId="shipping-method-tracking-url"
                  prefix="https://"
                />
                <CustomFormFields table="shippingMethod" />
              </VStack>
            </ModalDrawerBody>
            <ModalDrawerFooter>
              <HStack>
                <Submit isDisabled={isDisabled}>
                  <Trans>Save</Trans>
                </Submit>
              </HStack>
            </ModalDrawerFooter>
          </ValidatedForm>
        </ModalDrawerContent>
      </ModalDrawer>
    </ModalDrawerProvider>
  );
};

export default ShippingMethodForm;