import { useState, useEffect, useRef } from "react"; import todoApi from "../../apis/todo.api"; import { useAuth } from "../../utils/context"; import catchErrors from "../../utils/catchErrors"; import moment from "moment"; import styles from "./modal.module.scss"; const TodoModal = ({ curDate, selectTodo = "" }) => { const { user } = useAuth() const [todo, setTodo] = useState({ todoTitle: "", todoDate: moment(curDate).format("YYYY-MM-DD") }) const [error, setError] = useState(""); useEffect(() => { setTodo({ ...todo, todoDate: curDate }) }, [curDate]) useEffect(() => { if (selectTodo) { console.log("selectTodo 값 변경으로 실행") setTodo({ ...todo, ...selectTodo }) } }, [selectTodo]) const handleChange = (e) => { const { name, value } = e.target setTodo({ ...todo, [name]: value }) } async function handleSubmit() { try { setError("") if (selectTodo) { await todoApi.edit(todo, user.id) alert("해당 할일이 성공적으로 수정되었습니다.") } else { await todoApi.submit(todo, user.id) alert("해당 할일이 성공적으로 등록되었습니다.") } window.location.reload() } catch (error) { catchErrors(error, setError) } } return ( ) } export default TodoModal;