fix: setting duplicate question Ids now does not set random ID when click saved, the user has to update it

This commit is contained in:
ShubhamPalriwala
2023-09-01 16:27:29 +05:30
parent 632bab40f3
commit 59fb26a6fa
2 changed files with 15 additions and 13 deletions

View File

@@ -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!");

View File

@@ -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 (
<div>
<Label htmlFor="questionId">Question ID</Label>