diff --git a/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/SurveyMenuBar.tsx b/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/SurveyMenuBar.tsx index e0095e6633..3a514f80b0 100644 --- a/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/SurveyMenuBar.tsx +++ b/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/SurveyMenuBar.tsx @@ -45,8 +45,8 @@ export default function SurveyMenuBar({ const { product } = useProduct(environmentId); let faultyQuestions: String[] = []; const existingLogicConditions = new Set(); + const existingQuestionIds = new Set(); - useEffect(() => { if (audiencePrompt && activeId === "settings") { setAudiencePrompt(false); @@ -118,6 +118,11 @@ export default function SurveyMenuBar({ } for (const question of survey.questions) { + if (existingQuestionIds.has(question.id)) { + toast.error("You cannot have 2 same question IDs, please change one!"); + return false; + } + existingQuestionIds.add(question.id); for (const logic of question.logic || []) { if (question.required && logic.condition === "skipped") { toast.error("User cannot skip a required question, please change the logic jump!"); diff --git a/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/UpdateQuestionId.tsx b/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/UpdateQuestionId.tsx index 1b18a11d26..32ad333608 100644 --- a/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/UpdateQuestionId.tsx +++ b/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/UpdateQuestionId.tsx @@ -7,6 +7,9 @@ import toast from "react-hot-toast"; export default function UpdateQuestionId({ localSurvey, question, questionIdx, updateQuestion }) { const [currentValue, setCurrentValue] = useState(question.id); const [prevValue, setPrevValue] = useState(question.id); + const [isInputInvalid, setIsInputInvalid] = useState( + currentValue.trim() === "" || currentValue.includes(" ") + ); const saveAction = () => { // return early if the input value was not changed @@ -14,28 +17,22 @@ export default function UpdateQuestionId({ localSurvey, question, questionIdx, u return; } - // check if id is unique const questionIds = localSurvey.questions.map((q) => q.id); if (questionIds.includes(currentValue)) { + setIsInputInvalid(true); toast.error("IDs have to be unique per survey."); - setCurrentValue(question.id); - return; - } - - // check if id contains any spaces - if (currentValue.trim() === "" || currentValue.includes(" ")) { + } else if (currentValue.trim() === "" || currentValue.includes(" ")) { + setIsInputInvalid(true); toast.error("ID should not contain space."); - setCurrentValue(question.id); - return; + } else { + setIsInputInvalid(false); + toast.success("Question ID updated."); } updateQuestion(questionIdx, { id: currentValue }); - toast.success("Question ID updated."); setPrevValue(currentValue); // after successful update, set current value as previous value }; - const isInputInvalid = currentValue.trim() === "" || currentValue.includes(" "); - return (