Skip to content

Commit

Permalink
Feature/Submit logic path
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryczko authored May 17, 2024
1 parent 57cfee7 commit b3606da
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 37 deletions.
3 changes: 2 additions & 1 deletion locales/en/surveyCreate.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
"options": "Display options",
"GREATER_THAN": "Greater than",
"LESS_THAN": "Less than",
"EQUAL": "Equal"
"EQUAL": "equals",
"SUBMITTED": "is submitted"
}
3 changes: 2 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ model AnswerData {
type LogicPath {
nextQuestionId String? @db.ObjectId
endSurvey Boolean?
selectedOption String?
comparisonType ComparisonType
selectedOption String
}

enum QuestionType {
Expand All @@ -134,4 +134,5 @@ enum ComparisonType {
EQUAL
GREATER_THAN
LESS_THAN
SUBMITTED
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export default function Condition({
isSubmitted,
} = useSurveyCreatorContext();

const currentQuestion = questions[questionIndex];

useEffect(() => {
if (conditionOptions?.comparisons.length === 1) {
updateLogicPath(questionIndex, stepIndex, {
Expand All @@ -42,6 +40,13 @@ export default function Condition({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const currentQuestion = questions[questionIndex];

const displayAnswersOptions =
currentQuestion.logicPaths?.[stepIndex]?.comparisonType &&
currentQuestion.logicPaths?.[stepIndex].comparisonType !==
ComparisonType.SUBMITTED;

return (
<>
<div className="mb-2 flex items-center justify-between gap-4">
Expand All @@ -55,41 +60,40 @@ export default function Condition({
<>
<ArrowRightIcon className="h-4 w-4" />
if this answer
{conditionOptions?.comparisons.length === 1 ? (
' is'
) : (
<Select
classNames="flex-grow"
required
submitted={isSubmitted}
disabled={isEditMode}
selectedValue={
currentQuestion.logicPaths?.[stepIndex].comparisonType
}
onChangeCallback={(option) =>
updateLogicPath(questionIndex, stepIndex, {
comparisonType: option.value as ComparisonType,
})
}
options={conditionOptions?.comparisons ?? []}
/>
{displayAnswersOptions && (
<Select
classNames="flex-grow"
disabled={isEditMode}
required
submitted={isSubmitted}
disabled={isEditMode}
emojiContent={currentQuestion.type === QuestionType.EMOJI}
selectedValue={
currentQuestion.logicPaths?.[stepIndex].comparisonType
currentQuestion.logicPaths?.[stepIndex].selectedOption ??
undefined
}
onChangeCallback={(option) =>
updateLogicPath(questionIndex, stepIndex, {
comparisonType: option.value as ComparisonType,
selectedOption: option.value,
})
}
options={conditionOptions?.comparisons ?? []}
options={conditionOptions?.options ?? []}
/>
)}
<Select
classNames="flex-grow"
disabled={isEditMode}
required
submitted={isSubmitted}
emojiContent={currentQuestion.type === QuestionType.EMOJI}
selectedValue={
currentQuestion.logicPaths?.[stepIndex].selectedOption
}
onChangeCallback={(option) =>
updateLogicPath(questionIndex, stepIndex, {
selectedOption: option.value,
})
}
options={conditionOptions?.options ?? []}
/>
jump to
<Select
classNames="flex-grow"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import LogicalJump from 'features/surveys/features/SurveyCreator/components/LogicalJump/LogicalJump';
import { DraftQuestion } from 'features/surveys/features/SurveyCreator/managers/createSurveyManager/createSurveyManager';
import { useSurveyCreatorContext } from 'features/surveys/features/SurveyCreator/managers/createSurveyManager/context';

import useTranslation from 'next-translate/useTranslation';
import { getAvailableOptions } from 'features/surveys/features/SurveyCreator/components/QuestionBlocks/AdvancedSettings/utils/getAvailableOptions';
import { getAvailableComparisons } from 'features/surveys/features/SurveyCreator/components/QuestionBlocks/AdvancedSettings/utils/getAvailableComparisons';
import { getAvailableJumps } from 'features/surveys/features/SurveyCreator/components/QuestionBlocks/AdvancedSettings/utils/getAvailableJumps';

interface RateAdvancedSettingsProps {
questionData: DraftQuestion;
questionIndex: number;
}

export default function InputAdvancedSettings({
questionData,
questionIndex,
}: RateAdvancedSettingsProps) {
const { questions } = useSurveyCreatorContext();

const { t } = useTranslation('surveyCreate');

return (
<div>
<LogicalJump
questionIndex={questionIndex}
conditions={
questionData.logicPaths?.map(() => ({
comparisons: getAvailableComparisons([], t),
options: getAvailableOptions([]),
jumpQuestions: getAvailableJumps(questions, questionData),
})) ?? []
}
/>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ export const getAvailableComparisons = (
value: comparison,
}));

namesWithValues.push({
name: t(ComparisonType.SUBMITTED),
value: ComparisonType.SUBMITTED,
});

return namesWithValues;
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DraftQuestion } from 'features/surveys/features/SurveyCreator/managers/
import RateAdvancedSettings from 'features/surveys/features/SurveyCreator/components/QuestionBlocks/AdvancedSettings/RateAdvancedSettings';
import ChoiceAdvancedSettings from 'features/surveys/features/SurveyCreator/components/QuestionBlocks/AdvancedSettings/ChoiceAdvancedSettings';
import EmojiAdvancedSettings from 'features/surveys/features/SurveyCreator/components/QuestionBlocks/AdvancedSettings/EmojiAdvancedSettings';
import InputAdvancedSettings from 'features/surveys/features/SurveyCreator/components/QuestionBlocks/AdvancedSettings/InputAdvancedSettings';

interface QuestionBlockFactoryProps {
questionData: DraftQuestion;
Expand Down Expand Up @@ -49,6 +50,15 @@ export default function QuestionBlockFactory({
);
}

if (questionData.type === QuestionType.INPUT) {
return (
<InputAdvancedSettings
questionData={questionData}
questionIndex={questionIndex}
/>
);
}

return null;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';

export const usePreviewPanelManager = () => {
const [isPanelOpened, setIsPanelOpened] = useState(false);

// useEffect(() => {
// if (window.innerWidth > 1280) {
// setIsPanelOpened(true);
// }
// }, []);
useEffect(() => {
if (window.innerWidth > 1280) {
setIsPanelOpened(true);
}
}, []);

const togglePanel = () => {
setIsPanelOpened((prev) => !prev);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ export const useSurveyAnswerManager = (

for (let i = 0; i < activeQuestion.logicPaths.length; i++) {
const path = activeQuestion.logicPaths[i];
if (

const equalCondition =
path.comparisonType === ComparisonType.EQUAL &&
activeQuestion.answer &&
path.selectedOption === activeQuestion.answer
) {
path.selectedOption === activeQuestion.answer;

const submitCondition =
path.comparisonType === ComparisonType.SUBMITTED;

if (equalCondition || submitCondition) {
if (path.endSurvey) {
handleSave(false);
return;
Expand All @@ -103,7 +108,9 @@ export const useSurveyAnswerManager = (
(question) => question.id === path.nextQuestionId
);
setActiveQuestionIndex(
nextQuestionIndex !== -1 ? nextQuestionIndex : 0
nextQuestionIndex !== -1
? nextQuestionIndex
: activeQuestionIndex + 1
);
setIsSubmitted(false);

Expand Down

0 comments on commit b3606da

Please sign in to comment.