Skip to content

Commit b1a2275

Browse files
author
Ole Martin Handeland
committed
This mutation check was not really reactive, so it could end up locking the entire application if the component never re-renders. Also, if you're unlucky you'll suddenly get the loader again during normal form filling because this re-rendered and suddenly found a (normal) mutation was in progress.
Fixing this seems to make both tests stable again: - components.ts 'should be possible to change language back and forth and reflect the change in the UI', which was the initially flaky one that made me write this code - validation.ts 'Required field validation should be visible on submit, not on blur', which now failed sometimes because you can't blur a field that doesn't have focus (and it didn't have focus any more because the loader flashed by for a short while when mutating).
1 parent 8dd381d commit b1a2275

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/features/datamodel/DataModelsProvider.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { useCallback, useEffect, useMemo } from 'react';
1+
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
22
import type { PropsWithChildren } from 'react';
33

4-
import { useQueryClient } from '@tanstack/react-query';
4+
import { useMutationState, useQueryClient } from '@tanstack/react-query';
55
import deepEqual from 'fast-deep-equal';
66
import { createStore } from 'zustand';
77
import type { JSONSchema7 } from 'json-schema';
@@ -256,18 +256,19 @@ function BlockUntilLoaded({ children }: PropsWithChildren) {
256256
useSelector((state) => state);
257257
const actualCurrentTask = useCurrentLayoutSetId();
258258
const isPDF = useIsPdf();
259-
const mutations = useQueryClient()
260-
.getMutationCache()
261-
.findAll({ mutationKey: ['saveFormData'] });
262259

263-
if (mutations.some((m) => m.state.status === 'pending')) {
260+
const currentMutations = useMutationState({ filters: { status: 'pending', mutationKey: ['saveFormData'] } });
261+
const hasPassedMutationCheck = useRef(false);
262+
if (currentMutations.length > 0 && !hasPassedMutationCheck.current) {
264263
// FormDataWrite automatically saves unsaved changes on unmount. If something happens above us in the render tree
265264
// that causes FormDataWrite to be unmounted (forcing it to save) and re-mounts everything (including us), we
266265
// should wait for that previously started save to complete. Otherwise, we'd end up saving outdated initial data
267266
// and cause a 409 when patching later.
268267
return <Loader reason='save-form-data' />;
269268
}
270269

270+
hasPassedMutationCheck.current = true;
271+
271272
if (error) {
272273
// Error trying to fetch data, if missing rights we display relevant page
273274
if (isAxiosError(error) && error.response?.status === HttpStatusCodes.Forbidden) {

0 commit comments

Comments
 (0)