Moving Refine Form logic from crud lib to implementation
This commit is contained in:
@@ -8,32 +8,23 @@ import { BaseForm } from "./base-form";
|
||||
type CrudFormProps = {
|
||||
schemaName: string,
|
||||
uiSchema?: UiSchema,
|
||||
resourceBasePath?: string,
|
||||
resource: string,
|
||||
id?: string,
|
||||
onSuccess?: (data: any) => void,
|
||||
record?: any,
|
||||
resourceBasePath: string,
|
||||
onSubmit: (data: any) => void,
|
||||
defaultValue?: any,
|
||||
children?: ReactNode
|
||||
}
|
||||
|
||||
export const CrudForm: React.FC<CrudFormProps> = (props) => {
|
||||
const { schemaName, uiSchema, resourceBasePath="" ,resource, id, onSuccess, defaultValue, children } = props;
|
||||
|
||||
const { onFinish, query, formLoading } = useForm({
|
||||
resource: resourceBasePath == "" ? resource : `${resourceBasePath}/${resource}`,
|
||||
action: id === undefined ? "create" : "edit",
|
||||
redirect: "show",
|
||||
id,
|
||||
onMutationSuccess: (data: any) => { if (onSuccess) { onSuccess(data) } },
|
||||
});
|
||||
const { schemaName, uiSchema, record, resourceBasePath, defaultValue, children, onSubmit } = props;
|
||||
|
||||
const [schema, setSchema] = useState({});
|
||||
const [schemaLoading, setSchemaLoading] = useState(true);
|
||||
useEffect(() => {
|
||||
const fetchSchema = async () => {
|
||||
try {
|
||||
const resourceSchema = id === undefined ? await jsonschemaProvider.getCreateResourceSchema(schemaName)
|
||||
: await jsonschemaProvider.getCardResourceSchema(schemaName);
|
||||
const resourceSchema = record === undefined ? await jsonschemaProvider.getCreateResourceSchema(schemaName)
|
||||
: await jsonschemaProvider.getUpdateResourceSchema(schemaName);
|
||||
setSchema(resourceSchema);
|
||||
setSchemaLoading(false);
|
||||
} catch (error) {
|
||||
@@ -44,19 +35,18 @@ export const CrudForm: React.FC<CrudFormProps> = (props) => {
|
||||
fetchSchema();
|
||||
}, []);
|
||||
|
||||
if(formLoading || schemaLoading) {
|
||||
if(schemaLoading) {
|
||||
return <CircularProgress />
|
||||
}
|
||||
|
||||
const record = query?.data?.data || defaultValue;
|
||||
return (
|
||||
<BaseForm
|
||||
schema={schema}
|
||||
uiSchema={uiSchema}
|
||||
formData={record}
|
||||
formData={record || defaultValue}
|
||||
resourceBasePath={resourceBasePath}
|
||||
onSubmit={
|
||||
(data: any) => onFinish(data)
|
||||
(data: any) => onSubmit(data)
|
||||
}
|
||||
children={children}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user