diff --git a/src/app/_components/domains/Task/CreateTaskModal/CreateTaskModal.tsx b/src/app/_components/domains/Task/CreateTaskModal/CreateTaskModal.tsx new file mode 100644 index 0000000..a67f63c --- /dev/null +++ b/src/app/_components/domains/Task/CreateTaskModal/CreateTaskModal.tsx @@ -0,0 +1,90 @@ +'use client'; + +import { FC, useCallback, useState } from 'react'; +import { Button, Input, Modal, ModalBody, ModalContent, ModalFooter, ModalHeader, Textarea } from '@nextui-org/react'; +import { Controller, SubmitHandler, useForm } from 'react-hook-form'; +import { Task } from '~/domains/Task'; + +type Props = { + isOpen: boolean; + onOpenChange: () => void; + currentTask?: Task; +}; + +interface IFormInput { + title: string; + body: string; +} + +export const CreateModal: FC = ({ isOpen, onOpenChange, currentTask }) => { + const [isLoading, setIsLoading] = useState(false); + + const { control, handleSubmit, reset } = useForm({ + defaultValues: { + title: currentTask?.title, + body: currentTask?.body, + }, + }); + + const handleOpenChange = useCallback(() => { + reset(); + onOpenChange(); + }, [onOpenChange, reset]); + + const onSubmit: SubmitHandler = useCallback(async () => { + if (isLoading) return; + setIsLoading(true); + new Promise((resolve) => setTimeout(resolve, 1000)) + .catch((error) => { + // TODO: 本来はコンソールに出すのではなく、ユーザーにエラーを通知する + console.error(error); + }) + .then(() => { + handleOpenChange(); + }) + .finally(() => { + setIsLoading(false); + }); + }, [handleOpenChange, isLoading]); + + return ( + + +
+ タスクを{currentTask ? '編集' : '作成'}する + + ( + + )} + /> + ( +