Commit 7275ba42 authored by Jiwon Yoon's avatar Jiwon Yoon
Browse files

result에 맞게 다시 수정

parent 10f7f53d
...@@ -3,11 +3,10 @@ import { IQuestionData } from "../types"; ...@@ -3,11 +3,10 @@ import { IQuestionData } from "../types";
type Props = { type Props = {
question: IQuestionData; question: IQuestionData;
answers: any;
}; };
export const RCheckbox = ({ question, answers }: Props) => { export const RCheckbox = ({ question }: Props) => {
const result = answers.flat().reduce((acc: any, cur: any) => { const result = question.answers.flat().reduce((acc: any, cur: any) => {
acc[cur] = (acc[cur] || 0) + 1; acc[cur] = (acc[cur] || 0) + 1;
return acc; return acc;
}, {}); }, {});
......
...@@ -3,13 +3,12 @@ import { IQuestionData } from "../types"; ...@@ -3,13 +3,12 @@ import { IQuestionData } from "../types";
type Props = { type Props = {
question: IQuestionData; question: IQuestionData;
answers: any;
}; };
export const RDate = ({ question, answers }: Props) => { export const RDate = ({ question }: Props) => {
return ( return (
<div className="m-5"> <div className="m-5">
{answers.map((answer: any) => ( {question.answers.map((answer: any) => (
<div key={answer} className="font-bold"> <div key={answer} className="font-bold">
{answer} {answer}
</div> </div>
......
...@@ -3,11 +3,10 @@ import { IQuestionData } from "../types"; ...@@ -3,11 +3,10 @@ import { IQuestionData } from "../types";
type Props = { type Props = {
question: IQuestionData; question: IQuestionData;
answers: any;
}; };
export const RDropdown = ({ question, answers }: Props) => { export const RDropdown = ({ question }: Props) => {
const result = answers.reduce((acc: any, cur: any) => { const result = question.answers.reduce((acc: any, cur: any) => {
acc[cur] = (acc[cur] || 0) + 1; acc[cur] = (acc[cur] || 0) + 1;
return acc; return acc;
}, {}); }, {});
......
...@@ -3,13 +3,12 @@ import { IQuestionData } from "../types"; ...@@ -3,13 +3,12 @@ import { IQuestionData } from "../types";
type Props = { type Props = {
question: IQuestionData; question: IQuestionData;
answers: any;
}; };
export const REssay = ({ question, answers }: Props) => { export const REssay = ({ question }: Props) => {
return ( return (
<div className="m-5"> <div className="m-5">
{answers.map((answer: any, index: number) => ( {question.answers.map((answer: any, index: number) => (
<div key={index} className="font-bold"> <div key={index} className="font-bold">
{answer} {answer}
</div> </div>
......
...@@ -3,14 +3,13 @@ import { baseImageUrl } from "../apis"; ...@@ -3,14 +3,13 @@ import { baseImageUrl } from "../apis";
type Props = { type Props = {
question: any; question: any;
answers: any;
}; };
export const RFile = ({ question, answers }: Props) => { export const RFile = ({ question }: Props) => {
console.log("question", question); console.log("question", question);
return ( return (
<div className="m-5 flex justify-start items-center"> <div className="m-5 flex justify-start items-center">
{answers.map((answer: any, index: number) => ( {question.answers.map((answer: any, index: number) => (
<Fragment key={index}> <Fragment key={index}>
<img <img
className="h-14" className="h-14"
......
...@@ -3,11 +3,10 @@ import { IQuestionData } from "../types"; ...@@ -3,11 +3,10 @@ import { IQuestionData } from "../types";
type Props = { type Props = {
question: IQuestionData; question: IQuestionData;
answers: any;
}; };
export const RRadio = ({ question, answers }: Props) => { export const RRadio = ({ question }: Props) => {
const result = answers.reduce((acc: any, cur: any) => { const result = question.answers.reduce((acc: any, cur: any) => {
acc[cur] = (acc[cur] || 0) + 1; acc[cur] = (acc[cur] || 0) + 1;
return acc; return acc;
}, {}); }, {});
......
...@@ -3,11 +3,10 @@ import { IQuestionData } from "../types"; ...@@ -3,11 +3,10 @@ import { IQuestionData } from "../types";
type Props = { type Props = {
question: IQuestionData; question: IQuestionData;
answers: any;
}; };
export const RRating = ({ question, answers }: Props) => { export const RRating = ({ question }: Props) => {
const result = answers.reduce((acc: any, cur: any) => { const result = question.answers.reduce((acc: any, cur: any) => {
acc[cur] = (acc[cur] || 0) + 1; acc[cur] = (acc[cur] || 0) + 1;
return acc; return acc;
}, {}); }, {});
......
...@@ -109,25 +109,22 @@ export const getAnswerElementByType = ( ...@@ -109,25 +109,22 @@ export const getAnswerElementByType = (
} }
}; };
export const getResultElementByType = ( export const getResultElementByType = (question: IQuestionData) => {
question: IQuestionData,
answers: any
) => {
switch (question.type) { switch (question.type) {
case "singletext": case "singletext":
return <REssay question={question} answers={answers} />; return <REssay question={question} />;
case "radio": case "radio":
return <RRadio question={question} answers={answers} />; return <RRadio question={question} />;
case "checkbox": case "checkbox":
return <RCheckbox question={question} answers={answers} />; return <RCheckbox question={question} />;
case "dropdown": case "dropdown":
return <RDropdown question={question} answers={answers} />; return <RDropdown question={question} />;
case "file": case "file":
return <RFile question={question} answers={answers} />; return <RFile question={question} />;
case "rating": case "rating":
return <RRating question={question} answers={answers} />; return <RRating question={question} />;
case "date": case "date":
return <RDate question={question} answers={answers} />; return <RDate question={question} />;
default: default:
return <></>; return <></>;
} }
......
...@@ -4,10 +4,9 @@ import { getResultElementByType } from "../helpers/question.helper"; ...@@ -4,10 +4,9 @@ import { getResultElementByType } from "../helpers/question.helper";
type AccordionProps = { type AccordionProps = {
question: IQuestionData; question: IQuestionData;
answers: any;
}; };
export const Accordion = ({ question, answers }: AccordionProps) => { export const Accordion = ({ question }: AccordionProps) => {
const [isOpened, setOpened] = useState<boolean>(false); const [isOpened, setOpened] = useState<boolean>(false);
const [height, setHeight] = useState<string>("0px"); const [height, setHeight] = useState<string>("0px");
const contentElement = useRef<HTMLDivElement>(null); const contentElement = useRef<HTMLDivElement>(null);
...@@ -33,7 +32,7 @@ export const Accordion = ({ question, answers }: AccordionProps) => { ...@@ -33,7 +32,7 @@ export const Accordion = ({ question, answers }: AccordionProps) => {
style={{ height: height }} style={{ height: height }}
className="bg-gray-100 overflow-hidden transition-all duration-300" className="bg-gray-100 overflow-hidden transition-all duration-300"
> >
{answers && getResultElementByType(question, answers)} {question.answers && getResultElementByType(question)}
</div> </div>
</div> </div>
</div> </div>
......
...@@ -51,11 +51,7 @@ export const ResultSurvey = () => { ...@@ -51,11 +51,7 @@ export const ResultSurvey = () => {
<div className="container w-11/12 place-self-center"> <div className="container w-11/12 place-self-center">
{survey.questions.map((question) => ( {survey.questions.map((question) => (
<Accordion <Accordion key={question._id} question={question} />
key={question._id}
question={question.questionInfo}
answers={question.answers}
/>
))} ))}
</div> </div>
</div> </div>
......
...@@ -34,6 +34,9 @@ export const getAnswers = async (surveyId: string) => { ...@@ -34,6 +34,9 @@ export const getAnswers = async (surveyId: string) => {
$unwind: "$questionInfo", $unwind: "$questionInfo",
}, },
{ $set: { "questionInfo.answers": "$answers" } },
{ $unset: "answers" },
// 질문 순서대로 정렬 // 질문 순서대로 정렬
{ $sort: { "questionInfo.order": 1 } }, { $sort: { "questionInfo.order": 1 } },
...@@ -42,7 +45,7 @@ export const getAnswers = async (surveyId: string) => { ...@@ -42,7 +45,7 @@ export const getAnswers = async (surveyId: string) => {
$group: { $group: {
_id: "$surveyId", _id: "$surveyId",
questions: { questions: {
$push: { questionInfo: "$questionInfo", answers: "$answers" }, $push: "$questionInfo",
}, },
}, },
}, },
......
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