Commit de41c36d authored by Jiwon Yoon's avatar Jiwon Yoon
Browse files

createQuestion, Profile UI 수정

parent 8b8c6f9c
...@@ -17,7 +17,7 @@ export const SurveyRouter = () => { ...@@ -17,7 +17,7 @@ export const SurveyRouter = () => {
<Route path="login" element={<Login />} /> <Route path="login" element={<Login />} />
<Route path="signup" element={<SignUp />} /> <Route path="signup" element={<SignUp />} />
<Route path="surveys/edit/:surveyId" element={<EditSurvey />} /> <Route path="surveys/edit/:surveyId" element={<EditSurvey />} />
<Route path="survey" element={<SurveyForm />} /> <Route path="surveys/:surveyId" element={<SurveyForm />} />
<Route <Route
path="profile" path="profile"
element={ element={
......
...@@ -2,14 +2,17 @@ import axios from "axios"; ...@@ -2,14 +2,17 @@ import axios from "axios";
import { BasicQuestionType } from "../types"; import { BasicQuestionType } from "../types";
import baseUrl from "./baseUrl"; import baseUrl from "./baseUrl";
export const createQuestion = async () => { export const createQuestion = async (surveyId: string) => {
const { data } = await axios.post(`${baseUrl}/questions/create`, { const { data } = await axios.post(
`${baseUrl}/surveys/${surveyId}/questions`,
{
type: "essay", type: "essay",
title: "", title: "",
isRequired: false, isRequired: false,
comment: "", comment: "",
content: { choices: [] }, content: { choices: [] },
}); }
);
return data; return data;
}; };
......
...@@ -11,6 +11,10 @@ export const getSurvey = async (surveyId: string) => { ...@@ -11,6 +11,10 @@ export const getSurvey = async (surveyId: string) => {
const { data } = await axios.get(`${baseUrl}/surveys/edit/${surveyId}`); const { data } = await axios.get(`${baseUrl}/surveys/edit/${surveyId}`);
return data; return data;
}; };
export const getASurvey = async (surveyId: string) => {
const { data } = await axios.get(`${baseUrl}/surveys/edit/${surveyId}`);
return data;
};
//동혁 //동혁
export const getSurveys = async () => { export const getSurveys = async () => {
const { data } = await axios.get(`${baseUrl}/surveys/`); const { data } = await axios.get(`${baseUrl}/surveys/`);
......
import React, { InputHTMLAttributes } from "react"; import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { surveyApi } from "../apis";
import { Question } from "../questions";
import { SurveyType } from "../types";
import { ACheckboxForm } from "./ACheckbox"; import { ACheckboxForm } from "./ACheckbox";
import { ADropdownForm } from "./ADropdown"; import { ADropdownForm } from "./ADropdown";
import { AEssayForm } from "./AEssayForm"; import { AEssayForm } from "./AEssayForm";
import { ARadioForm } from "./ARadioForm"; import { ARadioForm } from "./ARadioForm";
export const SurveyForm = () => { export const SurveyForm = () => {
let { surveyId } = useParams<{ surveyId: string }>();
const [survey, setSurvey] = useState<SurveyType>({
_id: surveyId,
user: {},
title: "",
comment: "",
questions: [],
});
useEffect(() => {
getSurvey();
}, [surveyId]);
async function getSurvey() {
try {
if (surveyId) {
const thisSurvey: SurveyType = await surveyApi.getASurvey(surveyId);
console.log(thisSurvey);
setSurvey(thisSurvey);
// setSuccess(true);
// setError("");
} else {
// setLoading(true);
}
} catch (error) {
// catchErrors(error, setError);
} finally {
// setLoading(false);
}
}
return ( return (
<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">
...@@ -15,10 +47,6 @@ export const SurveyForm = () => { ...@@ -15,10 +47,6 @@ export const SurveyForm = () => {
rows={2} rows={2}
cols={60} cols={60}
></textarea> ></textarea>
{/* <ACheckboxForm></ACheckboxForm> */}
<ADropdownForm></ADropdownForm>
<AEssayForm></AEssayForm>
<ARadioForm></ARadioForm>
<div> <div>
<button className="rounded bg-themeColor my-5 py-2 px-5 font-bold text-white"> <button className="rounded bg-themeColor my-5 py-2 px-5 font-bold text-white">
제출하기 제출하기
......
...@@ -20,6 +20,12 @@ export const MySurveyCard = ({ data }: Props) => { ...@@ -20,6 +20,12 @@ export const MySurveyCard = ({ data }: Props) => {
}); });
}; };
const goSurvey = () => {
navigate(`/surveys/${data._id}`, {
replace: true,
});
};
async function deleteSurvey() { async function deleteSurvey() {
try { try {
if (data._id) { if (data._id) {
...@@ -47,9 +53,11 @@ export const MySurveyCard = ({ data }: Props) => { ...@@ -47,9 +53,11 @@ export const MySurveyCard = ({ data }: Props) => {
</div> </div>
<div className="flex flex-col px-5 py-3"> <div className="flex flex-col px-5 py-3">
<div className="h-12"> <div className="h-12">
<button type="button" onClick={goSurvey}>
<p className="font-bold"> <p className="font-bold">
{data.title ? data.title : "제목없는 설문조사"} {data.title ? data.title : "제목없는 설문조사"}
</p> </p>
</button>
<p className="text-gray-500 text-sm"> <p className="text-gray-500 text-sm">
{data.updatedAt?.substring(0, 10)} {data.updatedAt?.substring(0, 10)}
</p> </p>
......
...@@ -36,7 +36,7 @@ export const Profile = () => { ...@@ -36,7 +36,7 @@ export const Profile = () => {
<div className="flex flex-col items-center"> <div className="flex flex-col items-center">
<div className="mt-10 text-xl font-bold">나의 설문조사</div> <div className="mt-10 text-xl font-bold">나의 설문조사</div>
<img src={`${baseImageUrl}/9e24ad36a2947b08c89913b01`} /> <img src={`${baseImageUrl}/9e24ad36a2947b08c89913b01`} />
<div className="flex space-x-4 mt-6"> <div className="grid grid-cols-1 md:grid-cols-4 sm:grid-cols-2 gap-4 mt-6">
<button <button
onClick={createSurvey} onClick={createSurvey}
className="flex h-60 w-52 items-center border-2 border-themeColor font-bold bg-gray-200 hover:bg-themeColor rounded-lg " className="flex h-60 w-52 items-center border-2 border-themeColor font-bold bg-gray-200 hover:bg-themeColor rounded-lg "
......
...@@ -4,10 +4,10 @@ import { CheckboxType } from "../types"; ...@@ -4,10 +4,10 @@ import { CheckboxType } from "../types";
type Props = { type Props = {
element: CheckboxType; element: CheckboxType;
handleQuestion: (id: string) => void; handleQuestion: (id: string) => void;
currentId: string; save: boolean;
}; };
export const CheckboxForm = ({ element, handleQuestion, currentId }: Props) => { export const CheckboxForm = ({ element, handleQuestion, save }: Props) => {
const [choices, setChoices] = useState([...element.content.choices]); const [choices, setChoices] = useState([...element.content.choices]);
function handleContent(event: React.ChangeEvent<HTMLInputElement>) { function handleContent(event: React.ChangeEvent<HTMLInputElement>) {
...@@ -41,7 +41,7 @@ export const CheckboxForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -41,7 +41,7 @@ export const CheckboxForm = ({ element, handleQuestion, currentId }: Props) => {
placeholder="선택지" placeholder="선택지"
value={choice.text} value={choice.text}
onChange={handleContent} onChange={handleContent}
disabled={currentId !== element._id} disabled={save}
></input> ></input>
</div> </div>
))} ))}
...@@ -52,7 +52,7 @@ export const CheckboxForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -52,7 +52,7 @@ export const CheckboxForm = ({ element, handleQuestion, currentId }: Props) => {
name="rateValues" name="rateValues"
className="border border-red-500 rounded mx-2 px-2" className="border border-red-500 rounded mx-2 px-2"
onClick={deleteValue} onClick={deleteValue}
disabled={currentId !== element._id} disabled={save}
> >
삭제 삭제
</button> </button>
...@@ -61,7 +61,7 @@ export const CheckboxForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -61,7 +61,7 @@ export const CheckboxForm = ({ element, handleQuestion, currentId }: Props) => {
name="rateValues" name="rateValues"
className="border border-blue-500 rounded mx-2 px-2" className="border border-blue-500 rounded mx-2 px-2"
onClick={addValue} onClick={addValue}
disabled={currentId !== element._id} disabled={save}
> >
추가 추가
</button> </button>
......
import React from "react";
// import { DateType } from "../types";
type Props = {
// element: DateType;
// save: boolean;
};
export const DateForm = ({}: Props) => {
return (
<div id="content" className="flex mt-4 w-full justify-center">
<input type="date" className="w-11/12" disabled></input>
</div>
);
};
...@@ -4,10 +4,10 @@ import { DropdownType } from "../types"; ...@@ -4,10 +4,10 @@ import { DropdownType } from "../types";
type Props = { type Props = {
element: DropdownType; element: DropdownType;
handleQuestion: (id: string) => void; handleQuestion: (id: string) => void;
currentId: string; save: boolean;
}; };
export const DropdownForm = ({ element, handleQuestion, currentId }: Props) => { export const DropdownForm = ({ element, handleQuestion, save }: Props) => {
const [choices, setChoices] = useState([...element.content.choices]); const [choices, setChoices] = useState([...element.content.choices]);
function handleContent(event: React.ChangeEvent<HTMLInputElement>) { function handleContent(event: React.ChangeEvent<HTMLInputElement>) {
...@@ -45,7 +45,7 @@ export const DropdownForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -45,7 +45,7 @@ export const DropdownForm = ({ element, handleQuestion, currentId }: Props) => {
placeholder="선택지" placeholder="선택지"
value={choice.text} value={choice.text}
onChange={handleContent} onChange={handleContent}
disabled={currentId !== element._id} disabled={save}
></input> ></input>
</div> </div>
))} ))}
...@@ -56,7 +56,7 @@ export const DropdownForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -56,7 +56,7 @@ export const DropdownForm = ({ element, handleQuestion, currentId }: Props) => {
name="rateValues" name="rateValues"
className="border border-red-500 rounded mx-2 px-2" className="border border-red-500 rounded mx-2 px-2"
onClick={deleteValue} onClick={deleteValue}
disabled={currentId !== element._id} disabled={save}
> >
삭제 삭제
</button> </button>
...@@ -65,7 +65,7 @@ export const DropdownForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -65,7 +65,7 @@ export const DropdownForm = ({ element, handleQuestion, currentId }: Props) => {
name="rateValues" name="rateValues"
className="border border-blue-500 rounded mx-2 px-2" className="border border-blue-500 rounded mx-2 px-2"
onClick={addValue} onClick={addValue}
disabled={currentId !== element._id} disabled={save}
> >
추가 추가
</button> </button>
......
...@@ -3,10 +3,10 @@ import { EssayType } from "../types"; ...@@ -3,10 +3,10 @@ import { EssayType } from "../types";
type Props = { type Props = {
element: EssayType; element: EssayType;
currentId: string; save: boolean;
}; };
export const EssayForm = ({ element, currentId }: Props) => { export const EssayForm = ({ element, save }: Props) => {
return ( return (
<div id="commentarea" className="flex mt-4 w-full justify-center"> <div id="commentarea" className="flex mt-4 w-full justify-center">
<input className="border w-11/12 h-16" disabled></input> <input className="border w-11/12 h-16" disabled></input>
......
import React, { useState } from "react"; import React from "react";
import { FileType } from "../types"; import { FileType } from "../types";
type Props = { type Props = {
element: FileType; element: FileType;
currentId: string; save: boolean;
}; };
export const FileForm = ({ element, currentId }: Props) => { export const FileForm = ({ element, save }: Props) => {
return ( return (
<div id="content" className="flex mt-4 w-full justify-center"> <div id="content" className="flex mt-4 w-full justify-center">
<input type="file" className=" w-11/12 h-16" disabled></input> <input type="file" className=" w-11/12 h-16" disabled></input>
......
...@@ -7,13 +7,12 @@ import { RadioForm } from "./RadioForm"; ...@@ -7,13 +7,12 @@ import { RadioForm } from "./RadioForm";
import { DropdownForm } from "./DropdownForm"; import { DropdownForm } from "./DropdownForm";
import { FileForm } from "./FileForm"; import { FileForm } from "./FileForm";
import { RatingForm } from "./RatingForm"; import { RatingForm } from "./RatingForm";
import { DateForm } from "./DateForm";
type Props = { type Props = {
element: BasicQuestionType; element: BasicQuestionType;
handleQuestion: (id: string) => void; handleQuestion: (id: string) => void;
deleteQuestion: (id: string) => void; deleteQuestion: (id: string) => void;
changeCurrentId: (id: string) => void;
currentId: string;
}; };
const typeDropDown = new Map([ const typeDropDown = new Map([
...@@ -31,16 +30,15 @@ export const Question = ({ ...@@ -31,16 +30,15 @@ export const Question = ({
element, element,
handleQuestion, handleQuestion,
deleteQuestion, deleteQuestion,
changeCurrentId,
currentId,
}: Props) => { }: Props) => {
const [save, setSave] = useState(true);
async function handleEditComplete() { async function handleEditComplete() {
try { try {
const newQuestion: BasicQuestionType = await questionApi.updateQuestion( const newQuestion: BasicQuestionType = await questionApi.updateQuestion(
element element
); );
console.log(newQuestion); console.log(newQuestion);
changeCurrentId(""); setSave(true);
// setSuccess(true); // setSuccess(true);
// setError(""); // setError("");
} catch (error) { } catch (error) {
...@@ -92,13 +90,13 @@ export const Question = ({ ...@@ -92,13 +90,13 @@ export const Question = ({
function getContent(element: BasicQuestionType) { function getContent(element: BasicQuestionType) {
switch (element.type) { switch (element.type) {
case "essay": case "essay":
return <EssayForm element={element} currentId={currentId} />; return <EssayForm element={element} save={save} />;
case "radio": case "radio":
return ( return (
<RadioForm <RadioForm
handleQuestion={handleQuestion} handleQuestion={handleQuestion}
element={element} element={element}
currentId={currentId} save={save}
/> />
); );
case "checkbox": case "checkbox":
...@@ -106,7 +104,7 @@ export const Question = ({ ...@@ -106,7 +104,7 @@ export const Question = ({
<CheckboxForm <CheckboxForm
handleQuestion={handleQuestion} handleQuestion={handleQuestion}
element={element} element={element}
currentId={currentId} save={save}
/> />
); );
case "dropdown": case "dropdown":
...@@ -114,19 +112,21 @@ export const Question = ({ ...@@ -114,19 +112,21 @@ export const Question = ({
<DropdownForm <DropdownForm
handleQuestion={handleQuestion} handleQuestion={handleQuestion}
element={element} element={element}
currentId={currentId} save={save}
/> />
); );
case "file": case "file":
return <FileForm element={element} currentId={currentId} />; return <FileForm element={element} save={save} />;
case "rating": case "rating":
return ( return (
<RatingForm <RatingForm
handleQuestion={handleQuestion} handleQuestion={handleQuestion}
element={element} element={element}
currentId={currentId} save={save}
/> />
); );
case "date":
return <DateForm />;
default: default:
return <></>; return <></>;
} }
...@@ -140,11 +140,11 @@ export const Question = ({ ...@@ -140,11 +140,11 @@ export const Question = ({
deleteQuestion(element._id); deleteQuestion(element._id);
}; };
const handleEditClick = () => { const handleEditClick = () => {
changeCurrentId(element._id); setSave(false);
}; };
return ( return (
<div <div
style={{ borderColor: currentId === element._id ? "red" : "#58ACFA" }} style={{ borderColor: save ? "#58ACFA" : "red" }}
className="flex flex-col container w-4/5 h-auto border-2 items-center m-3 py-2" className="flex flex-col container w-4/5 h-auto border-2 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">
...@@ -156,7 +156,7 @@ export const Question = ({ ...@@ -156,7 +156,7 @@ export const Question = ({
placeholder={"Question Title"} placeholder={"Question Title"}
value={element.title} value={element.title}
onChange={handleQuestionInfo} onChange={handleQuestionInfo}
disabled={currentId !== element._id} disabled={save}
></input> ></input>
<select <select
id={element._id} id={element._id}
...@@ -184,7 +184,7 @@ export const Question = ({ ...@@ -184,7 +184,7 @@ export const Question = ({
placeholder="질문에 대한 설명을 입력해주세요" placeholder="질문에 대한 설명을 입력해주세요"
value={element.comment} value={element.comment}
onChange={handleQuestionInfo} onChange={handleQuestionInfo}
disabled={currentId !== element._id} disabled={save}
></input> ></input>
</div> </div>
{getContent(element)} {getContent(element)}
...@@ -195,7 +195,7 @@ export const Question = ({ ...@@ -195,7 +195,7 @@ export const Question = ({
id="isRequired" id="isRequired"
value="isRequired" value="isRequired"
onChange={handleRequired} onChange={handleRequired}
disabled={currentId !== element._id} disabled={save}
checked={element.isRequired} checked={element.isRequired}
/> />
<label htmlFor="isRequired" className="px-1"> <label htmlFor="isRequired" className="px-1">
...@@ -204,14 +204,14 @@ export const Question = ({ ...@@ -204,14 +204,14 @@ export const Question = ({
<button type="button" className="px-1" onClick={handleDelete}> <button type="button" className="px-1" onClick={handleDelete}>
삭제 삭제
</button> </button>
{currentId === element._id ? ( {save ? (
<button type="button" className="px-1" onClick={handleEditComplete}>
수정완료
</button>
) : (
<button type="button" className="px-1" onClick={handleEditClick}> <button type="button" className="px-1" onClick={handleEditClick}>
수정하기 수정하기
</button> </button>
) : (
<button type="button" className="px-1" onClick={handleEditComplete}>
수정완료
</button>
)} )}
</div> </div>
</div> </div>
......
...@@ -4,10 +4,10 @@ import { RadioType } from "../types"; ...@@ -4,10 +4,10 @@ import { RadioType } from "../types";
type Props = { type Props = {
element: RadioType; element: RadioType;
handleQuestion: (id: string) => void; handleQuestion: (id: string) => void;
currentId: string; save: boolean;
}; };
export const RadioForm = ({ element, handleQuestion, currentId }: Props) => { export const RadioForm = ({ element, handleQuestion, save }: Props) => {
const [choices, setChoices] = useState([...element.content.choices]); const [choices, setChoices] = useState([...element.content.choices]);
function handleContent(event: React.ChangeEvent<HTMLInputElement>) { function handleContent(event: React.ChangeEvent<HTMLInputElement>) {
...@@ -42,7 +42,7 @@ export const RadioForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -42,7 +42,7 @@ export const RadioForm = ({ element, handleQuestion, currentId }: Props) => {
placeholder="선택지" placeholder="선택지"
value={choice.text} value={choice.text}
onChange={handleContent} onChange={handleContent}
disabled={currentId !== element._id} disabled={save}
></input> ></input>
</div> </div>
))} ))}
...@@ -53,7 +53,7 @@ export const RadioForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -53,7 +53,7 @@ export const RadioForm = ({ element, handleQuestion, currentId }: Props) => {
name="rateValues" name="rateValues"
className="border border-red-500 rounded mx-2 px-2" className="border border-red-500 rounded mx-2 px-2"
onClick={deleteValue} onClick={deleteValue}
disabled={currentId !== element._id} disabled={save}
> >
삭제 삭제
</button> </button>
...@@ -62,7 +62,7 @@ export const RadioForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -62,7 +62,7 @@ export const RadioForm = ({ element, handleQuestion, currentId }: Props) => {
name="rateValues" name="rateValues"
className="border border-blue-500 rounded mx-2 px-2" className="border border-blue-500 rounded mx-2 px-2"
onClick={addValue} onClick={addValue}
disabled={currentId !== element._id} disabled={save}
> >
추가 추가
</button> </button>
......
...@@ -4,10 +4,10 @@ import { RatingType } from "../types"; ...@@ -4,10 +4,10 @@ import { RatingType } from "../types";
type Props = { type Props = {
element: RatingType; element: RatingType;
handleQuestion: (id: string) => void; handleQuestion: (id: string) => void;
currentId: string; save: boolean;
}; };
export const RatingForm = ({ element, handleQuestion, currentId }: Props) => { export const RatingForm = ({ element, handleQuestion, save }: Props) => {
const [choices, setChoices] = useState([...element.content.choices]); const [choices, setChoices] = useState([...element.content.choices]);
function handleContent(event: React.ChangeEvent<HTMLInputElement>) { function handleContent(event: React.ChangeEvent<HTMLInputElement>) {
...@@ -46,7 +46,7 @@ export const RatingForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -46,7 +46,7 @@ export const RatingForm = ({ element, handleQuestion, currentId }: Props) => {
placeholder="비동의" placeholder="비동의"
value={element.content.minRateDescription} value={element.content.minRateDescription}
onChange={handleContent} onChange={handleContent}
disabled={currentId !== element._id} disabled={save}
></input> ></input>
{choices.map((choice: any, index: number) => ( {choices.map((choice: any, index: number) => (
<input <input
...@@ -58,7 +58,7 @@ export const RatingForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -58,7 +58,7 @@ export const RatingForm = ({ element, handleQuestion, currentId }: Props) => {
placeholder="0" placeholder="0"
value={choice.text} value={choice.text}
onChange={handleContent} onChange={handleContent}
disabled={currentId !== element._id} disabled={save}
></input> ></input>
))} ))}
<input <input
...@@ -68,7 +68,7 @@ export const RatingForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -68,7 +68,7 @@ export const RatingForm = ({ element, handleQuestion, currentId }: Props) => {
placeholder="동의" placeholder="동의"
value={element.content.maxRateDescription} value={element.content.maxRateDescription}
onChange={handleContent} onChange={handleContent}
disabled={currentId !== element._id} disabled={save}
></input> ></input>
</div> </div>
<div> <div>
...@@ -77,7 +77,7 @@ export const RatingForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -77,7 +77,7 @@ export const RatingForm = ({ element, handleQuestion, currentId }: Props) => {
name="rateValues" name="rateValues"
className="border border-red-500 rounded mx-2 px-2" className="border border-red-500 rounded mx-2 px-2"
onClick={deleteValue} onClick={deleteValue}
disabled={currentId !== element._id} disabled={save}
> >
삭제 삭제
</button> </button>
...@@ -86,7 +86,7 @@ export const RatingForm = ({ element, handleQuestion, currentId }: Props) => { ...@@ -86,7 +86,7 @@ export const RatingForm = ({ element, handleQuestion, currentId }: Props) => {
name="rateValues" name="rateValues"
className="border border-blue-500 rounded mx-2 px-2" className="border border-blue-500 rounded mx-2 px-2"
onClick={addValue} onClick={addValue}
disabled={currentId !== element._id} disabled={save}
> >
추가 추가
</button> </button>
......
import React, { FormEvent, useEffect, useState } from "react"; import React, { FormEvent, useEffect, useState } from "react";
import { useParams, useNavigate } from "react-router-dom"; import { useParams } from "react-router-dom";
import { questionApi, surveyApi } from "../apis"; import { questionApi, surveyApi } from "../apis";
import { SpinnerIcon } from "../icons"; import { SpinnerIcon } from "../icons";
import { Question } from "../questions"; import { Question } from "../questions";
...@@ -8,7 +8,6 @@ import { catchErrors } from "../helpers"; ...@@ -8,7 +8,6 @@ import { catchErrors } from "../helpers";
export const EditSurvey = () => { export const EditSurvey = () => {
let { surveyId } = useParams<{ surveyId: string }>(); let { surveyId } = useParams<{ surveyId: string }>();
const navigate = useNavigate();
useEffect(() => { useEffect(() => {
getSurvey(); getSurvey();
}, [surveyId]); }, [surveyId]);
...@@ -22,10 +21,6 @@ export const EditSurvey = () => { ...@@ -22,10 +21,6 @@ export const EditSurvey = () => {
comment: "", comment: "",
questions: [], questions: [],
}); });
const [currentId, setCurrentId] = useState("");
const changeCurrentId = (id: string) => {
setCurrentId(id);
};
async function getSurvey() { async function getSurvey() {
try { try {
if (surveyId) { if (surveyId) {
...@@ -38,9 +33,6 @@ export const EditSurvey = () => { ...@@ -38,9 +33,6 @@ export const EditSurvey = () => {
} }
} catch (error) { } catch (error) {
catchErrors(error, setError); catchErrors(error, setError);
// navigate(`/`, {
// replace: false,
// });
} finally { } finally {
setLoading(false); setLoading(false);
} }
...@@ -72,10 +64,17 @@ export const EditSurvey = () => { ...@@ -72,10 +64,17 @@ export const EditSurvey = () => {
async function addQuestion() { async function addQuestion() {
try { try {
const newQuestion: BasicQuestionType = await questionApi.createQuestion(); if (surveyId) {
setSurvey({ ...survey, questions: [...survey.questions, newQuestion] }); const questions: BasicQuestionType[] = await questionApi.createQuestion(
surveyId
);
console.log(questions);
setSurvey({ ...survey, questions: questions });
setSuccess(true); setSuccess(true);
setError(""); setError("");
} else {
setLoading(true);
}
} catch (error) { } catch (error) {
catchErrors(error, setError); catchErrors(error, setError);
} finally { } finally {
...@@ -133,8 +132,6 @@ export const EditSurvey = () => { ...@@ -133,8 +132,6 @@ export const EditSurvey = () => {
element={question} element={question}
handleQuestion={handleQuestion} handleQuestion={handleQuestion}
deleteQuestion={deleteQuestion} deleteQuestion={deleteQuestion}
changeCurrentId={changeCurrentId}
currentId={currentId}
/> />
))} ))}
<div className="flex w-4/5 content-center justify-center border-2 border-black h-8 mt-3"> <div className="flex w-4/5 content-center justify-center border-2 border-black h-8 mt-3">
......
import { NextFunction, Request, Response } from "express"; import { NextFunction, Request, Response } from "express";
import { questionDb } from "../db"; import { questionDb, surveyDb } from "../db";
import { asyncWrap } from "../helpers/asyncWrap"; import { asyncWrap } from "../helpers/asyncWrap";
export interface TypedRequestAuth<T> extends Request { export interface TypedRequestAuth<T> extends Request {
...@@ -9,13 +9,28 @@ export interface TypedRequestAuth<T> extends Request { ...@@ -9,13 +9,28 @@ export interface TypedRequestAuth<T> extends Request {
export const createQuestion = asyncWrap( export const createQuestion = asyncWrap(
async (reqExp: Request, res: Response, next: NextFunction) => { async (reqExp: Request, res: Response, next: NextFunction) => {
try {
const req = reqExp as TypedRequestAuth<{ userId: string }>; const req = reqExp as TypedRequestAuth<{ userId: string }>;
const { userId } = req.auth; const { userId } = req.auth;
if (!userId) {
return res.status(404).send("올바른 접근이 아닙니다");
} else {
let question = req.body; let question = req.body;
question.user = userId; question.user = userId;
console.log("question body", question);
const newQuestion = await questionDb.createQuestion(question); const newQuestion = await questionDb.createQuestion(question);
return res.json(newQuestion); const { surveyId } = req.params;
const updatedSurvey = await surveyDb.putNewQuestion(
newQuestion,
surveyId
);
console.log(updatedSurvey);
return res.json(updatedSurvey?.questions);
}
} catch (error: any) {
return res
.status(500)
.send(error.message || "질문을 생성하는 중 오류 발생");
}
} }
); );
......
...@@ -26,7 +26,6 @@ export const getSurveyById = asyncWrap(async (req, res) => { ...@@ -26,7 +26,6 @@ export const getSurveyById = asyncWrap(async (req, res) => {
return res.json(survey); return res.json(survey);
}); });
//동혁
export const getSurveys = asyncWrap(async (reqExp: Request, res: Response) => { export const getSurveys = asyncWrap(async (reqExp: Request, res: Response) => {
const req = reqExp as TypedRequestAuth<{ userId: string }>; const req = reqExp as TypedRequestAuth<{ userId: string }>;
const { userId } = req.auth; const { userId } = req.auth;
......
...@@ -22,7 +22,7 @@ export const getSurveyById = async (surveyId: string) => { ...@@ -22,7 +22,7 @@ export const getSurveyById = async (surveyId: string) => {
}; };
export const getSurveys = async (userId: string) => { export const getSurveys = async (userId: string) => {
const surveys = await Survey.find({ user: userId }); const surveys = await Survey.find({ user: userId }).sort({ updatedAt: -1 });
return surveys; return surveys;
}; };
...@@ -36,3 +36,16 @@ export const deleteSurvey = async (surveyId: string) => { ...@@ -36,3 +36,16 @@ export const deleteSurvey = async (surveyId: string) => {
const survey = await Survey.findOneAndDelete({ _id: surveyId }); const survey = await Survey.findOneAndDelete({ _id: surveyId });
return survey; return survey;
}; };
export const putNewQuestion = async (newQuestion: any, surveyId: string) => {
console.log(newQuestion, surveyId);
if (newQuestion !== null) {
const updatedSurvey = await Survey.findOneAndUpdate(
{ _id: surveyId },
{ $push: { questions: newQuestion } },
{ new: true }
).populate("questions");
return updatedSurvey;
}
return null;
};
import { model, Schema, Types } from "mongoose";
export interface IResponse {
_id?: Types.ObjectId;
surveyId?: Types.ObjectId;
questionId?: Types.ObjectId;
respondent?: string;
answer?: any;
}
const schema = new Schema<IResponse>(
{
surveyId: { type: Schema.Types.ObjectId, ref: "Survey" },
questionId: { type: Schema.Types.ObjectId, ref: "Question" },
respondent: { type: String },
answer: { type: Object },
},
{ timestamps: true }
);
export default model<IResponse>("Response", schema);
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