Commit e5778631 authored by Yoon, Daeki's avatar Yoon, Daeki 😅
Browse files

edit survey 로직 수정 변경

parent 56d0596c
...@@ -12,25 +12,29 @@ import { QUESTION_TYPES } from "../commons"; ...@@ -12,25 +12,29 @@ import { QUESTION_TYPES } from "../commons";
type Props = { type Props = {
element: BasicQuestionType; element: BasicQuestionType;
handleQuestion: (id: string) => void; handleQuestion: (element: BasicQuestionType) => void;
deleteQuestion: (id: string) => void; deleteQuestion: (id: string) => void;
isSave: boolean; // isSave: boolean;
}; };
export const Question = ({ export const Question = ({
element, element,
handleQuestion, handleQuestion,
deleteQuestion, deleteQuestion,
isSave, }: // isSave,
}: Props) => { Props) => {
const [save, setSave] = useState(isSave); const [question, setQuestion] = useState({ ...element });
const [isSaved, setIsSaved] = useState(false);
async function handleEditComplete() { async function handleEditComplete() {
try { try {
console.log(question);
const newQuestion: BasicQuestionType = await questionApi.updateQuestion( const newQuestion: BasicQuestionType = await questionApi.updateQuestion(
element element
); );
console.log(newQuestion); // console.log(newQuestion);
setSave(true); handleQuestion(question);
setIsSaved(true);
// setSuccess(true); // setSuccess(true);
// setError(""); // setError("");
} catch (error) { } catch (error) {
...@@ -44,12 +48,13 @@ export const Question = ({ ...@@ -44,12 +48,13 @@ export const Question = ({
function handleSelect(event: React.ChangeEvent<HTMLSelectElement>) { function handleSelect(event: React.ChangeEvent<HTMLSelectElement>) {
const selectedType = event.currentTarget.value; const selectedType = event.currentTarget.value;
console.log(selectedType); console.log(selectedType);
let content;
if ( if (
selectedType === "radio" || selectedType === "radio" ||
selectedType === "dropdown" || selectedType === "dropdown" ||
selectedType === "checkbox" selectedType === "checkbox"
) { ) {
element.content = { content = {
choices: [ choices: [
{ text: "", value: 0 }, { text: "", value: 0 },
{ text: "", value: 1 }, { text: "", value: 1 },
...@@ -57,9 +62,9 @@ export const Question = ({ ...@@ -57,9 +62,9 @@ export const Question = ({
], ],
}; };
} else if (selectedType === "essay") { } else if (selectedType === "essay") {
element.content = { choices: [] }; content = { choices: [] };
} else if (selectedType === "rating") { } else if (selectedType === "rating") {
element.content = { content = {
minRateDescription: "", minRateDescription: "",
maxRateDescription: "", maxRateDescription: "",
choices: [ choices: [
...@@ -69,52 +74,59 @@ export const Question = ({ ...@@ -69,52 +74,59 @@ export const Question = ({
], ],
}; };
} }
element.type = selectedType; // question.type = selectedType;
handleQuestion(element._id); setQuestion({ ...question, type: selectedType, content: content });
// handleQuestion(question._id);
} }
const handleElement = () => {
console.log("handle element");
setQuestion({ ...question });
};
function handleQuestionInfo(event: React.ChangeEvent<HTMLInputElement>) { function handleQuestionInfo(event: React.ChangeEvent<HTMLInputElement>) {
const { name, value } = event.currentTarget; const { name, value } = event.currentTarget;
element[name] = value; // question[name] = value;
handleQuestion(element._id); setQuestion({ ...question, [name]: value });
// handleQuestion(question._id);
} }
function getContent(element: BasicQuestionType) { function getContent(element: BasicQuestionType) {
switch (element.type) { switch (element.type) {
case "essay": case "essay":
return <EssayForm element={element} save={save} />; return <EssayForm element={element} save={isSaved} />;
case "radio": case "radio":
return ( return (
<RadioForm <RadioForm
handleQuestion={handleQuestion} handleQuestion={handleElement}
element={element} element={element}
save={save} save={isSaved}
/> />
); );
case "checkbox": case "checkbox":
return ( return (
<CheckboxForm <CheckboxForm
handleQuestion={handleQuestion} handleQuestion={handleElement}
element={element} element={element}
save={save} save={isSaved}
/> />
); );
case "dropdown": case "dropdown":
return ( return (
<DropdownForm <DropdownForm
handleQuestion={handleQuestion} handleQuestion={handleElement}
element={element} element={element}
save={save} save={isSaved}
/> />
); );
case "file": case "file":
return <FileForm element={element} save={save} />; return <FileForm element={element} save={isSaved} />;
case "rating": case "rating":
return ( return (
<RatingForm <RatingForm
handleQuestion={handleQuestion} handleQuestion={handleElement}
element={element} element={element}
save={save} save={isSaved}
/> />
); );
case "date": case "date":
...@@ -123,45 +135,57 @@ export const Question = ({ ...@@ -123,45 +135,57 @@ export const Question = ({
return <></>; return <></>;
} }
} }
const handleRequired = (event: React.ChangeEvent<HTMLInputElement>) => { const handleRequired = (event: React.ChangeEvent<HTMLInputElement>) => {
const { checked, value } = event.currentTarget; const { checked, value } = event.currentTarget;
element[value] = checked; question[value] = checked;
handleQuestion(element._id); setQuestion({ ...question, [value]: checked });
// handleQuestion(question._id);
};
const onCancel = () => {
console.log("element canceled button clicked", element);
console.log("question canceled button clicked", question);
setQuestion(element);
setIsSaved(true);
}; };
const handleDelete = () => { const handleDelete = () => {
deleteQuestion(element._id); deleteQuestion(question._id);
}; };
const handleEditClick = () => { const handleEditClick = () => {
setSave(false); setIsSaved(false);
}; };
return ( return (
<div <div
style={{ borderColor: save ? "#58ACFA" : "red" }} style={{ borderColor: isSaved ? "#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">
<input <input
type="text" type="text"
name="title" name="title"
id={element._id} id={question._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={"Question Title"} placeholder={"Question Title"}
value={element.title} value={question.title}
onChange={handleQuestionInfo} onChange={handleQuestionInfo}
disabled={save} disabled={isSaved}
></input> ></input>
<select <select
id={element._id} id={question._id}
name="type" name="type"
onChange={handleSelect} onChange={handleSelect}
disabled={save} disabled={isSaved}
value={element.type} value={question.type}
className="w-32 md: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-32 md: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"
> >
{Array.from(QUESTION_TYPES.entries()).map(([key, value]) => ( {Array.from(QUESTION_TYPES.entries()).map(([key, value]) => (
<option <option
key={key} key={key}
id={element._id} id={question._id}
value={key} value={key}
// selected={key === element.type} // selected={key === element.type}
> >
...@@ -174,15 +198,15 @@ export const Question = ({ ...@@ -174,15 +198,15 @@ export const Question = ({
<input <input
type="text" type="text"
name="comment" name="comment"
id={element._id} id={question._id}
className="border w-11/12" className="border w-11/12"
placeholder="질문에 대한 설명을 입력해주세요" placeholder="질문에 대한 설명을 입력해주세요"
value={element.comment} value={question.comment}
onChange={handleQuestionInfo} onChange={handleQuestionInfo}
disabled={save} disabled={isSaved}
></input> ></input>
</div> </div>
{getContent(element)} {getContent(question)}
<div className="place-self-end py-2"> <div className="place-self-end py-2">
<input <input
...@@ -190,23 +214,31 @@ export const Question = ({ ...@@ -190,23 +214,31 @@ export const Question = ({
id="isRequired" id="isRequired"
value="isRequired" value="isRequired"
onChange={handleRequired} onChange={handleRequired}
disabled={save} disabled={isSaved}
checked={element.isRequired} checked={question.isRequired}
/> />
<label htmlFor="isRequired" className="px-1"> <label htmlFor="isRequired" className="px-1">
필수 필수
</label> </label>
<button type="button" className="px-1" onClick={handleDelete}> {isSaved ? (
삭제 <>
</button> <button type="button" className="px-1" onClick={handleDelete}>
{save ? ( 삭제
<button type="button" className="px-1" onClick={handleEditClick}> </button>
수정하기 <button type="button" className="px-1" onClick={handleEditClick}>
</button> 수정
</button>
</>
) : ( ) : (
<button type="button" className="px-1" onClick={handleEditComplete}> <>
수정완료 <button type="button" className="px-1" onClick={onCancel}>
</button> 취소
</button>
<button type="button" className="px-1" onClick={handleEditComplete}>
확인
</button>
</>
)} )}
</div> </div>
</div> </div>
......
...@@ -45,8 +45,14 @@ export const EditSurvey = () => { ...@@ -45,8 +45,14 @@ export const EditSurvey = () => {
} }
} }
const handleQuestion = (id: string) => { const handleQuestion = (element: BasicQuestionType) => {
const newList: BasicQuestionType[] = [...survey.questions]; const index = survey.questions.findIndex(
(question) => question._id === element._id
);
survey.questions[index] = element;
const newList = [...survey.questions];
// const newList: BasicQuestionType[] = [...survey.questions];
console.log("new list in handle question", newList);
setSurvey({ ...survey, questions: newList }); setSurvey({ ...survey, questions: newList });
}; };
...@@ -141,7 +147,7 @@ export const EditSurvey = () => { ...@@ -141,7 +147,7 @@ export const EditSurvey = () => {
<Question <Question
key={question._id} key={question._id}
element={question} element={question}
isSave={state ? true : false} // isSave={state ? true : false}
handleQuestion={handleQuestion} handleQuestion={handleQuestion}
deleteQuestion={deleteQuestion} deleteQuestion={deleteQuestion}
/> />
......
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