Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrr committed Aug 7, 2024
1 parent ceaa98c commit 590d8c7
Showing 1 changed file with 35 additions and 42 deletions.
77 changes: 35 additions & 42 deletions client/src/components/MultiStepForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from "react";
import Backend from "../apis/backend";
import useStickyState from "../hooks/useStickyState";
import { IQuestionData, questionData } from '../data/questions';

import "./style.css";

function MultiStepForm(props) {
Expand All @@ -11,21 +11,16 @@ function MultiStepForm(props) {
// );

const [currentStepIndex, setCurrentStepIndex] = useState(0);
const [questions, setQuestions] = useState<IQuestionData[]>([]);

useEffect(() => {
setQuestions(questionData);
}, []);

function checkAnswers(answerList: string[]) {
function checkAnswers(answerList) {
if (answerList.includes("")) {
return false;
} else {
return true;
}
}

function whichQuestion(answerList: string[]) {
function whichQuestion(answerList) {
if (answerList.includes("")) {
return answerList.indexOf("");
} else {
Expand Down Expand Up @@ -55,42 +50,40 @@ function MultiStepForm(props) {
}

// if the user answered the statement, then save the answer and set the answerSaved flag to true

if (!props.steps[currentStepIndex].answereSaved) {
const answers = props.steps[currentStepIndex].answers;
const currentQuestion = questions.find(q => q.id === props.steps[currentStepIndex].id);

if (currentQuestion) {
const payload: Record<string, any> = {
statementId: props.steps[currentStepIndex].id,
sessionId: props.sessionId,
origLanguage: "en",
clientVersion: process.env.GITHUB_HASH || 'default_version', // Ensure this is set in your environment
};

// Construct payload dynamically based on possible answers
currentQuestion.possibleAnswers.forEach((_, index) => {
const [id, value] = answers[index].split("-");
if (index === 0) payload["I_agree"] = value === "Yes" ? 1 : 0;
if (index === 1) payload["I_agree_reason"] = value;
if (index === 2) payload["others_agree"] = value === "Yes" ? 1 : 0;
if (index === 3) payload["others_agree_reason"] = value;
if (index === 4) payload["perceived_commonsense"] = value === "Yes" ? 1 : 0;
if (index === 5 && value !== "") payload["clarity"] = value;
});

Backend.post("/answers", payload).then((response) => {
props.handleAnswerSaving(props.steps[currentStepIndex].id, true);
props.steps[currentStepIndex].answereSaved = true;
});
}
} else {
// TODO: invoke error on the button
props.setUnansweredQuestionIndex(
whichQuestion(props.steps[currentStepIndex].answers.slice(0, 5))
);
return whichQuestion(props.steps[currentStepIndex].answers.slice(0, 5));
Backend.post("/answers", {
statementId: props.steps[currentStepIndex].id,
I_agree:
props.steps[currentStepIndex].answers[0].split("-")[1] === "Yes"
? 1
: 0,
I_agree_reason:
props.steps[currentStepIndex].answers[1].split("-")[1],
others_agree:
props.steps[currentStepIndex].answers[2].split("-")[1] === "Yes"
? 1
: 0,
others_agree_reason:
props.steps[currentStepIndex].answers[3].split("-")[1],
perceived_commonsense:
props.steps[currentStepIndex].answers[4].split("-")[1] === "Yes"
? 1
: 0,
clarity: "removed",
origLanguage: "en",
sessionId: props.sessionId,
withCredentials: true,
}).then((response) => {
props.handleAnswerSaving(props.steps[currentStepIndex].id, true);
props.steps[currentStepIndex].answereSaved = true;
});
}
} else {
// TODO: invoke error on the button
props.setUnansweredQuestionIndex(
whichQuestion(props.steps[currentStepIndex].answers.slice(0, 5))
);
return whichQuestion(props.steps[currentStepIndex].answers.slice(0, 5));
}

window.scrollTo({
Expand Down

0 comments on commit 590d8c7

Please sign in to comment.