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[]; projectId?: number; queryKey: any[]; }; export type BoardsCrud = { onCreate: (name: string) => void; onUpdate: (boardId: number, board: UpdateBoardSchema) => void; onDelete: (board: BoardSchema) => void; }; export const useBoardsCrud = ({ boards, projectId, queryKey, }: UseBoardsOperationsProps): BoardsCrud => { return useCrudOperations( { key: "getBoards", queryKey, 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: () => "Удаление доски", } ); };