refactor: sorted hooks
This commit is contained in:
65
src/hooks/cruds/useBoardsCrud.tsx
Normal file
65
src/hooks/cruds/useBoardsCrud.tsx
Normal 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: () => "Удаление доски",
|
||||
}
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user