Skip to content
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

Fix data model reader not getting updated by action result #2896

Draft
wants to merge 2 commits 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
3 changes: 1 addition & 2 deletions src/features/formData/FormDataReaders.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ async function render(props: TestProps) {
function urlFor(dataModelName: string) {
for (const [uuid, name] of Object.entries(idToNameMap)) {
if (name === dataModelName) {
const isDefault = dataModelName === props.defaultDataModel;
return `https://local.altinn.cloud/ttd/test/instances/${instanceId}/data/${uuid}?includeRowId=${isDefault.toString()}&language=nb`;
return `https://local.altinn.cloud/ttd/test/instances/${instanceId}/data/${uuid}?includeRowId=true&language=nb`;
}
}
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/features/formData/FormDataReaders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ function SpecificDataModelFetcher({ reader, isAvailable }: { reader: DataModelRe
const dataType = reader.getName();
const dataElements = useLaxInstanceDataElements(dataType);
const dataElementId = getFirstDataElementId(dataElements, dataType);
const url = useDataModelUrl({ includeRowIds: false, dataType, dataElementId, language: useCurrentLanguage() });
const enabled = isAvailable && reader.isLoading();
const url = useDataModelUrl({ includeRowIds: true, dataType, dataElementId, language: useCurrentLanguage() });
const enabled = isAvailable;
const { data, error } = useFormDataQuery(enabled ? url : undefined);
const { updateModel } = useCtx();

Expand Down
51 changes: 33 additions & 18 deletions src/features/formData/FormDataWrite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ const {
function useFormDataSaveMutation() {
const { doPatchFormData, doPostStatelessFormData } = useAppMutations();
const getDataModelUrl = useGetDataModelUrl();
const updateQueryCache = useUpdateQueryCache();
const instanceId = useLaxInstanceId();
const multiPatchUrl = instanceId ? getMultiPatchUrl(instanceId) : undefined;
const currentLanguage = useAsRef(useCurrentLanguage());
const dataModelsRef = useAsRef(useSelector((state) => state.dataModels));
const dataModelsRef = useSelectorAsRef((state) => state.dataModels);
const saveFinished = useSelector((s) => s.saveFinished);
const cancelSave = useSelector((s) => s.cancelSave);
const isStateless = useApplicationMetadata().isStatelessApp;
Expand All @@ -99,21 +100,6 @@ function useFormDataSaveMutation() {
FormDataContext
>(useStore());
const useIsSavingRef = useAsRef(useIsSaving());
const queryClient = useQueryClient();

// This updates the query cache with the new data models every time a save has finished. This means we won't have to
// refetch the data from the backend if the providers suddenly change (i.e. when navigating back and forth between
// the main form and a subform).
function updateQueryCache(result: FDSaveFinished) {
for (const { dataType, data, dataElementId } of result.newDataModels) {
const url = getDataModelUrl({ dataType, dataElementId, includeRowIds: true });
if (!url) {
continue;
}
const queryKey = getFormDataQueryKey(url);
queryClient.setQueryData(queryKey, data);
}
}

const mutation = useMutation({
mutationKey: ['saveFormData'],
Expand Down Expand Up @@ -261,7 +247,7 @@ function useFormDataSaveMutation() {
cancelSave();
},
onSuccess: (result) => {
result && updateQueryCache(result);
result && updateQueryCache(result.newDataModels);
result && saveFinished(result);
!result && cancelSave();
},
Expand All @@ -288,6 +274,28 @@ export function useIsSaving() {
);
}

// This updates the query cache with the new data models every time a save has finished. This means we won't have to
// refetch the data from the backend if the providers suddenly change (i.e. when navigating back and forth between
// the main form and a subform).
function useUpdateQueryCache() {
const getDataModelUrl = useGetDataModelUrl();
const queryClient = useQueryClient();

return useCallback(
(newDataModels: { dataType?: string; dataElementId?: string; data: unknown }[]) => {
for (const { dataType, data, dataElementId } of newDataModels) {
const url = getDataModelUrl({ dataType, dataElementId, includeRowIds: true });
if (!url) {
continue;
}
const queryKey = getFormDataQueryKey(url);
queryClient.setQueryData(queryKey, data);
}
},
[getDataModelUrl, queryClient],
);
}

export function FormDataWriteProvider({ children }: PropsWithChildren) {
const proxies = useFormDataWriteProxies();
const ruleConnections = useRuleConnections();
Expand Down Expand Up @@ -792,6 +800,8 @@ export const FD = {
* in a certain state. Locking will effectively ignore all saving until you unlock it again.
*/
useLocking(lockId: string) {
const updateQueryCache = useUpdateQueryCache();

const rawLock = useSelector((s) => s.lock);
const rawUnlock = useSelector((s) => s.unlock);

Expand Down Expand Up @@ -835,10 +845,15 @@ export const FD = {
return false;
}

const updatedDataModels =
actionResult?.updatedDataModels &&
Object.entries(actionResult.updatedDataModels).map(([dataElementId, data]) => ({ dataElementId, data }));

updatedDataModels && updateQueryCache(updatedDataModels);
rawUnlock(actionResult);
return true;
},
[isLockedByMeRef, isLockedRef, lockId, lockedByRef, rawUnlock],
[isLockedByMeRef, isLockedRef, lockId, lockedByRef, rawUnlock, updateQueryCache],
);

return useMemo(
Expand Down
Loading