|
1 |
| -import React, { useCallback, useEffect, useMemo } from 'react'; |
| 1 | +import React, { useCallback, useEffect, useMemo, useRef } from 'react'; |
2 | 2 | import type { PropsWithChildren } from 'react';
|
3 | 3 |
|
4 |
| -import { useQueryClient } from '@tanstack/react-query'; |
| 4 | +import { useMutationState, useQueryClient } from '@tanstack/react-query'; |
5 | 5 | import deepEqual from 'fast-deep-equal';
|
6 | 6 | import { createStore } from 'zustand';
|
7 | 7 | import type { JSONSchema7 } from 'json-schema';
|
@@ -256,18 +256,19 @@ function BlockUntilLoaded({ children }: PropsWithChildren) {
|
256 | 256 | useSelector((state) => state);
|
257 | 257 | const actualCurrentTask = useCurrentLayoutSetId();
|
258 | 258 | const isPDF = useIsPdf();
|
259 |
| - const mutations = useQueryClient() |
260 |
| - .getMutationCache() |
261 |
| - .findAll({ mutationKey: ['saveFormData'] }); |
262 | 259 |
|
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) { |
264 | 263 | // FormDataWrite automatically saves unsaved changes on unmount. If something happens above us in the render tree
|
265 | 264 | // that causes FormDataWrite to be unmounted (forcing it to save) and re-mounts everything (including us), we
|
266 | 265 | // should wait for that previously started save to complete. Otherwise, we'd end up saving outdated initial data
|
267 | 266 | // and cause a 409 when patching later.
|
268 | 267 | return <Loader reason='save-form-data' />;
|
269 | 268 | }
|
270 | 269 |
|
| 270 | + hasPassedMutationCheck.current = true; |
| 271 | + |
271 | 272 | if (error) {
|
272 | 273 | // Error trying to fetch data, if missing rights we display relevant page
|
273 | 274 | if (isAxiosError(error) && error.response?.status === HttpStatusCodes.Forbidden) {
|
|
0 commit comments