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({ 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;