import { Edit, useForm, useSelect } from "@refinedev/antd"; import { HttpError, IResourceComponentsProps, useTranslate } from "@refinedev/core"; import { Alert, ColorPicker, DatePicker, Form, Input, InputNumber, message, Radio, Select, Typography } from "antd"; import TextArea from "antd/es/input/TextArea"; import dayjs from "dayjs"; import React, { useEffect, useState } from "react"; import { ExtraFieldFormItem, ParsedExtras, StringifiedExtras } from "../../components/extraFields"; import { MultiColorPicker } from "../../components/multiColorPicker"; import { numberFormatter, numberParser, numberParserAllowEmpty } from "../../utils/parsing"; import { EntityType, useGetFields } from "../../utils/queryFields"; import { getCurrencySymbol, useCurrency } from "../../utils/settings"; import { IVendor } from "../vendors/model"; import { IFilament, IFilamentParsedExtras } from "./model"; /* The API returns the extra fields as JSON values, but we need to parse them into their real types in order for Ant design's form to work properly. ParsedExtras does this for us. We also need to stringify them again before sending them back to the API, which is done by overriding the form's onFinish method. Form.Item's normalize should do this, but it doesn't seem to work. */ export const FilamentEdit: React.FC = () => { const t = useTranslate(); const [messageApi, contextHolder] = message.useMessage(); const [hasChanged, setHasChanged] = useState(false); const extraFields = useGetFields(EntityType.filament); const currency = useCurrency(); const [colorType, setColorType] = useState<"single" | "multi">("single"); const { formProps, saveButtonProps } = useForm({ liveMode: "manual", onLiveEvent() { // Warn the user if the filament has been updated since the form was opened messageApi.warning(t("filament.form.filament_updated")); setHasChanged(true); }, }); // Get vendor selection options const { selectProps } = useSelect({ resource: "vendor", optionLabel: "name", }); // Add the vendor_id field to the form if (formProps.initialValues) { formProps.initialValues["vendor_id"] = formProps.initialValues["vendor"]?.id; // Parse the extra fields from string values into real types formProps.initialValues = ParsedExtras(formProps.initialValues); } // Update colorType state useEffect(() => { if (formProps.initialValues?.multi_color_hexes) { setColorType("multi"); } else { setColorType("single"); } }, [formProps.initialValues?.multi_color_hexes]); // Override the form's onFinish method to stringify the extra fields const originalOnFinish = formProps.onFinish; formProps.onFinish = (allValues: IFilamentParsedExtras) => { if (allValues !== undefined && allValues !== null) { if (colorType == "single") { allValues.multi_color_hexes = ''; } // Lot of stupidity here to make types work const stringifiedAllValues = StringifiedExtras(allValues); originalOnFinish?.({ extra: {}, ...stringifiedAllValues, }); } }; return ( {contextHolder}
({ value: value ? dayjs(value) : undefined, })} > { if (value === undefined) { return null; } return value; }} >