Commit 2e1489ce authored by Jiwon Yoon's avatar Jiwon Yoon
Browse files

usecontext

parent eab79372
import React, { useState } from "react"; import React, { useState } from "react";
import { Question } from "./Question"; import { Question } from "./Question";
import axios from "axios"; import { QuestionProvider } from "./question.context";
export interface BasicQuestionType { export interface BasicQuestionType {
type: string; type: string;
id: string; _id: string;
title: string; title: string;
isRequired: boolean; isRequired: boolean;
comment: string; comment: string;
...@@ -50,7 +50,7 @@ export interface RatingType extends BasicQuestionType { ...@@ -50,7 +50,7 @@ export interface RatingType extends BasicQuestionType {
const EssayQ: EssayType = { const EssayQ: EssayType = {
type: "essay", type: "essay",
id: "000000000000", _id: "000000000000",
title: "Question Title", title: "Question Title",
isRequired: false, isRequired: false,
comment: "질문에 대한 설명을 입력해주세요", comment: "질문에 대한 설명을 입력해주세요",
...@@ -58,7 +58,7 @@ const EssayQ: EssayType = { ...@@ -58,7 +58,7 @@ const EssayQ: EssayType = {
}; };
const RadioQ: RadioType = { const RadioQ: RadioType = {
type: "radio", type: "radio",
id: "000000000001", _id: "000000000001",
title: "Question Title", title: "Question Title",
isRequired: false, isRequired: false,
comment: "질문에 대한 설명을 입력해주세요", comment: "질문에 대한 설명을 입력해주세요",
...@@ -70,7 +70,7 @@ const RadioQ: RadioType = { ...@@ -70,7 +70,7 @@ const RadioQ: RadioType = {
}; };
const CheckboxQ: CheckboxType = { const CheckboxQ: CheckboxType = {
type: "checkbox", type: "checkbox",
id: "000000000002", _id: "000000000002",
title: "Question Title", title: "Question Title",
isRequired: false, isRequired: false,
comment: "질문에 대한 설명을 입력해주세요", comment: "질문에 대한 설명을 입력해주세요",
...@@ -81,7 +81,7 @@ const CheckboxQ: CheckboxType = { ...@@ -81,7 +81,7 @@ const CheckboxQ: CheckboxType = {
}; };
const DropdownQ: DropdownType = { const DropdownQ: DropdownType = {
type: "dropdown", type: "dropdown",
id: "000000000003", _id: "000000000003",
title: "Question Title", title: "Question Title",
isRequired: false, isRequired: false,
comment: "질문에 대한 설명을 입력해주세요", comment: "질문에 대한 설명을 입력해주세요",
...@@ -92,7 +92,7 @@ const DropdownQ: DropdownType = { ...@@ -92,7 +92,7 @@ const DropdownQ: DropdownType = {
}; };
const FileQ: FileType = { const FileQ: FileType = {
type: "file", type: "file",
id: "000000000004", _id: "000000000004",
title: "Question Title", title: "Question Title",
isRequired: false, isRequired: false,
comment: "질문에 대한 설명을 입력해주세요", comment: "질문에 대한 설명을 입력해주세요",
...@@ -103,7 +103,7 @@ const FileQ: FileType = { ...@@ -103,7 +103,7 @@ const FileQ: FileType = {
}; };
const RatingQ: RatingType = { const RatingQ: RatingType = {
type: "rating", type: "rating",
id: "000000000005", _id: "000000000005",
title: "Question Title", title: "Question Title",
isRequired: false, isRequired: false,
comment: "질문에 대한 설명을 입력해주세요", comment: "질문에 대한 설명을 입력해주세요",
...@@ -119,94 +119,36 @@ const RatingQ: RatingType = { ...@@ -119,94 +119,36 @@ const RatingQ: RatingType = {
}; };
export const CreateSurveyForm = () => { export const CreateSurveyForm = () => {
const [currentId, setCurrentId] = useState<string>(""); const [survey, setSurvey] = useState();
const [error, setError] = useState(""); const [error, setError] = useState("");
const [disabled, setDisabled] = useState(false); const [disabled, setDisabled] = useState(false);
const [success, setSuccess] = useState(false); const [success, setSuccess] = useState(false);
const [questionList, setQuestionList] = useState<Array<BasicQuestionType>>([
EssayQ,
RadioQ,
CheckboxQ,
]);
const [survey, setSurvey] = useState();
function changeCurrentId(event: React.MouseEvent<HTMLButtonElement>): void {
setCurrentId(event.currentTarget.id);
}
function QuestionListChange(e: React.ChangeEvent<HTMLInputElement>): void {
const newList: BasicQuestionType[] = [...questionList];
const obj: any = newList.find((a) => a.id === e.target.id); //고유 _id로 질문찾기
const targetKey: any = e.target.name;
obj[targetKey] = e.target.value;
setQuestionList(newList);
}
async function addQuestion(event: React.MouseEvent<HTMLButtonElement>) {
event.preventDefault();
try {
const res = await axios.post("/api/question/create");
console.log("서버연결됬나요", res);
console.log("회원가입");
setSuccess(true);
setError("");
} catch (error) {
console.log("에러발생");
// catchErrors(error, setError)
} finally {
// setLoading(false);
}
//무작위로 12자리 ID제공, 추후에 질문을 DB에 생성하고 _id를 DB에서 가져오는 것으로 교체할 예정
function getRandomInt(min: number, max: number): string {
min = Math.ceil(min);
max = Math.floor(max);
const randomNum: number = Math.floor(Math.random() * (max - min)) + min;
return randomNum.toString();
}
const randomId: string = getRandomInt(100000000000, 999999999999);
//새로운 질문 생성
const newQ: EssayType = {
type: "essay",
id: randomId,
title: "Question",
isRequired: false,
comment: "질문에 대한 설명을 입력해주세요",
content: null,
};
setQuestionList([...questionList, newQ]);
}
function deleteQuestion(): void {}
return ( return (
<> <>
{console.log(currentId)} <QuestionProvider>
<div className="flex flex-col place-items-center"> <div className="flex flex-col place-items-center">
<div className="flex flex-col container place-items-center mt-4"> <div className="flex flex-col container place-items-center mt-4">
<input <input
type="text" type="text"
className="font-bold text-4xl text-center m-2 border-b-2" className="font-bold text-4xl text-center m-2 border-b-2"
placeholder="설문지 제목" placeholder="설문지 제목"
></input> ></input>
<textarea <textarea
className="font-bold text-1xl text-center m-2 resize-none" className="font-bold text-1xl text-center m-2 resize-none"
placeholder="설문조사에 대한 설명을 입력해주세요" placeholder="설문조사에 대한 설명을 입력해주세요"
rows={2} rows={2}
cols={60} cols={60}
></textarea> ></textarea>
</div> </div>
<Question <Question />
questionList={questionList} <div>
QuestionListChange={QuestionListChange} <button className="border bg-themeColor my-5 py-2 px-3 font-bold text-white">
addQuestion={addQuestion} 설문조사 생성
changeCurrentId={changeCurrentId} </button>
/> </div>
<div>
<button className="border bg-themeColor my-5 py-2 px-3 font-bold text-white">
설문조사 생성
</button>
</div> </div>
</div> </QuestionProvider>
</> </>
); );
}; };
import React from "react"; import React from "react";
import { useQuestion } from "./question.context";
type Props = { type Props = {
id: string; id: string;
changeCurrentId: (event: React.MouseEvent<HTMLButtonElement>) => void;
}; };
export const Edit = ({ id, changeCurrentId }: Props) => { export const Edit = ({ id }: Props) => {
const { editClick } = useQuestion();
return ( return (
<button id={id} className="" onClick={changeCurrentId}> <button id={id} className="w-1/12" onClick={editClick}>
수정 수정
</button> </button>
); );
......
import React from "react"; import React from "react";
import { CheckboxType } from "./CreateSurveyFormPage"; import { CheckboxType } from "./CreateSurveyFormPage";
import { useQuestion } from "./question.context";
import { Edit } from "./Edit";
type Props = { type Props = {
element: CheckboxType; element: CheckboxType;
QuestionListChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
}; };
export const QCheckbox = ({ element, QuestionListChange }: Props) => ( export const QCheckbox = ({ element }: Props) => {
<div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2"> const { questionListChange } = useQuestion();
<div className="flex flexgi-row h-16 w-full place-content-between items-center">
<input return (
type="text" <div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2">
name={element.name} <div className="flex flexgi-row h-16 w-full place-content-between items-center">
id="title" <input
className="text-xl font-bold ml-6 border-b-2 w-1/2" type="text"
placeholder={element.title} name="title"
onChange={QuestionListChange} id={element._id}
></input> className="text-xl font-bold ml-6 border-b-2 w-1/2"
<select placeholder={element.title}
id="Questions" onChange={questionListChange}
className="w-36 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-themeColor w-full mr-3 p-2.5" ></input>
> <select
<option>질문종류</option> id="Questions"
<option value="essay">주관식</option> className="w-36 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-themeColor w-full mr-3 p-2.5"
<option value="radio">객관식</option> >
<option value="dropdown">드롭다운(객관식)</option> <option>질문종류</option>
<option value="checkbox" selected> <option value="essay">주관식</option>
체크박스(객관식) <option value="radio">객관식</option>
</option> <option value="dropdown">드롭다운(객관식)</option>
<option value="file">파일업로드</option> <option value="checkbox" selected>
<option value="rating">선형</option> 체크박스(객관식)
<option value="grid">그리드</option> </option>
<option value="date">날짜</option> <option value="file">파일업로드</option>
</select> <option value="rating">선형</option>
</div> <option value="grid">그리드</option>
<div className="flex w-full justify-center"> <option value="date">날짜</option>
<input </select>
type="text" </div>
name={element.name} <div className="flex w-full justify-center">
id="comment" <input
className="border w-11/12" type="text"
placeholder="질문에 대한 설명을 입력해주세요" name="comment"
onChange={QuestionListChange} id={element._id}
></input> className="border w-11/12"
</div> placeholder="질문에 대한 설명을 입력해주세요"
<div id="commentarea" className="flex mt-4"> onChange={questionListChange}
{element.content.choices.map((e: string) => ( ></input>
<div> </div>
<input type="checkbox" checked={false}></input> <div id="commentarea" className="flex mt-4">
<input {element.content.choices.map((e: string) => (
type="text" <div>
className="mx-2 border-b-2" <input type="checkbox" checked={false}></input>
placeholder={e} <input
></input> type="text"
</div> className="mx-2 border-b-2"
))} placeholder={e}
</div> ></input>
<div className="flex w-full flex-row justify-end py-2"> </div>
<button className="w-1/12">필수</button> ))}
<button className="w-1/12">옵션</button> </div>
<button className="w-1/12">삭제</button> <div className="flex w-full flex-row justify-end py-2">
<button className="w-1/12">필수</button>
<button className="w-1/12">옵션</button>
<button className="w-1/12">삭제</button>
<Edit id={element._id} />
</div>
</div> </div>
</div> );
); };
import { useQuestion } from "./question.context";
import React from "react"; import React from "react";
import { DropdownType } from "./CreateSurveyFormPage"; import { DropdownType } from "./CreateSurveyFormPage";
import { useQuestion } from "./question.context";
type Props = { type Props = {
element: DropdownType; element: DropdownType;
QuestionListChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
}; };
export const QDropdown = ({ element, QuestionListChange }: Props) => ( export const QDropdown = ({ element }: Props) => {
<div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2"> const { questionListChange } = useQuestion();
<div className="flex flexgi-row h-16 w-full place-content-between items-center"> return (
<input <div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2">
type="text" <div className="flex flexgi-row h-16 w-full place-content-between items-center">
name={element.name} <input
id="title" type="text"
className="text-xl font-bold ml-6 border-b-2 w-1/2" name="title"
placeholder={element.title} id={element._id}
onChange={QuestionListChange} className="text-xl font-bold ml-6 border-b-2 w-1/2"
></input> placeholder={element.title}
<select onChange={questionListChange}
id="Questions" ></input>
className="w-36 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-themeColor w-full mr-3 p-2.5" <select
> id="Questions"
<option>질문종류</option> className="w-36 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-themeColor w-full mr-3 p-2.5"
<option value="essay">주관식</option> >
<option value="radio">객관식</option> <option>질문종류</option>
<option value="dropdown" selected> <option value="essay">주관식</option>
드롭다운(객관식) <option value="radio">객관식</option>
</option> <option value="dropdown" selected>
<option value="checkbox">체크박스(객관식)</option> 드롭다운(객관식)
<option value="file">파일업로드</option> </option>
<option value="rating">선형</option> <option value="checkbox">체크박스(객관식)</option>
<option value="grid">그리드</option> <option value="file">파일업로드</option>
<option value="date">날짜</option> <option value="rating">선형</option>
</select> <option value="grid">그리드</option>
<option value="date">날짜</option>
</select>
</div>
<div className="flex w-full justify-center">
<input
type="text"
name="comment"
id={element._id}
className="border w-11/12"
placeholder="질문에 대한 설명을 입력해주세요"
onChange={questionListChange}
></input>
</div>
<div id="commentarea" className="flex mt-4">
{element.content.choices.map((e: string) => (
<div>
<input type="checkbox" checked={false}></input>
<input
type="text"
className="mx-2 border-b-2"
placeholder={e}
></input>
</div>
))}
</div>
<div className="flex w-full flex-row justify-end py-2">
<button className="w-1/12">필수</button>
<button className="w-1/12">옵션</button>
<button className="w-1/12">삭제</button>
</div>
</div> </div>
<div className="flex w-full justify-center"> );
<input };
type="text"
name={element.name}
id="comment"
className="border w-11/12"
placeholder="질문에 대한 설명을 입력해주세요"
onChange={QuestionListChange}
></input>
</div>
<div id="commentarea" className="flex mt-4">
{element.content.choices.map((e: string) => (
<div>
<input type="checkbox" checked={false}></input>
<input
type="text"
className="mx-2 border-b-2"
placeholder={e}
></input>
</div>
))}
</div>
<div className="flex w-full flex-row justify-end py-2">
<button className="w-1/12">필수</button>
<button className="w-1/12">옵션</button>
<button className="w-1/12">삭제</button>
</div>
</div>
);
import React, { useState } from "react"; import React, { useState } from "react";
import { EssayType } from "./CreateSurveyFormPage"; import { EssayType } from "./CreateSurveyFormPage";
import { useQuestion } from "./question.context";
import { Edit } from "./Edit"; import { Edit } from "./Edit";
type Props = { type Props = {
element: EssayType; element: EssayType;
QuestionListChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
changeCurrentId: (event: React.MouseEvent<HTMLButtonElement>) => void;
}; };
export const QEssay = ({ export const QEssay = ({ element }: Props) => {
element, const { questionListChange } = useQuestion();
QuestionListChange,
changeCurrentId,
}: Props) => {
return ( return (
<div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2"> <div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2">
<div className="flex h-16 w-full place-content-between items-center"> <div className="flex h-16 w-full place-content-between items-center">
<input <input
type="text" type="text"
name="title" name="title"
id={element.id} id={element._id}
className="text-xl font-bold ml-6 border-b-2 w-1/2" className="text-xl font-bold ml-6 border-b-2 w-1/2"
placeholder={element.title} placeholder={element.title}
onChange={QuestionListChange} onChange={questionListChange}
></input> ></input>
<select <select
id="Questions" id="Questions"
...@@ -45,10 +42,10 @@ export const QEssay = ({ ...@@ -45,10 +42,10 @@ export const QEssay = ({
<input <input
type="text" type="text"
name="comment" name="comment"
id={element.id} id={element._id}
className="border w-11/12" className="border w-11/12"
placeholder="질문에 대한 설명을 입력해주세요" placeholder="질문에 대한 설명을 입력해주세요"
onChange={QuestionListChange} onChange={questionListChange}
></input> ></input>
</div> </div>
<div id="commentarea" className="flex mt-4 w-full justify-center"> <div id="commentarea" className="flex mt-4 w-full justify-center">
...@@ -58,7 +55,7 @@ export const QEssay = ({ ...@@ -58,7 +55,7 @@ export const QEssay = ({
<button className="w-1/12">필수</button> <button className="w-1/12">필수</button>
<button className="w-1/12">옵션</button> <button className="w-1/12">옵션</button>
<button className="w-1/12">삭제</button> <button className="w-1/12">삭제</button>
<Edit id={element.id} changeCurrentId={changeCurrentId} /> <Edit id={element._id} />
</div> </div>
</div> </div>
); );
......
import React, { useState } from "react"; import React, { useState } from "react";
import { FileType } from "./CreateSurveyFormPage"; import { FileType } from "./CreateSurveyFormPage";
import { useQuestion } from "./question.context";
type Props = { type Props = {
element: FileType; element: FileType;
QuestionListChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
}; };
export const QFile = ({ element, QuestionListChange }: Props) => { export const QFile = ({ element }: Props) => {
const { questionListChange } = useQuestion();
return ( return (
<div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2"> <div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2">
<div className="flex h-16 w-full place-content-between items-center"> <div className="flex h-16 w-full place-content-between items-center">
<input <input
type="text" type="text"
name={element.name} name="title"
id="title" id={element._id}
className="text-xl font-bold ml-6 border-b-2 w-1/2" className="text-xl font-bold ml-6 border-b-2 w-1/2"
placeholder={element.title} placeholder={element.title}
onChange={QuestionListChange} onChange={questionListChange}
></input> ></input>
<select <select
id="Questions" id="Questions"
...@@ -38,11 +40,11 @@ export const QFile = ({ element, QuestionListChange }: Props) => { ...@@ -38,11 +40,11 @@ export const QFile = ({ element, QuestionListChange }: Props) => {
<div className="flex w-full justify-center"> <div className="flex w-full justify-center">
<input <input
type="text" type="text"
name={element.name} name="comment"
id="comment" id={element._id}
className="border w-11/12" className="border w-11/12"
placeholder="질문에 대한 설명을 입력해주세요" placeholder="질문에 대한 설명을 입력해주세요"
onChange={QuestionListChange} onChange={questionListChange}
></input> ></input>
</div> </div>
<div id="commentarea" className="flex mt-4 w-full justify-center"> <div id="commentarea" className="flex mt-4 w-full justify-center">
......
import { useQuestion } from "./question.context";
import React from "react"; import React from "react";
import { RadioType } from "./CreateSurveyFormPage"; import { RadioType } from "./CreateSurveyFormPage";
import { useQuestion } from "./question.context";
type Props = { type Props = {
element: RadioType; element: RadioType;
QuestionListChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
}; };
export const QRadio = ({ element, QuestionListChange }: Props) => ( export const QRadio = ({ element }: Props) => {
<div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2"> const { questionListChange } = useQuestion();
<div className="flex h-16 w-full place-content-between items-center"> return (
<input <div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2">
type="text" <div className="flex h-16 w-full place-content-between items-center">
id={element.id} <input
name="title" type="text"
className="text-xl font-bold ml-6 border-b-2 w-1/2" name="title"
placeholder={element.title} id={element._id}
onChange={QuestionListChange} className="text-xl font-bold ml-6 border-b-2 w-1/2"
></input> placeholder={element.title}
<select onChange={questionListChange}
id="Questions" ></input>
className="w-36 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-themeColor focus:themeColor block w-full mr-3 p-2.5" <select
> id="Questions"
<option>질문종류</option> className="w-36 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-themeColor focus:themeColor block w-full mr-3 p-2.5"
<option value="Essay">주관식</option> >
<option value="MultipleChoice" selected> <option>질문종류</option>
객관식 <option value="Essay">주관식</option>
</option> <option value="MultipleChoice" selected>
<option value="Dropdown">드롭다운(객관식)</option> 객관식
<option value="CheckBox">체크박스(객관식)</option> </option>
<option value="Rating">선형</option> <option value="Dropdown">드롭다운(객관식)</option>
<option value="Grid">그리드</option> <option value="CheckBox">체크박스(객관식)</option>
<option value="Date">날짜</option> <option value="Rating">선형</option>
</select> <option value="Grid">그리드</option>
</div> <option value="Date">날짜</option>
<div className="flex w-full justify-center"> </select>
<input </div>
type="text" <div className="flex w-full justify-center">
id={element.id} <input
name="comment" type="text"
className="border w-11/12" name="comment"
placeholder="질문에 대한 설명을 입력해주세요" id={element._id}
onChange={QuestionListChange} className="border w-11/12"
></input> placeholder="질문에 대한 설명을 입력해주세요"
</div> onChange={questionListChange}
<div className="flex mt-4"> ></input>
{element.content.choices.map((e: string, index: number) => ( </div>
<div> <div className="flex mt-4">
<input {element.content.choices.map((e: string, index: number) => (
type="radio" <div>
id={element.id} <input
name="choice" type="radio"
value={e} id={element._id}
disabled name="choice"
/> value={e}
<input disabled
type="text" />
name={"choice" + `${index}`} <input
// key={`${index}`} type="text"
className="mx-2 border-b-2" name={"choice" + `${index}`}
placeholder={e} // key={`${index}`}
onChange={QuestionListChange} className="mx-2 border-b-2"
></input> placeholder={e}
<button></button> onChange={questionListChange}
</div> ></input>
))} <button></button>
{/* <button className="border rounded-full border-green-500 border-4 text-green-500 font-bold px-2"> </div>
))}
{/* <button className="border rounded-full border-green-500 border-4 text-green-500 font-bold px-2">
+ +
</button> */} </button> */}
</div>
<div className="flex w-full flex-row justify-end py-2">
<button className="w-1/12">필수</button>
<button className="w-1/12">옵션</button>
<button className="w-1/12">삭제</button>
</div>
</div> </div>
<div className="flex w-full flex-row justify-end py-2"> );
<button className="w-1/12">필수</button> };
<button className="w-1/12">옵션</button>
<button className="w-1/12">삭제</button>
</div>
</div>
);
import React from "react"; import React from "react";
import { RatingType } from "./CreateSurveyFormPage"; import { RatingType } from "./CreateSurveyFormPage";
import { useQuestion } from "./question.context";
type Props = { type Props = {
element: RatingType; element: RatingType;
QuestionListChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
// deleteValue: (e: React.MouseEvent<HTMLButtonElement>) => void; // deleteValue: (e: React.MouseEvent<HTMLButtonElement>) => void;
}; };
export const QRating = ({ element, QuestionListChange }: Props) => { export const QRating = ({ element }: Props) => {
const { questionListChange } = useQuestion();
return ( return (
<div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2"> <div className="flex flex-col container w-4/5 h-auto border-2 border-themeColor items-center m-3 py-2">
<div className="flex h-16 w-full place-content-between items-center"> <div className="flex h-16 w-full place-content-between items-center">
<input <input
type="text" type="text"
name="title" name="title"
id={element.id} id={element._id}
className="text-xl font-bold ml-6 border-b-2 w-1/2" className="text-xl font-bold ml-6 border-b-2 w-1/2"
placeholder={element.title} placeholder={element.title}
onChange={QuestionListChange} onChange={questionListChange}
></input> ></input>
<select <select
id={element.id} id={element._id}
className="w-36 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-themeColor w-full mr-3 p-2.5" className="w-36 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-themeColor w-full mr-3 p-2.5"
> >
<option>질문종류</option> <option>질문종류</option>
...@@ -40,16 +42,16 @@ export const QRating = ({ element, QuestionListChange }: Props) => { ...@@ -40,16 +42,16 @@ export const QRating = ({ element, QuestionListChange }: Props) => {
<input <input
type="text" type="text"
name="comment" name="comment"
id={element.id} id={element._id}
className="border w-11/12" className="border w-11/12"
placeholder="질문에 대한 설명을 입력해주세요" placeholder="질문에 대한 설명을 입력해주세요"
onChange={QuestionListChange} onChange={questionListChange}
></input> ></input>
</div> </div>
<div className="flex place-content-between items-center p-5"> <div className="flex place-content-between items-center p-5">
<input <input
name="minRateDescription" name="minRateDescription"
id={element.id} id={element._id}
className="border-b-2 text-center" className="border-b-2 text-center"
size={10} size={10}
placeholder={element.content.minRateDescription} placeholder={element.content.minRateDescription}
...@@ -57,7 +59,7 @@ export const QRating = ({ element, QuestionListChange }: Props) => { ...@@ -57,7 +59,7 @@ export const QRating = ({ element, QuestionListChange }: Props) => {
{element.content.rateValues.map((e) => ( {element.content.rateValues.map((e) => (
<input <input
name="text" name="text"
id={element.id} id={element._id}
type="text" type="text"
className="border border-black rounded-full py-1 m-2 text-center" className="border border-black rounded-full py-1 m-2 text-center"
size={1} size={1}
...@@ -66,7 +68,7 @@ export const QRating = ({ element, QuestionListChange }: Props) => { ...@@ -66,7 +68,7 @@ export const QRating = ({ element, QuestionListChange }: Props) => {
))} ))}
<input <input
name="maxRateDescription" name="maxRateDescription"
id={element.id} id={element._id}
className="border-b-2 text-center" className="border-b-2 text-center"
size={10} size={10}
placeholder={element.content.maxRateDescription} placeholder={element.content.maxRateDescription}
...@@ -76,7 +78,7 @@ export const QRating = ({ element, QuestionListChange }: Props) => { ...@@ -76,7 +78,7 @@ export const QRating = ({ element, QuestionListChange }: Props) => {
<button <button
// type="button" // type="button"
name="rateValues" name="rateValues"
id={element.id} id={element._id}
className="border border-red-500 rounded mx-2 px-2" className="border border-red-500 rounded mx-2 px-2"
// onClick={deleteValue} // onClick={deleteValue}
> >
......
import React, { useState } from "react"; import React, { useState } from "react";
import { BasicQuestionType } from "./CreateSurveyFormPage";
import { QEssay } from "./QEssay"; import { QEssay } from "./QEssay";
import { QCheckbox } from "./QCheckbox"; import { QCheckbox } from "./QCheckbox";
import { QRadio } from "./QRadio"; import { QRadio } from "./QRadio";
import { QDropdown } from "./QDropdown"; import { QDropdown } from "./QDropdown";
import { QFile } from "./QFile"; import { QFile } from "./QFile";
import { QRating } from "./QRating"; import { QRating } from "./QRating";
import { useQuestion } from "./question.context";
type Props = { type Props = {};
questionList: BasicQuestionType[];
QuestionListChange: (e: React.ChangeEvent<HTMLInputElement>) => void; export const Question = ({}: Props) => {
addQuestion: (event: React.MouseEvent<HTMLButtonElement>) => void; const { addQuestion, questionList, currentId } = useQuestion();
changeCurrentId: (event: React.MouseEvent<HTMLButtonElement>) => void;
};
export const Question = ({
questionList,
QuestionListChange,
addQuestion,
changeCurrentId,
}: Props) => {
return ( return (
<> <>
{console.log(questionList)} {console.log(questionList, currentId)}
{questionList.map((element) => { {questionList.map((element) => {
switch (element.type) { switch (element.type) {
case "essay": case "essay":
return ( return <QEssay element={element} />;
<QEssay
element={element}
QuestionListChange={QuestionListChange}
changeCurrentId={changeCurrentId}
/>
);
case "radio": case "radio":
return ( return <QRadio element={element} />;
<QRadio
element={element}
QuestionListChange={QuestionListChange}
/>
);
case "checkbox": case "checkbox":
return ( return <QCheckbox element={element} />;
<QCheckbox
element={element}
QuestionListChange={QuestionListChange}
/>
);
case "dropdown": case "dropdown":
return ( return <QDropdown element={element} />;
<QDropdown
element={element}
QuestionListChange={QuestionListChange}
/>
);
case "file": case "file":
return ( return <QFile element={element} />;
<QFile
element={element}
QuestionListChange={QuestionListChange}
/>
);
case "rating": case "rating":
return ( return <QRating element={element} />;
<QRating
element={element}
QuestionListChange={QuestionListChange}
/>
);
default: default:
break; break;
} }
......
import React, {
createContext,
FC,
ReactNode,
useContext,
useState,
} from "react";
import axios from "axios";
import { BasicQuestionType } from "./CreateSurveyFormPage";
interface IQuestionContext {
questionListChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
questionList: BasicQuestionType[];
editClick: (e: React.MouseEvent<HTMLButtonElement>) => void;
currentId: string;
addQuestion: (e: React.MouseEvent<HTMLButtonElement>) => Promise<void>;
}
const QuestionContext = createContext<IQuestionContext>({
questionListChange: () => {},
questionList: [],
editClick: () => {},
currentId: "",
addQuestion: async () => {},
});
export const QuestionProvider: FC<{ children: ReactNode }> = ({ children }) => {
const [questionList, setQuestionList] = useState<Array<BasicQuestionType>>(
[]
);
const [currentId, setCurrentId] = useState<string>("");
function questionListChange(e: React.ChangeEvent<HTMLInputElement>): void {
const newList: BasicQuestionType[] = [...questionList];
const obj: any = newList.find((a) => a._id === e.target.id); //고유 _id로 질문찾기
const targetKey: any = e.target.name;
obj[targetKey] = e.target.value;
setQuestionList(newList);
}
async function addQuestion(e: React.MouseEvent<HTMLButtonElement>) {
try {
const res = await axios.post("/api/questions/create", {
type: "essay",
title: "Question Title",
isRequired: false,
comment: "질문에 대한 설명을 입력해주세요",
content: null,
});
console.log(res.data);
setQuestionList([...questionList, res.data]);
// setSuccess(true);
// setError("");
} catch (error) {
console.log("에러발생");
// catchErrors(error, setError)
} finally {
// setLoading(false);
}
}
function editClick(e: React.MouseEvent<HTMLButtonElement>) {
setCurrentId(e.currentTarget.id);
}
return (
<QuestionContext.Provider
value={{
questionListChange,
addQuestion,
questionList,
editClick,
currentId,
}}
>
{children}
</QuestionContext.Provider>
);
};
export const useQuestion = () => useContext(QuestionContext);
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Webpack TypeScript React TailwindCSS Template</title> <title>Simple Survey Form</title>
</head> </head>
<body> <body>
<div id="root"></div> <div id="root"></div>
......
...@@ -5,5 +5,5 @@ export const createQuestion = asyncWrap(async (req, res) => { ...@@ -5,5 +5,5 @@ export const createQuestion = asyncWrap(async (req, res) => {
const question = req.body; const question = req.body;
console.log("question body", question); console.log("question body", question);
const newQuestion = await questionDb.createQuestion(question); const newQuestion = await questionDb.createQuestion(question);
return res.json(question); return res.json(newQuestion);
}); });
...@@ -2,5 +2,13 @@ import { Question, IQuestion } from "../models"; ...@@ -2,5 +2,13 @@ import { Question, IQuestion } from "../models";
export const createQuestion = async (question: IQuestion) => { export const createQuestion = async (question: IQuestion) => {
const newQuestion = await Question.create(question); const newQuestion = await Question.create(question);
return newQuestion; const newQ = {
_id: newQuestion._id,
type: newQuestion.type,
title: newQuestion.title,
isRequired: newQuestion.isRequired,
comment: newQuestion.comment,
content: newQuestion.content,
}
return newQ;
}; };
...@@ -2,7 +2,7 @@ import { model, Schema, Types } from "mongoose"; ...@@ -2,7 +2,7 @@ import { model, Schema, Types } from "mongoose";
export interface IQuestion { export interface IQuestion {
type: string; type: string;
id: string; // id: string;
title?: string; title?: string;
isRequired: boolean; isRequired: boolean;
comment?: string; comment?: string;
...@@ -10,7 +10,7 @@ export interface IQuestion { ...@@ -10,7 +10,7 @@ export interface IQuestion {
} }
const schema = new Schema<IQuestion>({ const schema = new Schema<IQuestion>({
id: {type:String}, // id: {type:String},
type:{type:String}, type:{type:String},
title: {type:String}, title: {type:String},
isRequired: {type:Boolean}, isRequired: {type:Boolean},
......
...@@ -7,6 +7,6 @@ const router = express.Router(); ...@@ -7,6 +7,6 @@ const router = express.Router();
router.use("/users", userRouter); router.use("/users", userRouter);
router.use("/auth", authRouter); router.use("/auth", authRouter);
router.use("/question", questionRouter) router.use("/questions", questionRouter)
export default router; export default router;
...@@ -5,7 +5,6 @@ const router = express.Router(); ...@@ -5,7 +5,6 @@ const router = express.Router();
router router
.route("/create") .route("/create")
.get()
.post(questionCtrl.createQuestion); .post(questionCtrl.createQuestion);
export default router; export default router;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment