import { Edit, useForm } from "@refinedev/antd"; import { HttpError, IResourceComponentsProps, useTranslate } from "@refinedev/core"; import { Alert, DatePicker, Form, Input, InputNumber, message, Typography } from "antd"; import TextArea from "antd/es/input/TextArea"; import dayjs from "dayjs"; import React, { useState } from "react"; import { ExtraFieldFormItem, ParsedExtras, StringifiedExtras } from "../../components/extraFields"; import { EntityType, useGetFields } from "../../utils/queryFields"; import { IVendor, IVendorParsedExtras } 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 VendorEdit: React.FC = () => { const t = useTranslate(); const [messageApi, contextHolder] = message.useMessage(); const [hasChanged, setHasChanged] = useState(false); const extraFields = useGetFields(EntityType.vendor); const { formProps, saveButtonProps } = useForm({ liveMode: "manual", onLiveEvent() { // Warn the user if the vendor has been updated since the form was opened messageApi.warning(t("vendor.form.vendor_updated")); setHasChanged(true); }, }); // Parse the extra fields from string values into real types if (formProps.initialValues) { formProps.initialValues = ParsedExtras(formProps.initialValues); } // Override the form's onFinish method to stringify the extra fields const originalOnFinish = formProps.onFinish; formProps.onFinish = (allValues: IVendorParsedExtras) => { if (allValues !== undefined && allValues !== null) { // Lot of stupidity here to make types work const stringifiedAllValues = StringifiedExtras(allValues); originalOnFinish?.({ extra: {}, ...stringifiedAllValues, }); } }; return ( {contextHolder}
({ value: value ? dayjs(value) : undefined, })} >