MySurveyCard.tsx 2.45 KB
Newer Older
1
2
3
4
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import { surveyApi } from "../apis";
import { SurveyType } from "../types";
Jiwon Yoon's avatar
Jiwon Yoon committed
5
import { catchErrors } from "../helpers";
Yoon, Daeki's avatar
Yoon, Daeki committed
6
import { DuplicateIcon } from "../icons";
7
8
9
10
11
12
13
14
15
16
17
18

type Props = {
  data: SurveyType;
};

export const MySurveyCard = ({ data }: Props) => {
  const navigate = useNavigate();
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);
  const [success, setSuccess] = useState(false);

  const editSurvey = () => {
Jiwon Yoon's avatar
Jiwon Yoon committed
19
    navigate(`/surveys/${data._id}/edit`, {
20
      replace: true,
Jiwon Yoon's avatar
Jiwon Yoon committed
21
      state: { save: true },
22
23
24
    });
  };

Jiwon Yoon's avatar
Jiwon Yoon committed
25
  const copyLink = () => {
Jiwon Yoon's avatar
Jiwon Yoon committed
26
    navigator.clipboard.writeText(`http://localhost:8080/survey/${data._id}`);
Jiwon Yoon's avatar
Jiwon Yoon committed
27
28
29
    alert("설문조사의 링크가 클립보드에 저장되었습니다.");
  };

30
  async function deleteSurvey() {
Jiwon Yoon's avatar
Jiwon Yoon committed
31
    if (window.confirm("해당 설문조사를 삭제하시겠습니까?")) {
Jiwon Yoon's avatar
Jiwon Yoon committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
      try {
        if (data._id) {
          const survey = await surveyApi.deleteSurvey(data._id);
          setSuccess(true);
          setError("");
          alert("삭제되었습니다.");
          location.reload();
        } else {
          setLoading(true);
        }
      } catch (error) {
        console.log("에러발생");
        catchErrors(error, setError);
      } finally {
        setLoading(false);
47
      }
Jiwon Yoon's avatar
Jiwon Yoon committed
48
49
    } else {
      alert("삭제를 취소합니다.");
50
51
52
53
    }
  }

  return (
jang dong hyeok's avatar
jang dong hyeok committed
54
    <div className="w-40 h-48 md:w-52 md:h-60 rounded border-2 hover:border-2 hover:border-themeColor">
55
      <button className="w-full" onClick={editSurvey}>
jang dong hyeok's avatar
jang dong hyeok committed
56
57
58
59
60
        <p className="font-bold">
          {data.title ? data.title : "제목없는 설문조사"}
        </p>

        <div className="h-24 md:h-36 p-3 text-ellipsis overflow-y-scroll">
61
62
63
64
          <p className="text-gray-700">
            {data.comment ? data.comment : "설명없는 설문조사"}
          </p>
        </div>
jang dong hyeok's avatar
jang dong hyeok committed
65
66
67
        <p className="text-gray-500 text-sm">
          {data.updatedAt?.substring(0, 10)}
        </p>
68
      </button>
jang dong hyeok's avatar
jang dong hyeok committed
69
      <div className="flex justify-end pt-1 pr-1">
jang dong hyeok's avatar
jang dong hyeok committed
70
        <button className="flex place-self-center" onClick={copyLink}>
Yoon, Daeki's avatar
Yoon, Daeki committed
71
72
          링크복사
          <DuplicateIcon className="w-7 h-7" />
73
        </button>
74
75
        <button
          type="button"
jang dong hyeok's avatar
jang dong hyeok committed
76
          className="bg-themeColor rounded text-white py-1 px-1.5 ml-1 "
77
78
79
80
          onClick={deleteSurvey}
        >
          삭제
        </button>
81
82
83
84
      </div>
    </div>
  );
};