TodoList.js 3.49 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
2
import { useState, useEffect } from "react";
import { useParams } from "react-router-dom";
Kim, Subin's avatar
Kim, Subin committed
3
4
import TodoModal from "../components/Modal/TodoModal";
import TodoPostModal from "../components/Modal/TodoPostModal";
Kim, Subin's avatar
Kim, Subin committed
5
6
7
8
import todoApi from "../apis/todo.api";
import { useAuth } from "../utils/context";
import catchErrors from "../utils/catchErrors";
import moment from "moment";
9
10
11
import styles from "../components/Form/form.module.scss";

const TodoList = () => {
Kim, Subin's avatar
Kim, Subin committed
12
13
14
15
  const { user } = useAuth()
  const { date } = useParams()
  const [todoList, setTodoList] = useState([])
  const [selectTodo, setSelectTodo] = useState(null)
Kim, Subin's avatar
Kim, Subin committed
16
17
  const [clicked, setClicked] = useState(false)
  const [error, setError] = useState("")
Kim, Subin's avatar
Kim, Subin committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

  useEffect(() => {
    getAll()
  }, [date])

  async function getAll() {
    try {
      setError("")
      const resList = await todoApi.getTodo(user.id, date)
      setTodoList(resList)
    } catch (error) {
      catchErrors(error, setError)
    }
  }

Kim, Subin's avatar
Kim, Subin committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  async function checkFn(e, todoId) {
    try {
      setError("")
      const check_v = e.target.value === "true" ? true : false
      await todoApi.edit({ id: todoId, done: check_v }, user.id)
      alert("해당 변경사항이 정상적으로 저장되었습니다.")
      window.location.reload()
      // getAll()
      // getTodoList()
    } catch (error) {
      catchErrors(error, setError)
    }
  }

Kim, Subin's avatar
Kim, Subin committed
47
48
49
50
51
  async function delayTodo() {
    try {
      setError("")
      const nextDate = moment(date).add(1, 'day').format("YYYY-MM-DD")
      await todoApi.edit({ id: selectTodo.id, todoDate: nextDate }, user.id)
Kim, Subin's avatar
Kim, Subin committed
52
      window.location.reload()
Kim, Subin's avatar
Kim, Subin committed
53
54
55
56
57
58
59
60
61
62
    } catch (error) {
      catchErrors(error, setError)
    }
  }

  async function delTodo(todoId) {
    try {
      setError("")
      await todoApi.remove(todoId, user.id)
      alert("해당 할일이 성공적으로 삭제되었습니다.")
Kim, Subin's avatar
Kim, Subin committed
63
      window.location.reload()
Kim, Subin's avatar
Kim, Subin committed
64
65
66
67
    } catch (error) {
      catchErrors(error, setError)
    }
  }
68
69

  return (
Kim, Subin's avatar
Kim, Subin committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    <div className={`mt-3 ${styles.list}`}>
      <div className={todoList.length ? "d-flex flex-column" : "d-block"}>
        {todoList.length !== 0 ?
          todoList.map((todo, idx) => <div key={idx} className="d-flex mb-1">
            <div className="d-flex align-items-center" style={{ width: "75%" }}>
              <div className="col d-flex align-items-center">
                <input className={`form-check-input rounded-0 shadow-none mt-0 ${styles.checkBox}`} type="checkbox" id={"todoCheck" + idx} value={todo.done} checked={todo.done} onClick={(e) => checkFn(e, todo.id)} />
              </div>
              <label className="col-11 form-check-label fs-5 pe-1 text-nowrap" style={{ overflow: "hidden", textOverflow: "ellipsis" }}>{todo.todoTitle}</label>
            </div>
            <div className="d-flex justify-content-between" style={{ cursor: "pointer", width: "25%" }}>
              <i className="bi bi-arrow-right fs-5" data-bs-toggle="modal" data-bs-target="#postmodal" onClick={() => setSelectTodo(todo)}></i>
              <i className="bi bi-pencil-square fs-5" data-bs-toggle="modal" data-bs-target="#todomodal" onClick={() => { setSelectTodo(todo); setClicked(true) }}></i>
              <i className="bi bi-trash fs-5" onClick={() => delTodo(todo.id)}></i>
            </div>
          </div>) : <p className="text-center">등록된 할일이 없습니다.</p>}
        <TodoPostModal handleClick={delayTodo} />
        <TodoModal curDate={date} selectTodo={selectTodo} clicked={clicked} setClicked={setClicked} />
      </div>
89
    </div>
90
91
92
93
  )
}

export default TodoList;