2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-13 18:45:40 +00:00

Framework for a submit data query

This commit is contained in:
Oliver Walters
2023-07-28 17:39:39 +10:00
parent b5f8a9bc3c
commit 28d9e6e12b

View File

@ -91,7 +91,7 @@ export function ApiForm(props: ApiFormProps) {
props.opened && props.opened &&
!!props.url && !!props.url &&
fieldDefinitions.length > 0, fieldDefinitions.length > 0,
queryKey: ['form-initial-data', name, props.url, props.pk], queryKey: ['form-initial-data', props.name, props.url, props.pk],
queryFn: async () => { queryFn: async () => {
return api return api
.get(getUrl()) .get(getUrl())
@ -106,6 +106,25 @@ export function ApiForm(props: ApiFormProps) {
} }
}); });
// Query manager for submitting data
const submitQuery = useQuery({
enabled: false, //props.opened && !initialDataQuery.isFetching && !!props.url && fieldDefinitions.length > 0,
queryKey: ['form-submit', props.name, props.url, props.pk],
queryFn: async () => {
return api
.get(getUrl())
.then((response) => {
return response;
})
.catch((error) => {
console.error('Error submitting form:', error);
setError(error.message);
});
},
refetchOnMount: false,
refetchOnWindowFocus: false
});
// State variable to determine if the form can render the data // State variable to determine if the form can render the data
const [canRender, setCanRender] = useState<boolean>(false); const [canRender, setCanRender] = useState<boolean>(false);
@ -121,7 +140,7 @@ export function ApiForm(props: ApiFormProps) {
// Update the canSubmit state variable on status change // Update the canSubmit state variable on status change
useEffect(() => { useEffect(() => {
setCanSubmit(canRender && true); setCanSubmit(canRender && !submitQuery.isFetching);
// TODO: This will be updated when we have a query manager for form submission // TODO: This will be updated when we have a query manager for form submission
}, [canRender]); }, [canRender]);
@ -180,6 +199,11 @@ export function ApiForm(props: ApiFormProps) {
return definitions; return definitions;
} }
function submitForm() {
// console.log('Submitting form');
submitQuery.refetch();
}
return ( return (
<Modal <Modal
size="xl" size="xl"
@ -245,14 +269,14 @@ export function ApiForm(props: ApiFormProps) {
{props.cancelText ?? `Cancel`} {props.cancelText ?? `Cancel`}
</Button> </Button>
<Button <Button
onClick={() => null} onClick={submitForm}
variant="outline" variant="outline"
radius="sm" radius="sm"
color={props.submitColor ?? 'green'} color={props.submitColor ?? 'green'}
disabled={!canSubmit} disabled={!canSubmit}
> >
<Group position="right" spacing={5} noWrap={true}> <Group position="right" spacing={5} noWrap={true}>
<Loader size="xs" /> {submitQuery.isFetching && <Loader size="xs" />}
{props.submitText ?? `Submit`} {props.submitText ?? `Submit`}
</Group> </Group>
</Button> </Button>