43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import type { ButtonHTMLAttributes, MouseEventHandler } from 'react';
|
|
|
|
import { useState } from 'react';
|
|
|
|
import type { AddIncomeItemFormValues } from '@/entity/incomes/item';
|
|
import type { IncomeList } from '@/entity/incomes/list';
|
|
|
|
import { useAddIncomeItem } from '@/entity/incomes/item';
|
|
|
|
type UseAddIncomeItemButtonArgs = {
|
|
listId: IncomeList['id'];
|
|
onClick?: ButtonHTMLAttributes<HTMLButtonElement>['onClick'];
|
|
};
|
|
export const useAddIncomeItemButton = (args: UseAddIncomeItemButtonArgs) => {
|
|
const { listId, onClick } = args;
|
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
const { isPending, mutateAsync: addIncomeItemMutation } = useAddIncomeItem({ id: listId });
|
|
const onCloseModalHandler = () => {
|
|
setIsOpen(false);
|
|
};
|
|
|
|
const onClickHandler: MouseEventHandler<HTMLButtonElement> = (event) => {
|
|
setIsOpen(true);
|
|
onClick?.(event);
|
|
};
|
|
|
|
const onFormSubmitHandler = async (data: AddIncomeItemFormValues) => {
|
|
// todo добавить обработку ошибок (код + тест)
|
|
await addIncomeItemMutation(data);
|
|
|
|
setIsOpen(false);
|
|
};
|
|
|
|
return {
|
|
isOpen,
|
|
isPending,
|
|
onClickHandler,
|
|
onCloseModalHandler,
|
|
onFormSubmitHandler,
|
|
};
|
|
};
|