Skip to content

Refetch instance data on task change by adding task id to query key #3263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/features/instance/InstanceContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect } from 'react';
import { useParams } from 'react-router-dom';
import type { PropsWithChildren } from 'react';

import { skipToken, useQuery } from '@tanstack/react-query';
Expand Down Expand Up @@ -101,22 +102,32 @@ const {
})),
});

const instanceQueryKeys = {
instanceData: (
instanceOwnerPartyId: string | undefined,
instanceGuid: string | undefined,
taskId: string | undefined,
) => ['instanceData', instanceOwnerPartyId, instanceGuid, taskId],
};

// Also used for prefetching @see appPrefetcher.ts
export function useInstanceDataQueryDef(
hasResultFromInstantiation: boolean,
partyId?: string,
instanceGuid?: string,
partyId: string | undefined,
instanceGuid: string | undefined,
taskId: string | undefined,
): QueryDefinition<IInstance> {
const { fetchInstanceData } = useAppQueries();
return {
queryKey: ['fetchInstanceData', partyId, instanceGuid],
queryKey: instanceQueryKeys.instanceData(partyId, instanceGuid, taskId),
queryFn: partyId && instanceGuid ? () => fetchInstanceData(partyId, instanceGuid) : skipToken,
enabled: !!partyId && !!instanceGuid && !hasResultFromInstantiation,
};
}

function useGetInstanceDataQuery(hasResultFromInstantiation: boolean, partyId: string, instanceGuid: string) {
const utils = useQuery(useInstanceDataQueryDef(hasResultFromInstantiation, partyId, instanceGuid));
const { taskId } = useParams();
const utils = useQuery(useInstanceDataQueryDef(hasResultFromInstantiation, partyId, instanceGuid, taskId));

useEffect(() => {
utils.error && window.logError('Fetching instance data failed:\n', utils.error);
Expand Down
8 changes: 7 additions & 1 deletion src/queries/appPrefetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import { useProfileQueryDef } from 'src/features/profile/ProfileProvider';
export function AppPrefetcher() {
const { instanceOwnerPartyId, instanceGuid } =
matchPath({ path: '/instance/:instanceOwnerPartyId/:instanceGuid/*' }, window.location.hash.slice(1))?.params ?? {};

const taskId = matchPath(
{ path: '/instance/:instanceOwnerPartyId/:instanceGuid/:taskId/*' },
window.location.hash.slice(1),
)?.params?.taskId;

const instanceId = instanceOwnerPartyId && instanceGuid ? `${instanceOwnerPartyId}/${instanceGuid}` : undefined;

usePrefetchQuery(getApplicationMetadataQueryDef(instanceGuid));
Expand All @@ -28,7 +34,7 @@ export function AppPrefetcher() {
usePrefetchQuery(usePartiesQueryDef(true), Boolean(instanceOwnerPartyId));
usePrefetchQuery(useCurrentPartyQueryDef(true), Boolean(instanceOwnerPartyId));

usePrefetchQuery(useInstanceDataQueryDef(false, instanceOwnerPartyId, instanceGuid));
usePrefetchQuery(useInstanceDataQueryDef(false, instanceOwnerPartyId, instanceGuid, taskId));
usePrefetchQuery(getProcessQueryDef(instanceId));

return null;
Expand Down
Loading