TodoModal.js 3.05 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
import { useState, useEffect } from "react";
Kim, Subin's avatar
Kim, Subin committed
2
3
4
import todoApi from "../../apis/todo.api";
import { useAuth } from "../../utils/context";
import catchErrors from "../../utils/catchErrors";
Choi Ga Young's avatar
Choi Ga Young committed
5
import moment from "moment";
6
7
import styles from "./modal.module.scss";

Kim, Subin's avatar
Kim, Subin committed
8
const TodoModal = ({ curDate, selectTodo, clicked, setClicked }) => {
Kim, Subin's avatar
Kim, Subin committed
9
  const { user } = useAuth()
Choi Ga Young's avatar
Choi Ga Young committed
10
11
  const [todo, setTodo] = useState({
    todoTitle: "",
Kim, Subin's avatar
Kim, Subin committed
12
    todoDate: moment(curDate).format("YYYY-MM-DD")
Choi Ga Young's avatar
Choi Ga Young committed
13
  })
Kim, Subin's avatar
Kim, Subin committed
14
  const [error, setError] = useState("")
Kim, Subin's avatar
Kim, Subin committed
15
16

  useEffect(() => {
Kim, Subin's avatar
Kim, Subin committed
17
18
    if (clicked) setTodo({ ...todo, ...selectTodo })
  }, [clicked])
Kim, Subin's avatar
Kim, Subin committed
19
20

  useEffect(() => {
Kim, Subin's avatar
Kim, Subin committed
21
22
    setTodo({ ...todo, todoDate: curDate })
  }, [curDate])
Choi Ga Young's avatar
Choi Ga Young committed
23
24
25
26
27

  const handleChange = (e) => {
    const { name, value } = e.target
    setTodo({ ...todo, [name]: value })
  }
28

Kim, Subin's avatar
Kim, Subin committed
29
30
31
32
33
  function handleClick() {
    setTodo({ todoTitle: "", todoDate: "" })
    setClicked(false)
  }

Kim, Subin's avatar
Kim, Subin committed
34
35
36
37
38
39
40
41
42
43
  async function handleSubmit() {
    try {
      setError("")
      if (selectTodo) {
        await todoApi.edit(todo, user.id)
        alert("해당 할일이 성공적으로 수정되었습니다.")
      } else {
        await todoApi.submit(todo, user.id)
        alert("해당 할일이 성공적으로 등록되었습니다.")
      }
Kim, Subin's avatar
Kim, Subin committed
44
      setClicked(false)
Kim, Subin's avatar
Kim, Subin committed
45
46
47
48
      window.location.reload()
    } catch (error) {
      catchErrors(error, setError)
    }
Choi Ga Young's avatar
Choi Ga Young committed
49
  }
50

Choi Ga Young's avatar
Choi Ga Young committed
51
  return (
Choi Ga Young's avatar
Choi Ga Young committed
52
    <div className="modal fade" id="todomodal" data-bs-backdrop="static" data-bs-keyboard="false" tabIndex="-1" aria-labelledby="todoLabel" aria-hidden="true">
Kim, Subin's avatar
Kim, Subin committed
53
54
55
      <div className="modal-dialog modal-dialog-centered">
        <div className="modal-content" style={{ backgroundColor: "crimson" }}>
          <div className="modal-header px-2 py-1" >
Choi Ga Young's avatar
Choi Ga Young committed
56
            <h5 className="modal-title text-white" id="todoLabel">To-do</h5>
Kim, Subin's avatar
Kim, Subin committed
57
58
            <button type="button" className="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
Choi Ga Young's avatar
Choi Ga Young committed
59
          <div className="modal-body bg-white">
Kim, Subin's avatar
Kim, Subin committed
60
61
            <input type="text" name="todoTitle"
              className={`form-control border-top-0 border-end-0 border-start-0 shadow-none rounded-0 ${styles.textInput}`}
Kim, Subin's avatar
Kim, Subin committed
62
              placeholder="제목" onChange={handleChange} value={todo.todoTitle} autoComplete="off" />
Choi Ga Young's avatar
Choi Ga Young committed
63
64
            <div className="d-flex justify-content-between mt-4">
              <label className="col-2 col-form-label ms-2">날짜</label>
Kim, Subin's avatar
Kim, Subin committed
65
              <div className="col-8 d-flex align-items-center">
Kim, Subin's avatar
Kim, Subin committed
66
                <input type="date" className={`form-control form-control-sm shadow-none ${styles.dateInput}`} name="todoDate" onChange={handleChange} value={todo.todoDate} />
Choi Ga Young's avatar
Choi Ga Young committed
67
              </div>
Choi Ga Young's avatar
Choi Ga Young committed
68
            </div>
Kim, Subin's avatar
Kim, Subin committed
69
          </div>
Choi Ga Young's avatar
Choi Ga Young committed
70
          <div className="modal-footer bg-white p-1" >
Kim, Subin's avatar
Kim, Subin committed
71
            <button type="button" className="btn btn-secondary btn-sm"
Kim, Subin's avatar
Kim, Subin committed
72
              data-bs-dismiss="modal" onClick={handleClick}>취소</button>
Kim, Subin's avatar
Kim, Subin committed
73
            <button type="button" className="btn btn-crimson btn-sm" onClick={handleSubmit}>{selectTodo ? "수정" : "확인"}</button>
Choi Ga Young's avatar
Choi Ga Young committed
74
75
76
          </div>
        </div>
      </div>
Kim, Subin's avatar
Kim, Subin committed
77
    </div>
Choi Ga Young's avatar
Choi Ga Young committed
78
79
80
81
  )
}

export default TodoModal;