SurveyLayout.tsx 3.6 KB
Newer Older
1
2
3
4
import React from "react";
import { NavLink, useOutletContext } from "react-router-dom";
import { Outlet, useNavigate, useParams } from "react-router-dom";
import { useSurveys } from "./SurveysLayout";
Yoon, Daeki's avatar
Yoon, Daeki committed
5
import type { CreateQuestionData, ICreateSurvey, ISurvey } from "../types";
6
import { SpinnerIcon } from "../icons";
Yoon, Daeki's avatar
Yoon, Daeki committed
7
import { surveyApi } from "../apis";
8
9

type SurveyContextType = {
10
  survey: ICreateSurvey;
11
  update: (survey: ISurvey) => Promise<any>;
Yoon, Daeki's avatar
Yoon, Daeki committed
12
13
14
  createQuestion: (question: CreateQuestionData) => Promise<any>;
  removeQuestion: (questionId: string) => Promise<any>;
  updateQuestion: (question: CreateQuestionData) => Promise<any>;
15
16
17
18
19
20
21
22
};

const activeStyle =
  "w-36 h-12 flex justify-center items-center bg-themeColor p-1 text-white text-center font-bold text-xl";
const inActiveStyle =
  "w-36 h-12 flex justify-center items-center bg-white border border-themeColor p-1 text-center font-bold text-xl";

export const SurveyLayout = () => {
Yoon, Daeki's avatar
Yoon, Daeki committed
23
  const { surveys, update, updateLocalSurveysList } = useSurveys();
24
25
26
  let { surveyId } = useParams<{ surveyId: string }>();
  const survey = surveys.find((survey) => survey._id === surveyId);

Yoon, Daeki's avatar
Yoon, Daeki committed
27
  // console.log("surveys in survey layout", surveys);
28
29
30
31
32
33
34
35
36

  if (!survey) {
    return (
      <div className="flex justify-center mt-5">
        <SpinnerIcon className="animate-spin h-10 w-10 mr-1 bg-white text-slate-500" />
      </div>
    );
  }

Yoon, Daeki's avatar
Yoon, Daeki committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  const createQuestion = async (question: CreateQuestionData) => {
    const newQuestion = await surveyApi.addQuestion(survey._id!, question);
    console.log("new question:", newQuestion);

    survey.questions.push(newQuestion);
    updateLocalSurveysList(survey);
  };

  const removeQuestion = async (questionId: string) => {
    await surveyApi.deleteQuestion(survey._id!, questionId);

    const questions = survey.questions;
    const updatedQuestions = questions.filter((q) => q._id !== questionId);

    console.log("questions after deleted question:", updatedQuestions);
    survey.questions = updatedQuestions;
    updateLocalSurveysList(survey);
  };

  const updateQuestion = async (question: CreateQuestionData) => {
    await surveyApi.updateQuestion(survey._id!, question);

    const questions = survey.questions;
    const index = questions.findIndex((q) => q._id === question._id);
    if (index < 0) {
      return;
    }
    questions[index] = question;
    console.log("questions in update question:", questions);
    // setQuestions([...questions]);
    survey.questions = questions;
    updateLocalSurveysList(survey);
  };

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  return (
    <div>
      <div className="flex justify-center items-center mt-6">
        <NavLink
          to={`/surveys/${surveyId}`}
          end={true}
          className={({ isActive }) =>
            isActive
              ? activeStyle + " rounded-l-3xl"
              : inActiveStyle + " rounded-l-3xl"
          }
        >
          설문 미리보기
        </NavLink>
        <NavLink
          to={`/surveys/${surveyId}/edit`}
          className={({ isActive }) => (isActive ? activeStyle : inActiveStyle)}
        >
          설문지 수정
        </NavLink>
        <NavLink
          to={`/surveys/${surveyId}/result`}
          className={({ isActive }) =>
            isActive
              ? activeStyle + " rounded-r-3xl"
              : inActiveStyle + " rounded-r-3xl"
          }
        >
          응답결과
        </NavLink>
      </div>
Yoon, Daeki's avatar
Yoon, Daeki committed
102
103
104
105
106
107
108
109
110
      <Outlet
        context={{
          survey,
          createQuestion,
          removeQuestion,
          update,
          updateQuestion,
        }}
      />
111
112
113
114
115
116
117
    </div>
  );
};

export const useSurvey = () => {
  return useOutletContext<SurveyContextType>();
};