Skip to content

Commit

Permalink
fix(form): wait for background fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
soofstad committed May 23, 2024
1 parent a02c711 commit 6d290e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
11 changes: 6 additions & 5 deletions packages/dm-core-plugins/src/form/FormPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
import { Form } from './components/Form'

export const FormPlugin = (props: IUIPlugin) => {
const { document, isLoading, updateDocument, error } = useDocument<any>(
props.idReference,
0
)
if (isLoading) return <Loading />
const { document, isLoading, updateDocument, error, isFetching } =
useDocument<any>(props.idReference, 0)

// react-hook-form is unable to rerender when the document is updated.
// This means that the form will not benefit from react-query caching.
if (isLoading || isFetching) return <Loading />

if (error) throw new Error(JSON.stringify(error, null, 2))

Expand Down
7 changes: 4 additions & 3 deletions packages/dm-core/src/hooks/useDocument.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useState } from 'react'
interface IUseDocumentReturnType<T> {
document: T | null
isLoading: boolean
isFetching: boolean // react-query boolean, same as 'isLoading', but also includes background fetching
updateDocument: (
newDocument: T,
notify: boolean,
Expand Down Expand Up @@ -57,15 +58,14 @@ export function useDocument<T>(
const { dmssAPI } = useApplication()
const [errorResponse, setErrorResponse] = useState<ErrorResponse | null>(null)
const queryClient = useQueryClient()

const queryKeys = ['documents', idReference, depth]
const documentDepth: number = depth ?? 0
if (documentDepth < 0 || documentDepth > 999)
throw new Error('Depth must be a positive number < 999')

const { isPending, data } = useQuery({
const { isPending, data, isFetching } = useQuery({
staleTime: 5 * 1000,
refetchOnMount: false,
refetchOnMount: true,
queryKey: queryKeys,
queryFn: () =>
dmssAPI
Expand Down Expand Up @@ -119,6 +119,7 @@ export function useDocument<T>(
return {
document: data || null,
isLoading: isPending,
isFetching: isFetching,
updateDocument: (newDocument: T, notify, partialUpdate) =>
mutation.mutateAsync({ newDocument, notify, partialUpdate }),
error: errorResponse,
Expand Down

0 comments on commit 6d290e8

Please sign in to comment.