fix: refine widget modernization
This commit is contained in:
1 parent
6f2cd7fc2b
commit
777bbd89a1
44 files changed
+259
-320
No files matched your search
@@ -1,18 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { getBoardSaveScopeId, shouldRefetchAfterSaveError } from "./updater";
|
||||
|
||||
describe("board persistence coordination", () => {
|
||||
it("uses one mutation scope for every save of the same board", () => {
|
||||
expect(getBoardSaveScopeId("board-1")).toBe(getBoardSaveScopeId("board-1"));
|
||||
expect(getBoardSaveScopeId("board-1")).not.toBe(getBoardSaveScopeId("board-2"));
|
||||
});
|
||||
|
||||
it("only refetches after a failed save when no newer local board exists", () => {
|
||||
const failedBoard = { id: "board-1", items: ["first"] };
|
||||
const newerBoard = { id: "board-1", items: ["first", "second"] };
|
||||
|
||||
expect(shouldRefetchAfterSaveError(failedBoard, failedBoard)).toBe(true);
|
||||
expect(shouldRefetchAfterSaveError(newerBoard, failedBoard)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -14,11 +14,6 @@ export const updateBoardName = (name: string | null) => {
|
||||
boardName = name;
|
||||
};
|
||||
|
||||
export const getBoardSaveScopeId = (boardId: string) => `board-save:${boardId}`;
|
||||
|
||||
export const shouldRefetchAfterSaveError = <T>(currentBoard: T | undefined, failedBoard: T) =>
|
||||
currentBoard === failedBoard;
|
||||
|
||||
export const useUpdateBoard = () => {
|
||||
const utils = clientApi.useUtils();
|
||||
|
||||
@@ -51,19 +46,17 @@ export const usePersistBoard = ({ id, name }: Pick<Board, "id" | "name">) => {
|
||||
const utils = clientApi.useUtils();
|
||||
const { updateBoard } = useUpdateBoard();
|
||||
const { mutateAsync: saveBoardAsync } = clientApi.board.saveBoard.useMutation({
|
||||
scope: { id: getBoardSaveScopeId(id) },
|
||||
scope: { id: `board-save:${id}` },
|
||||
});
|
||||
|
||||
const updateAndPersistBoard = useCallback(
|
||||
(updater: UpdateCallback, options?: PersistBoardOptions) => {
|
||||
const updatedBoard = updateBoard(updater);
|
||||
if (!updatedBoard) return undefined;
|
||||
const persistedBoard = utils.board.getBoardByName.getData({ name });
|
||||
const persistedBoard = updateBoard(updater);
|
||||
if (!persistedBoard) return undefined;
|
||||
|
||||
void saveBoardAsync(persistedBoard).catch((error: unknown) => {
|
||||
const currentBoard = utils.board.getBoardByName.getData({ name });
|
||||
if (shouldRefetchAfterSaveError(currentBoard, persistedBoard)) {
|
||||
if (currentBoard === persistedBoard) {
|
||||
void utils.board.getBoardByName.invalidate({ name });
|
||||
}
|
||||
options?.onError?.(error);
|
||||
|
||||
Reference in new issue
Block a user