Implemented extra fields for spools and vendors as well
This commit is contained in:
@@ -1,21 +1,18 @@
|
||||
import React from "react";
|
||||
import { HttpError, IResourceComponentsProps, useTranslate } from "@refinedev/core";
|
||||
import { Create, useForm, useSelect } from "@refinedev/antd";
|
||||
import { Form, Input, Select, InputNumber, ColorPicker, Button } from "antd";
|
||||
import { Form, Input, Select, InputNumber, ColorPicker, Button, Typography } from "antd";
|
||||
import TextArea from "antd/es/input/TextArea";
|
||||
import { numberFormatter, numberParser } from "../../utils/parsing";
|
||||
import { IVendor } from "../vendors/model";
|
||||
import { IFilament } from "./model";
|
||||
import { EntityType, FieldType, useGetFields } from "../../utils/queryFields";
|
||||
import { IFilament, IFilamentParsedExtras } from "./model";
|
||||
import { EntityType, useGetFields } from "../../utils/queryFields";
|
||||
import { ExtraFieldFormItem, StringifiedExtras } from "../../components/extraFields";
|
||||
import dayjs from "dayjs";
|
||||
import utc from "dayjs/plugin/utc";
|
||||
|
||||
dayjs.extend(utc);
|
||||
|
||||
// IFilamentParsedExtras is the same as IFilament, but with the extra field parsed into its real types
|
||||
type IFilamentParsedExtras = Omit<IFilament, "extra"> & { extra?: { [key: string]: unknown } };
|
||||
|
||||
interface CreateOrCloneProps {
|
||||
mode: "create" | "clone";
|
||||
}
|
||||
@@ -58,11 +55,7 @@ export const FilamentCreate: React.FC<IResourceComponentsProps & CreateOrClonePr
|
||||
React.useEffect(() => {
|
||||
extraFields.data?.forEach((field) => {
|
||||
if (formProps.initialValues && field.default_value) {
|
||||
let parsedValue = JSON.parse(field.default_value as string);
|
||||
if (field.field_type === FieldType.datetime) {
|
||||
parsedValue = dayjs(parsedValue);
|
||||
}
|
||||
|
||||
const parsedValue = JSON.parse(field.default_value as string);
|
||||
form.setFieldsValue({ extra: { [field.key]: parsedValue } });
|
||||
}
|
||||
});
|
||||
@@ -263,6 +256,7 @@ export const FilamentCreate: React.FC<IResourceComponentsProps & CreateOrClonePr
|
||||
>
|
||||
<TextArea maxLength={1024} />
|
||||
</Form.Item>
|
||||
<Typography.Title level={5}>{t("settings.extra_fields.tab")}</Typography.Title>
|
||||
{extraFields.data?.map((field, index) => (
|
||||
<ExtraFieldFormItem key={index} field={field} />
|
||||
))}
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
import React, { useState } from "react";
|
||||
import { HttpError, IResourceComponentsProps, useTranslate } from "@refinedev/core";
|
||||
import { Edit, useForm, useSelect } from "@refinedev/antd";
|
||||
import { Form, Input, DatePicker, Select, InputNumber, ColorPicker, message, Alert } from "antd";
|
||||
import { Form, Input, DatePicker, Select, InputNumber, ColorPicker, message, Alert, Typography } from "antd";
|
||||
import dayjs from "dayjs";
|
||||
import TextArea from "antd/es/input/TextArea";
|
||||
import { numberFormatter, numberParser } from "../../utils/parsing";
|
||||
import { IVendor } from "../vendors/model";
|
||||
import { IFilament } from "./model";
|
||||
import { IFilament, IFilamentParsedExtras } from "./model";
|
||||
import { EntityType, useGetFields } from "../../utils/queryFields";
|
||||
import { ExtraFieldFormItem, StringifiedExtras } from "../../components/extraFields";
|
||||
import { ParsedExtras } from "../../components/extraFields";
|
||||
|
||||
// IFilamentParsedExtras is the same as IFilament, but with the extra field parsed into its real types
|
||||
type IFilamentParsedExtras = Omit<IFilament, "extra"> & { extra?: { [key: string]: unknown } };
|
||||
|
||||
/*
|
||||
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.
|
||||
@@ -278,6 +275,7 @@ export const FilamentEdit: React.FC<IResourceComponentsProps> = () => {
|
||||
>
|
||||
<TextArea maxLength={1024} />
|
||||
</Form.Item>
|
||||
<Typography.Title level={5}>{t("settings.extra_fields.tab")}</Typography.Title>
|
||||
{extraFields.data?.map((field, index) => (
|
||||
<ExtraFieldFormItem key={index} field={field} />
|
||||
))}
|
||||
|
||||
@@ -30,8 +30,6 @@ import { useNavigate } from "react-router-dom";
|
||||
|
||||
dayjs.extend(utc);
|
||||
|
||||
// Extra is a key-value dict
|
||||
|
||||
interface IFilamentCollapsed extends Omit<IFilament, "vendor"> {
|
||||
"vendor.name": string | null;
|
||||
}
|
||||
|
||||
@@ -18,3 +18,6 @@ export interface IFilament {
|
||||
color_hex?: string;
|
||||
extra: { [key: string]: string };
|
||||
}
|
||||
|
||||
// IFilamentParsedExtras is the same as IFilament, but with the extra field parsed into its real types
|
||||
export type IFilamentParsedExtras = Omit<IFilament, "extra"> & { extra?: { [key: string]: unknown } };
|
||||
|
||||
@@ -135,6 +135,7 @@ export const FilamentShow: React.FC<IResourceComponentsProps> = () => {
|
||||
<TextField value={record?.article_number} />
|
||||
<Title level={5}>{t("filament.fields.comment")}</Title>
|
||||
<TextField value={enrichText(record?.comment)} />
|
||||
<Title level={4}>{t("settings.extra_fields.tab")}</Title>
|
||||
{extraFields?.data?.map((field, index) => (
|
||||
<ExtraFieldDisplay key={index} field={field} value={record?.extra[field.key]} />
|
||||
))}
|
||||
|
||||
Reference in New Issue
Block a user