MySurveyCard.tsx 2.39 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";
Jiwon Yoon's avatar
Jiwon Yoon committed
6
import CopyImg from "../icons/copy.png";
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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 = () => {
    navigate(`/surveys/edit/${data._id}`, {
      replace: true,
Jiwon Yoon's avatar
Jiwon Yoon committed
21
      state: { save: true },
22
23
24
    });
  };

Jiwon Yoon's avatar
Jiwon Yoon committed
25
26
27
28
29
30
  const goSurvey = () => {
    navigate(`/surveys/${data._id}`, {
      replace: true,
    });
  };

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

36
37
38
39
40
41
42
43
44
45
46
47
  async function deleteSurvey() {
    try {
      if (data._id) {
        const survey = await surveyApi.deleteSurvey(data._id);
        setSuccess(true);
        setError("");
        location.reload();
      } else {
        setLoading(true);
      }
    } catch (error) {
      console.log("에러발생");
Jiwon Yoon's avatar
Jiwon Yoon committed
48
      catchErrors(error, setError);
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    } finally {
      setLoading(false);
    }
  }

  return (
    <div className="w-52 h-60 rounded border-2">
      <div className="h-32 p-5">
        <p className="text-gray-700">
          {data.comment ? data.comment : "설명없는 설문조사"}
        </p>
      </div>
      <div className="flex flex-col px-5 py-3">
        <div className="h-12">
Jiwon Yoon's avatar
Jiwon Yoon committed
63
          <button type="button" onClick={editSurvey}>
Jiwon Yoon's avatar
Jiwon Yoon committed
64
65
66
67
            <p className="font-bold">
              {data.title ? data.title : "제목없는 설문조사"}
            </p>
          </button>
68
69
70
71
72
          <p className="text-gray-500 text-sm">
            {data.updatedAt?.substring(0, 10)}
          </p>
        </div>
        <div className="flex justify-end pt-1">
Jiwon Yoon's avatar
Jiwon Yoon committed
73
74
75
          <label className="pt-1">링크복사</label>
          <button className="" onClick={copyLink}>
            <img src={CopyImg} alt="copy"></img>
76
77
78
79
80
81
82
83
84
85
86
87
88
          </button>
          <button
            type="button"
            className="bg-themeColor rounded text-white py-1 px-1.5 ml-1"
            onClick={deleteSurvey}
          >
            삭제
          </button>
        </div>
      </div>
    </div>
  );
};