import clsx from "clsx"; import { useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { PropertyType } from "~/application/enums/entities/PropertyType"; import ButtonTertiary from "~/components/ui/buttons/ButtonTertiary"; import FormGroup from "~/components/ui/forms/FormGroup"; import PencilAltIcon from "~/components/ui/icons/PencilAltIcon"; import ViewListIcon from "~/components/ui/icons/ViewListIcon"; import InputCheckboxWithDescription from "~/components/ui/input/InputCheckboxWithDescription"; import InputNumber from "~/components/ui/input/InputNumber"; import InputText from "~/components/ui/input/InputText"; import { PropertyWithDetails, EntitySimple } from "~/utils/db/entities/entities.db.server"; import StringUtils from "~/utils/shared/StringUtils"; import PropertyTypeSelector from "./PropertyTypeSelector"; import PropertyOptionsForm, { OptionValue, RefPropertyOptionsForm } from "./PropertyOptionsForm"; import PropertyAttributeHelper from "~/utils/helpers/PropertyAttributeHelper"; import PropertyAttribute from "./PropertyAttribute"; import PropertySubtypeSelector from "./PropertySubtypeSelector"; import PropertyFormulaSelector from "./PropertyFormulaSelector"; import { FormulaDto } from "~/modules/formulas/dtos/FormulaDto"; import { Link } from "react-router"; interface Props { item?: PropertyWithDetails; properties: PropertyWithDetails[]; entities: EntitySimple[]; formulas: FormulaDto[]; formulaListPath?: string; formulaDetailsPath?: (formulaId: string) => string; } export default function PropertyForm({ item, properties, formulas, formulaListPath, formulaDetailsPath }: Props) { const { t } = useTranslation(); const selectOptionsForm = useRef(null); const [showAdvancedOptions, setShowAdvancedOptions] = useState(true); const [order, setOrder] = useState(item?.order ?? Math.max(...properties.map((o) => o.order)) + 1); const [name, setName] = useState(item?.name ?? ""); const [title, setTitle] = useState(item?.title ?? ""); const [type, setType] = useState(item?.type ?? PropertyType.TEXT); const [subtype, setSubtype] = useState(item?.subtype ?? ""); const [formulaId, setFormulaId] = useState(item?.formulaId ?? undefined); const [options, setOptions] = useState( item?.options?.map((o) => { return { id: o.id, order: o.order, value: o.value, name: o.name, color: o.color, }; }) ?? [] ); const [isRequired, setIsRequired] = useState(item?.isRequired ?? true); const [showInCreate, setShowInCreate] = useState(item?.showInCreate ?? true); const [isHidden, setIsHidden] = useState(item?.isHidden ?? false); const [isDisplay, setIsDisplay] = useState(item?.isDisplay ?? properties.filter((f) => !f.isDefault).length === 0); const [isReadOnly, setIsReadOnly] = useState(item?.isReadOnly ?? false); const [canUpdate, setCanUpdate] = useState(item ? item.canUpdate : true); const [attributes, setAttributes] = useState<{ name: string; value: string | undefined }[]>(item?.attributes ?? []); // const [formula, setFormula] = useState(); const [titleEnabled, setTitleEnabled] = useState(false); useEffect(() => { if (!item) { if (title.includes(".")) { const keys = title.split("."); setName(StringUtils.toCamelCase(keys[keys.length - 1].toLowerCase())); } else { setName(StringUtils.toCamelCase(title.toLowerCase())); } } }, [item, title, type]); useEffect(() => { const formula = formulas.find((f) => f.id === formulaId); if (!item && formula) { setTitle(formula.name); setName(StringUtils.toCamelCase(formula.name.toLowerCase())); } }, [item, formulaId, formulas]); useEffect(() => { setTitleEnabled(true); if (!item) { if (type === PropertyType.TEXT) { setSubtype("singleLine"); } else if (type === PropertyType.SELECT) { setSubtype("dropdown"); } else if (type === PropertyType.MULTI_SELECT) { setSubtype("combobox"); } else { // setSubtype(""); } } else { if (type === PropertyType.TEXT) { if (!subtype || !["singleLine", "email", "phone", "url"].includes(subtype)) { setSubtype("singleLine"); } } else if (type === PropertyType.SELECT) { if (!subtype || !["dropdown", "radioGroupCards"].includes(subtype)) { setSubtype("dropdown"); } } else if (type === PropertyType.MULTI_SELECT) { if (!subtype || !["combobox", "checkboxCards"].includes(subtype)) { setSubtype("combobox"); } } else { // setSubtype(""); } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [item, type]); useEffect(() => { if (type === PropertyType.FORMULA) { setShowAdvancedOptions(false); setIsRequired(false); setIsReadOnly(true); setShowInCreate(false); } }, [type]); return ( <>
setType(e)} />
{type === PropertyType.TEXT && (
setSubtype(e)} />
)} {type === PropertyType.SELECT && (
setSubtype(e)} />
)} {type === PropertyType.MULTI_SELECT && (
setSubtype(e)} />
)} {type === PropertyType.FORMULA && (
{item?.formulaId && formulaDetailsPath ? ( {t("models.property.goToFormula")} ) : formulaListPath ? ( {t("models.property.viewFormulas")} ) : null}
setFormulaId(e)} />
)} {titleEnabled && ( setTitle(e)} disabled={!titleEnabled} required withTranslation placeholder={t("models.property.form.placeholders.title")} autoFocus /> )} setName(e)} required placeholder={t("models.property.form.placeholders.name")} pattern="[a-z]+((\d)|([A-Z0-9][a-z0-9]+))*([A-Z])?" hint={{t("models.property.form.hints.camelCase")}} />
{/* {type === PropertyType.FORMULA && (
setFormula(e.toString())} disabled={!titleEnabled} required />
)} */} {[PropertyType.SELECT, PropertyType.MULTI_SELECT].includes(type) && (
{options.map((option) => { return ; })} f.value).join(", ")} />
)}
setShowAdvancedOptions(!showAdvancedOptions)}> {!showAdvancedOptions ? t("models.property.form.showAdvancedOptions") : t("models.property.form.hideAdvancedOptions")}
{/*
{t("models.propertyAttribute.plural")}
*/} {attributes.map((attribute) => { return ; })} {PropertyAttributeHelper.getAttributesByType(type, attributes).map((item) => { return ( f.name === item)?.value ?? undefined} setValue={(e) => { const value = { name: item, value: e?.toString() }; const found = attributes.find((f) => f.name === item); if (found) { setAttributes([...attributes.filter((f) => f.name !== item), value]); } else { setAttributes([...attributes, value]); } }} /> ); })}
setOptions(e)} /> ); }