mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Fix for initial form data (#6791)
* Fix for initial form data - Ref: https://github.com/inventree/InvenTree/pull/6699 * Hide fields until OPTIONS request is complete * Add divider at bottom of form * Fix forms.tsx
This commit is contained in:
parent
cfe00aaa0f
commit
405ca18ec4
@ -6,7 +6,7 @@ import {
|
|||||||
Paper,
|
Paper,
|
||||||
Text
|
Text
|
||||||
} from '@mantine/core';
|
} from '@mantine/core';
|
||||||
import { Button, Group, Stack } from '@mantine/core';
|
import { Button, Divider, Group, Stack } from '@mantine/core';
|
||||||
import { useId } from '@mantine/hooks';
|
import { useId } from '@mantine/hooks';
|
||||||
import { notifications } from '@mantine/notifications';
|
import { notifications } from '@mantine/notifications';
|
||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
@ -104,7 +104,7 @@ export function OptionsApiForm({
|
|||||||
[props.url, props.pk, props.pathParams]
|
[props.url, props.pk, props.pathParams]
|
||||||
);
|
);
|
||||||
|
|
||||||
const { data } = useQuery({
|
const optionsQuery = useQuery({
|
||||||
enabled: true,
|
enabled: true,
|
||||||
queryKey: [
|
queryKey: [
|
||||||
'form-options-data',
|
'form-options-data',
|
||||||
@ -139,18 +139,12 @@ export function OptionsApiForm({
|
|||||||
const formProps: ApiFormProps = useMemo(() => {
|
const formProps: ApiFormProps = useMemo(() => {
|
||||||
const _props = { ...props };
|
const _props = { ...props };
|
||||||
|
|
||||||
// This forcefully overrides initial data
|
|
||||||
// Currently, most modals do not get pre-loaded correctly
|
|
||||||
if (!data) {
|
|
||||||
_props.fields = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!_props.fields) return _props;
|
if (!_props.fields) return _props;
|
||||||
|
|
||||||
for (const [k, v] of Object.entries(_props.fields)) {
|
for (const [k, v] of Object.entries(_props.fields)) {
|
||||||
_props.fields[k] = constructField({
|
_props.fields[k] = constructField({
|
||||||
field: v,
|
field: v,
|
||||||
definition: data?.[k]
|
definition: optionsQuery?.data?.[k]
|
||||||
});
|
});
|
||||||
|
|
||||||
// If the user has specified initial data, use that value here
|
// If the user has specified initial data, use that value here
|
||||||
@ -162,16 +156,30 @@ export function OptionsApiForm({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return _props;
|
return _props;
|
||||||
}, [data, props]);
|
}, [optionsQuery.data, props]);
|
||||||
|
|
||||||
return <ApiForm id={id} props={formProps} />;
|
return (
|
||||||
|
<ApiForm
|
||||||
|
id={id}
|
||||||
|
props={formProps}
|
||||||
|
optionsLoading={optionsQuery.isFetching}
|
||||||
|
/>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An ApiForm component is a modal form which is rendered dynamically,
|
* An ApiForm component is a modal form which is rendered dynamically,
|
||||||
* based on an API endpoint.
|
* based on an API endpoint.
|
||||||
*/
|
*/
|
||||||
export function ApiForm({ id, props }: { id: string; props: ApiFormProps }) {
|
export function ApiForm({
|
||||||
|
id,
|
||||||
|
props,
|
||||||
|
optionsLoading
|
||||||
|
}: {
|
||||||
|
id: string;
|
||||||
|
props: ApiFormProps;
|
||||||
|
optionsLoading: boolean;
|
||||||
|
}) {
|
||||||
const defaultValues: FieldValues = useMemo(() => {
|
const defaultValues: FieldValues = useMemo(() => {
|
||||||
let defaultValuesMap = mapFields(props.fields ?? {}, (_path, field) => {
|
let defaultValuesMap = mapFields(props.fields ?? {}, (_path, field) => {
|
||||||
return field.value ?? field.default ?? undefined;
|
return field.value ?? field.default ?? undefined;
|
||||||
@ -227,6 +235,7 @@ export function ApiForm({ id, props }: { id: string; props: ApiFormProps }) {
|
|||||||
try {
|
try {
|
||||||
// Await API call
|
// Await API call
|
||||||
let response = await api.get(url);
|
let response = await api.get(url);
|
||||||
|
|
||||||
// Define function to process API response
|
// Define function to process API response
|
||||||
const processFields = (fields: ApiFormFieldSet, data: NestedDict) => {
|
const processFields = (fields: ApiFormFieldSet, data: NestedDict) => {
|
||||||
const res: NestedDict = {};
|
const res: NestedDict = {};
|
||||||
@ -387,9 +396,16 @@ export function ApiForm({ id, props }: { id: string; props: ApiFormProps }) {
|
|||||||
() =>
|
() =>
|
||||||
isFormLoading ||
|
isFormLoading ||
|
||||||
initialDataQuery.isFetching ||
|
initialDataQuery.isFetching ||
|
||||||
|
optionsLoading ||
|
||||||
isSubmitting ||
|
isSubmitting ||
|
||||||
!props.fields,
|
!props.fields,
|
||||||
[isFormLoading, initialDataQuery.isFetching, isSubmitting, props.fields]
|
[
|
||||||
|
isFormLoading,
|
||||||
|
initialDataQuery.isFetching,
|
||||||
|
isSubmitting,
|
||||||
|
props.fields,
|
||||||
|
optionsLoading
|
||||||
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
const onFormError = useCallback<SubmitErrorHandler<FieldValues>>(() => {
|
const onFormError = useCallback<SubmitErrorHandler<FieldValues>>(() => {
|
||||||
@ -431,7 +447,8 @@ export function ApiForm({ id, props }: { id: string; props: ApiFormProps }) {
|
|||||||
)}
|
)}
|
||||||
<FormProvider {...form}>
|
<FormProvider {...form}>
|
||||||
<Stack spacing="xs">
|
<Stack spacing="xs">
|
||||||
{Object.entries(props.fields ?? {}).map(
|
{!optionsLoading &&
|
||||||
|
Object.entries(props.fields ?? {}).map(
|
||||||
([fieldName, field]) => (
|
([fieldName, field]) => (
|
||||||
<ApiFormField
|
<ApiFormField
|
||||||
key={fieldName}
|
key={fieldName}
|
||||||
@ -449,6 +466,7 @@ export function ApiForm({ id, props }: { id: string; props: ApiFormProps }) {
|
|||||||
</Paper>
|
</Paper>
|
||||||
|
|
||||||
{/* Footer with Action Buttons */}
|
{/* Footer with Action Buttons */}
|
||||||
|
<Divider />
|
||||||
<div>
|
<div>
|
||||||
<Group position="right">
|
<Group position="right">
|
||||||
{props.actions?.map((action, i) => (
|
{props.actions?.map((action, i) => (
|
||||||
|
@ -246,7 +246,7 @@ export function openModalApiForm(props: OpenApiFormProps) {
|
|||||||
children: (
|
children: (
|
||||||
<Stack spacing={'xs'}>
|
<Stack spacing={'xs'}>
|
||||||
<Divider />
|
<Divider />
|
||||||
<ApiForm id={modalId} props={props} />
|
<ApiForm id={modalId} props={props} optionsLoading={false} />
|
||||||
</Stack>
|
</Stack>
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user