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.

2.6 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 { error, notFound, success } from "@carbon/auth";
import { requirePermissions } from "@carbon/auth/auth.server";
import { flash } from "@carbon/auth/session.server";
import { redirect } from "@carbon/utils";
import { useLingui } from "@lingui/react/macro";
import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";
import { useLoaderData, useNavigate, useParams } from "react-router";
import { ConfirmDelete } from "~/components/Modals";
import {
  deleteLocation,
  getLocation
} from "~/modules/resources/resources.service";
import { path } from "~/utils/path";

export async function loader({ request, params }: LoaderFunctionArgs) {
  const { client } = await requirePermissions(request, {
    view: "resources",
    role: "employee"
  });

  const { locationId } = params;
  if (!locationId) throw notFound("locationId not found");

  const location = await getLocation(client, locationId);
  if (location.error) {
    throw redirect(
      path.to.locations,
      await flash(request, error(location.error, "Failed to get location"))
    );
  }

  return {
    location: location.data
  };
}

export async function action({ request, params }: ActionFunctionArgs) {
  const { client } = await requirePermissions(request, {
    delete: "resources"
  });

  const { locationId } = params;
  if (!locationId) {
    throw redirect(
      path.to.locations,
      await flash(request, error(params, "Failed to get location id"))
    );
  }

  const { error: deleteLocationError } = await deleteLocation(
    client,
    locationId
  );
  if (deleteLocationError) {
    throw redirect(
      path.to.locations,
      await flash(
        request,
        error(deleteLocationError, "Failed to delete location")
      )
    );
  }

  throw redirect(
    path.to.locations,
    await flash(request, success("Successfully deleted location"))
  );
}

export default function DeleteLocationRoute() {
  const { locationId } = useParams();
  const { location } = useLoaderData<typeof loader>();
  const navigate = useNavigate();
  const { t } = useLingui();

  if (!location) return null;
  if (!locationId) throw new Error("locationId is not found");

  const onCancel = () => navigate(path.to.locations);
  const name = location.name;
  return (
    <ConfirmDelete
      action={path.to.deleteLocation(locationId)}
      name={name}
      text={t`Are you sure you want to delete the location: ${name}? This cannot be undone.`}
      onCancel={onCancel}
    />
  );
}