EditSurvey.tsx 1.64 KB
Newer Older
1
import React, { ChangeEvent, useState } from "react";
2
import { Navigate, useLocation, useParams } from "react-router-dom";
Yoon, Daeki's avatar
Yoon, Daeki committed
3
import { surveyApi } from "../apis";
4
import type { CreateQuestionData, IQuestionData, ISurvey } from "../types";
Yoon, Daeki's avatar
Yoon, Daeki committed
5
import { ModifySurvey } from "./ModifySurvey";
6
import { useSurvey } from "../layouts/SurveyLayout";
7
8
import { SpinnerIcon } from "../icons";
import { ModifySurveyView } from "./ModifySurveyView";
Yoon, Daeki's avatar
Yoon, Daeki committed
9
10

export const EditSurvey = () => {
11
12
13
14
15
16
17
  const {
    survey,
    createQuestion,
    removeQuestion,
    updateQuestion,
    updateTitleComment,
  } = useSurvey();
18
19

  const questions = survey.questions;
Yoon, Daeki's avatar
Yoon, Daeki committed
20

21
  console.log("survey", survey);
22
  console.log("questions", questions);
Yoon, Daeki's avatar
Yoon, Daeki committed
23

24
25
  const handleTitleComment = (state: { title: string; comment: string }) => {
    console.log("title in handle title and comment:", state);
26
    updateTitleComment(state);
27
28
  };

Yoon, Daeki's avatar
Yoon, Daeki committed
29
  const addQuestion = async () => {
30
    const question: IQuestionData = {
31
32
33
34
35
36
37
      order: questions.length,
      type: "singletext",
      title: "",
      comment: "",
      isRequired: false,
      content: { choices: [] },
    };
Yoon, Daeki's avatar
Yoon, Daeki committed
38
    await createQuestion(question);
39
40
41
  };

  async function deleteQuestion(id: string) {
Yoon, Daeki's avatar
Yoon, Daeki committed
42
    await removeQuestion(id);
43
44
  }

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

53
54
55
56
57
58
59
  return (
    <ModifySurveyView
      questions={questions}
      survey={survey}
      addQuestion={addQuestion}
      deleteQuestion={deleteQuestion}
      handleQuestion={updateQuestion}
60
      handleTitleComment={handleTitleComment}
61
62
    />
  );
Yoon, Daeki's avatar
Yoon, Daeki committed
63
};