refactor: sorted hooks

This commit is contained in:
2025-08-23 19:01:21 +04:00
parent 6ad813ea1d
commit 10f50ac254
27 changed files with 28 additions and 28 deletions

View File

@ -0,0 +1,65 @@
import React from "react";
import { LexoRank } from "lexorank";
import { useCrudOperations } from "@/hooks/cruds/baseCrud";
import {
BoardSchema,
CreateBoardSchema,
UpdateBoardSchema,
} from "@/lib/client";
import {
createBoardMutation,
deleteBoardMutation,
updateBoardMutation,
} from "@/lib/client/@tanstack/react-query.gen";
import { getMaxByLexorank, getNewLexorank } from "@/utils/lexorank";
type UseBoardsOperationsProps = {
boards: BoardSchema[];
setBoards: React.Dispatch<React.SetStateAction<BoardSchema[]>>;
refetchBoards: () => void;
projectId?: number;
};
export type BoardsCrud = {
onCreate: (name: string) => void;
onUpdate: (boardId: number, board: UpdateBoardSchema) => void;
onDelete: (board: BoardSchema) => void;
};
export const useBoardsCrud = ({
boards,
setBoards,
refetchBoards,
projectId,
}: UseBoardsOperationsProps): BoardsCrud => {
return useCrudOperations<BoardSchema, UpdateBoardSchema, CreateBoardSchema>(
{
entities: boards,
setEntities: setBoards,
refetch: refetchBoards,
mutations: {
create: createBoardMutation(),
update: updateBoardMutation(),
delete: deleteBoardMutation(),
},
getCreateEntity: name => {
if (!projectId) return null;
const lastBoard = getMaxByLexorank(boards);
const newLexorank = getNewLexorank(
lastBoard ? LexoRank.parse(lastBoard.lexorank) : null
);
return {
name,
projectId,
lexorank: newLexorank.toString(),
};
},
getUpdateEntity: (old, update) => ({
...old,
name: update.name ?? old.name,
lexorank: update.lexorank ?? old.lexorank,
}),
getDeleteConfirmTitle: () => "Удаление доски",
}
);
};