SurveysLayout.tsx 3.2 KB
Newer Older
1
2
3
4
import React, { useEffect, useState } from "react";
import { Outlet, useOutletContext } from "react-router-dom";
import { surveyApi } from "../apis";
import { catchErrors } from "../helpers";
5
import type { ICreateSurvey, ISurvey } from "../types";
6
7
8
9

type SurveysContextType = {
  error: string;
  loading: boolean;
10
  surveys: ICreateSurvey[];
11
12
  create: () => Promise<any>;
  remove: (id: string) => Promise<any>;
13
  update: (survey: ICreateSurvey) => Promise<any>;
Yoon, Daeki's avatar
Yoon, Daeki committed
14
  updateLocalSurveysList: (survey: ICreateSurvey) => void;
15
16
17
18
19
};

export const SurveysLayout = () => {
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(false);
20
  const [surveys, setSurveys] = useState<ICreateSurvey[]>([]);
21
22
23

  useEffect(() => {
    const getSurveys = async () => {
24
      const surveys: ICreateSurvey[] = await surveyApi.getSurveys();
25
26
27
28
29
30
31
      // console.log(surveys);
      setSurveys(surveys);
    };
    getSurveys();
  }, []);

  const create = async (surveyData: ISurvey) => {
32
    const result: ICreateSurvey = await surveyApi.createSurvey(surveyData);
33
34
35
36
    setSurveys([result, ...surveys]);
    return result;
  };

37
38
39
40
41
  /**
   * 수정된 설문 객체를 적용한 새로운 설문 배열 생성하여 리랜더링
   * @param surveyData 바꾸려는 설문 객체
   */
  const update = async (surveyData: ICreateSurvey) => {
Yoon, Daeki's avatar
Yoon, Daeki committed
42
43
44
45
46
47
48
49
50
51
52
53
    const result = await surveyApi.updateSurvey(surveyData);
    const index = surveys.findIndex((survey) => survey._id === result._id);
    surveys[index] = result;
    // const index = surveys.findIndex((survey) => survey._id === surveyData._id);
    // surveys[index] = surveyData;
    // console.log("update in surveys layout layout:", surveyData);
    console.log("updated survey data:", result);
    setSurveys([...surveys]);
    // return result;
  };

  const updateLocalSurveysList = (surveyData: ICreateSurvey) => {
54
55
    const index = surveys.findIndex((survey) => survey._id === surveyData._id);
    surveys[index] = surveyData;
Yoon, Daeki's avatar
Yoon, Daeki committed
56
57
58
59
    // const index = surveys.findIndex((survey) => survey._id === surveyData._id);
    // surveys[index] = surveyData;
    // console.log("update in surveys layout layout:", surveyData);
    console.log("updated local survey data:", surveyData);
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    setSurveys([...surveys]);
  };

  /**
   * 설문 삭제
   * @param id survey id
   */
  const remove = async (id: string) => {
    if (window.confirm("해당 설문조사를 삭제하시겠습니까?")) {
      try {
        setLoading(true);
        const result = await surveyApi.deleteSurvey(id);
        console.log("deleted survey", result);
        setError("");
        const items = surveys.filter((survey) => survey._id !== result._id);
        // console.log("items left:", newItems);
        setSurveys(items);
        alert("삭제되었습니다.");
      } catch (error) {
        console.log("에러발생");
        catchErrors(error, setError);
      } finally {
        setLoading(false);
      }
    }
  };

  return (
    <>
Yoon, Daeki's avatar
Yoon, Daeki committed
89
90
91
92
93
94
95
96
97
98
99
      <Outlet
        context={{
          error,
          loading,
          surveys,
          create,
          remove,
          update,
          updateLocalSurveysList,
        }}
      />
100
101
102
103
104
105
106
    </>
  );
};

export const useSurveys = () => {
  return useOutletContext<SurveysContextType>();
};