Files
Fulfillment-Frontend/src/pages/DealsPage/modals/BoardModal/hooks/useBoardModal.tsx
2025-02-07 20:07:10 +04:00

88 lines
2.2 KiB
TypeScript

import { useForm } from "@mantine/form";
import { BoardSchema, BoardService } from "../../../../../client";
import { notifications } from "../../../../../shared/lib/notifications.ts";
type BoardForm = {
name: string;
}
type Props = {
projectId: number;
board?: BoardSchema;
refetchBoards: () => void;
closeModal: () => void;
};
const useBoardModal = ({
projectId,
board,
refetchBoards,
closeModal,
}: Props) => {
const form = useForm<BoardForm>({
initialValues: {
name: board ? board.name : "",
},
validate: {
name: name => !name && "Необходимо ввести название доски",
},
});
const createBoard = (values: BoardForm) => {
BoardService.createBoard({
requestBody: {
board: {
projectId: projectId,
name: values.name,
},
},
})
.then(({ ok, message }) => {
if (!ok) {
notifications.error({ message });
return;
}
refetchBoards();
closeModal();
})
.catch(err => console.log(err));
};
const updateBoard = (values: BoardForm) => {
if (!board) return;
BoardService.updateBoard({
requestBody: {
board: {
...board,
name: values.name,
},
},
})
.then(({ ok, message }) => {
if (!ok) {
notifications.error({ message });
return;
}
refetchBoards();
closeModal();
})
.catch(err => console.log(err));
};
const onSubmit = (values: BoardForm) => {
if (board) {
updateBoard(values);
return;
}
createBoard(values);
};
return {
form,
onSubmit,
};
};
export default useBoardModal;