diff --git a/frontend/src/surveys/EditSurvey.tsx b/frontend/src/surveys/EditSurvey.tsx index bd437512a3165679547dfc2c91e26f9641da1fd1..85fe721442b9b923f9fec166504b188a69b37568 100644 --- a/frontend/src/surveys/EditSurvey.tsx +++ b/frontend/src/surveys/EditSurvey.tsx @@ -26,10 +26,10 @@ export const EditSurvey = () => { // return result; // }; - const handleTitle = (title: string) => { - console.log("title in handle title:", title); + const handleTitleComment = (state: { title: string; comment: string }) => { + console.log("title in handle title and comment:", state); // survey.title = title - update({ ...survey, title: title }); + update({ ...survey, title: state.title, comment: state.comment }); }; /** @@ -86,7 +86,7 @@ export const EditSurvey = () => { addQuestion={addQuestion} deleteQuestion={deleteQuestion} handleQuestion={updateQuestion} - handleTitle={handleTitle} + handleTitleComment={handleTitleComment} // callApi={update} /> ); diff --git a/frontend/src/surveys/ModifySurveyView.tsx b/frontend/src/surveys/ModifySurveyView.tsx index 0e6bf135701a30a78e6e25d6c48ab0c69aabf0f7..abe2f3a54b744c668027c0f2fb37d5028b2db39b 100644 --- a/frontend/src/surveys/ModifySurveyView.tsx +++ b/frontend/src/surveys/ModifySurveyView.tsx @@ -5,6 +5,7 @@ import { SpinnerIcon } from "../icons"; import { CreateQuestionData, ISurvey } from "../types"; import { QuestionsList } from "./QuestionsList"; import { SurveyTitle } from "./SurveyTitle"; +import { SurveyTitleAndComment } from "./SurveyTitleAndComment"; type Props = { questions: CreateQuestionData[]; @@ -12,7 +13,7 @@ type Props = { addQuestion: () => void; deleteQuestion: (id: string) => void; handleQuestion: (question: CreateQuestionData) => void; - handleTitle: Function; + handleTitleComment: Function; // callApi: (surveyData: ISurvey) => Promise; }; @@ -22,7 +23,7 @@ export const ModifySurveyView = ({ addQuestion, deleteQuestion, handleQuestion, - handleTitle, + handleTitleComment, }: // callApi, Props) => { const [error, setError] = useState(""); @@ -104,8 +105,13 @@ Props) => { )}
- -
+ {/* */} + + {/*
{ value={survey.comment} onChange={handleChange} > -
+
*/} { + const [state, setState] = useState({ title: title, comment: comment }); + const [disabled, setDisabled] = useState(true); + + console.log("title:", title, "comment:", comment, "state:", state); + + const handleChange = (e: ChangeEvent) => { + const { name, value } = e.target; + setState({ ...state, [name]: value }); + }; + + const onEdit = () => { + setDisabled(false); + }; + + const onCancel = () => { + setDisabled(true); + setState({ title, comment }); + }; + + const handleConfirm = () => { + setDisabled(true); + handleTitleComment(state); + }; + + return ( +
+ + + +
+ {disabled ? ( + <> + + + ) : ( + <> + + + + )} +
+
+ ); +}; diff --git a/src/controllers/survey.controller.ts b/src/controllers/survey.controller.ts index 773e448ed36d5a7d239e9f6e1debea9dba8cdb44..0013d3d4e2f73b82a724828f9ddf7f3ee766afb3 100644 --- a/src/controllers/survey.controller.ts +++ b/src/controllers/survey.controller.ts @@ -67,8 +67,8 @@ export const getSurveys = asyncWrap(async (reqExp: Request, res: Response) => { export const updateSurvey = asyncWrap(async (req, res) => { const survey = req.body; - const newSurvey = await surveyDb.updateSurvey(survey); - return res.json(newSurvey); + const updatedSurvey = await surveyDb.updateSurvey(survey); + return res.json(updatedSurvey); }); export const userBySurveyId = async ( diff --git a/src/db/survey.db.ts b/src/db/survey.db.ts index 422c349863efd26f93cfd129075e66d3d27b9a2a..810bf713ec5ce4dbed35191490c72e1be82d64ef 100644 --- a/src/db/survey.db.ts +++ b/src/db/survey.db.ts @@ -75,18 +75,22 @@ export const getSurveys = async (userId: string) => { export const updateSurvey = async (survey: HydratedDocument) => { console.log("update survey", survey); - await Promise.all( - survey.questions.map( - async (question) => - await Question.findOneAndUpdate({ _id: question._id }, question, { - upsert: true, - }) - ) - ); - const newSurvey = await Survey.findOneAndUpdate({ _id: survey._id }, survey, { - new: true, - }).populate("questions"); - return newSurvey; + // await Promise.all( + // survey.questions.map( + // async (question) => + // await Question.findOneAndUpdate({ _id: question._id }, question, { + // upsert: true, + // }) + // ) + // ); + const updatedSurvey = await Survey.findOneAndUpdate( + { _id: survey._id }, + survey, + { + new: true, + } + ).populate("questions"); + return updatedSurvey; }; export const putNewQuestion = async (newQuestion: any, surveyId: string) => {