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.

Parts Sequence Shortcut (#39)

Brad Barbin8212c7610398
diff --git a/apps/carbon/app/components/Form/InputControlled.tsx b/apps/carbon/app/components/Form/InputControlled.tsx
new file mode 100644
index 0000000000..2ef25cd1f1
--- /dev/null
+++ b/apps/carbon/app/components/Form/InputControlled.tsx
@@ -0,0 +1,84 @@
+import type { InputProps } from "@chakra-ui/react";
+import {
+ FormControl,
+ FormErrorMessage,
+ FormHelperText,
+ FormLabel,
+ Input as ChakraInput,
+ InputGroup,
+ InputLeftAddon,
+ InputRightAddon,
+} from "@chakra-ui/react";
+import type { ChangeEvent } from "react";
+import { forwardRef, useEffect } from "react";
+import { useControlField, useField } from "remix-validated-form";
+
+type FormInputControlledProps = Omit<InputProps, "value" | "onChange"> & {
+ name: string;
+ label?: string;
+ isRequired?: boolean;
+ helperText?: string;
+ prefix?: string;
+ suffix?: string;
+ value: string;
+ onChange?: (newValue: string) => void;
+};
+
+const InputControlled = forwardRef<HTMLInputElement, FormInputControlledProps>(
+ (
+ {
+ name,
+ label,
+ isRequired,
+ helperText,
+ prefix,
+ suffix,
+ value,
+ onChange,
+ ...rest
+ },
+ ref
+ ) => {
+ const { getInputProps, error } = useField(name);
+ const [controlValue, setControlValue] = useControlField<string>(name);
+
+ useEffect(() => {
+ console.log(`setting controlled value to ${value}`);
+ setControlValue(value);
+ }, [setControlValue, value]);
+
+ const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
+ setControlValue(e.target.value);
+ if (onChange && typeof onChange === "function") {
+ onChange(e.target.value);
+ }
+ };
+
+ return (
+ <FormControl isInvalid={!!error} isRequired={isRequired}>
+ {label && <FormLabel htmlFor={name}>{label}</FormLabel>}
+ <InputGroup>
+ {prefix && <InputLeftAddon children={prefix} />}
+ <ChakraInput
+ ref={ref}
+ {...getInputProps({
+ id: name,
+ ...rest,
+ // @ts-ignore
+ value: controlValue,
+ })}
+ onChange={handleChange}
+ value={controlValue}
+ />
+ {suffix && <InputRightAddon children={suffix} />}
+ </InputGroup>
+ {helperText && <FormHelperText>{helperText}</FormHelperText>}
+ {error && <FormErrorMessage>{error}</FormErrorMessage>}
+ </FormControl>
+ );
+ }
+);
+
+InputControlled.displayName = "InputControlled";
+
+export default InputControlled;
diff --git a/apps/carbon/app/components/Form/index.ts b/apps/carbon/app/components/Form/index.ts
index 7c887d297d..014bb9e27f 100644
--- a/apps/carbon/app/components/Form/index.ts
+++ b/apps/carbon/app/components/Form/index.ts
@@ -8,16 +8,17 @@ import Boolean from "./Boolean";
import Color from "./Color";
import CreatableMultiSelect from "./CreatableMultiSelect";
import CreatableSelect from "./CreatableSelect";
+import Currency from "./Currency";
import Customer from "./Customer";
import CustomerContact from "./CustomerContact";
import CustomerLocation from "./CustomerLocation";
-import Currency from "./Currency";
import DatePicker from "./DatePicker";
import Department from "./Department";
import Employee from "./Employee";
import Employees from "./Employees";
import Hidden from "./Hidden";
import Input from "./Input";
+import InputControlled from "./InputControlled";
import Location from "./Location";
import Number from "./Number";
import Part from "./Part";
@@ -59,6 +60,7 @@ export {
Employees,
Hidden,
Input,
+ InputControlled,
Location,
Number,
Part,
diff --git a/apps/carbon/app/modules/parts/ui/Part/PartForm/PartForm.tsx b/apps/carbon/app/modules/parts/ui/Part/PartForm/PartForm.tsx
index a2b3a249b8..3200d39d3a 100644
--- a/apps/carbon/app/modules/parts/ui/Part/PartForm/PartForm.tsx
+++ b/apps/carbon/app/modules/parts/ui/Part/PartForm/PartForm.tsx
@@ -8,9 +8,18 @@ import {
Text,
VStack,
} from "@chakra-ui/react";
+import { useState } from "react";
import { ValidatedForm } from "remix-validated-form";
-import { Boolean, Input, Select, Submit, TextArea } from "~/components/Form";
+import {
+ Boolean,
+ Input,
+ InputControlled,
+ Select,
+ Submit,
+ TextArea,
+} from "~/components/Form";
import { usePermissions } from "~/hooks";
+import { useSupabase } from "~/lib/supabase";
import type {
PartGroupListItem,
PartReplenishmentSystem,
@@ -39,6 +48,51 @@ type PartFormProps = {
unitOfMeasures: UnitOfMeasureListItem[];
};
+const useNextPartIdShortcut = () => {
+ const { supabase } = useSupabase();
+ const [loading, setLoading] = useState<boolean>(false);
+ const [partId, setPartId] = useState<string>("");
+
+ const onPartIdChange = async (newPartId: string) => {
+ if (newPartId.endsWith("...") && supabase) {
+ setLoading(true);
+
+ const prefix = newPartId.slice(0, -3);
+ try {
+ const { data } = await supabase
+ ?.from("part")
+ .select("id")
+ .ilike("id", `${prefix}%`)
+ .order("id", { ascending: false })
+ .limit(1)
+ .maybeSingle();
+ if (data?.id) {
+ const sequence = data.id.slice(prefix.length);
+ const currentSequence = parseInt(sequence);
+ const nextSequence = currentSequence + 1;
+ const nextId = `${prefix}${nextSequence
+ .toString()
+ .padStart(
+ sequence.length -
+ (data.id.split(`${currentSequence}`)?.[1].length ?? 0),
+ "0"
+ )}`;
+ setPartId(nextId);
+ } else {
+ setPartId(`${prefix}${(1).toString().padStart(9, "0")}`);
+ }
+ } catch {
+ } finally {
+ setLoading(false);
+ }
+ } else {
+ setPartId(newPartId);
+ }
+ };
+
+ return { partId, onPartIdChange, loading };
+};
+
const PartForm = ({
initialValues,
partGroups,
@@ -46,6 +100,7 @@ const PartForm = ({
partReplenishmentSystems,
unitOfMeasures,
}: PartFormProps) => {
+ const { partId, onPartIdChange, loading } = useNextPartIdShortcut();
const permissions = usePermissions();
const isEditing = initialValues.id !== undefined;
@@ -97,7 +152,18 @@ const PartForm = ({
w="full"
>
<VStack alignItems="start" spacing={2} w="full">
- <Input name="id" label="Part ID" isReadOnly={isEditing} />
+ {isEditing ? (
+ <Input name="id" label="Part ID" isReadOnly />
+ ) : (
+ <InputControlled
+ name="id"
+ label="Part ID"
+ value={partId}
+ onChange={onPartIdChange}
+ isDisabled={loading}
+ />
+ )}
+
<Input name="name" label="Name" />
<TextArea name="description" label="Description" />
</VStack>
diff --git a/apps/carbon/app/root.tsx b/apps/carbon/app/root.tsx
index f3adb51f9e..2a27f3258c 100644
--- a/apps/carbon/app/root.tsx
+++ b/apps/carbon/app/root.tsx
@@ -77,7 +77,7 @@ function Document({
}}
/>
<Scripts />
- <LiveReload port={8002} />
+ {process.env.NODE_ENV === "development" && <LiveReload />}
</body>
</html>
);
diff --git a/apps/carbon/remix.config.js b/apps/carbon/remix.config.js
index 8e676dca3a..6f1d0cdf7f 100644
--- a/apps/carbon/remix.config.js
+++ b/apps/carbon/remix.config.js
@@ -5,7 +5,7 @@ const { flatRoutes } = require("remix-flat-routes");
module.exports = {
future: {
- v2_dev: false,
+ v2_dev: true,
v2_errorBoundary: true,
v2_headers: false,
v2_meta: true,