Skip to content

Commit

Permalink
TES-242: After generating questions with AI, user must be redirected …
Browse files Browse the repository at this point in the history
…to the first question generated (#237)

* Handle identification answer

* Set index to first when doing AI generation
  • Loading branch information
HansGabriel authored Dec 6, 2023
1 parent 1b9d6cb commit ec23f8f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apps/expo/src/forms/CreateTestForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ const CreateTestForm: FC<Props> = ({
}),
);
removeBlankQuestions();
setLastIndex();
setSelectedIndex(0);
setErrorInAIQuestion(false);
setShowNumberOfQuestionsModal(false);
setShowAiModal(false);
Expand Down
1 change: 0 additions & 1 deletion apps/expo/src/screens/create-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const CreateTestScreen: FC = () => {
onSuccess: () => {
trpcUtils.test.invalidate();
trpcUtils.user.getTop.invalidate();
trpcUtils.collection.getTopCollections.invalidate();
},
});

Expand Down
3 changes: 0 additions & 3 deletions apps/expo/src/screens/edit-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import CreateTestForm from "../forms/CreateTestForm";
import { trpc } from "../utils/trpc";
import useQuestionStore from "../stores/useQuestionStore";
import useImageStore from "../stores/useImageStore";
import OptionsDropdown from "./create-question/options-dropdown";
import { match } from "ts-pattern";
import { mapZodError } from "../utils/helpers/zod";

Expand All @@ -28,7 +27,6 @@ export const EditTestScreen: FC<RootStackScreenProps<"EditTest">> = ({
navigation,
route,
}) => {
const { height, width } = Dimensions.get("window");
const { testId } = route.params;
const trpcUtils = trpc.useContext();
const goBack = useGoBack();
Expand Down Expand Up @@ -113,7 +111,6 @@ export const EditTestScreen: FC<RootStackScreenProps<"EditTest">> = ({
onSuccess: () => {
trpcUtils.test.invalidate();
trpcUtils.user.getTop.invalidate();
trpcUtils.collection.getTopCollections.invalidate();
},
});

Expand Down
8 changes: 4 additions & 4 deletions apps/expo/src/screens/play-test/TestCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useState } from "react";
import { View, Text, TouchableOpacity, TextInput } from "react-native";
import CheckboxIcon from "../../icons/CheckboxIcon";
import CloseSquareIcon from "../../icons/CloseSquareIcon";
Expand Down Expand Up @@ -117,19 +116,20 @@ export const TrueOrFalseCard = ({
};

export const IdentificationCard = ({
answer,
choices,
isDone,
setAnswer,
handleSubmit,
}: {
answer: string;
choices: ModifiedChoice[] | undefined;
isDone: boolean;
handleSubmit: (answer: string) => void;
setAnswer: React.Dispatch<React.SetStateAction<string>>;
}) => {
const [answer, setAnswer] = useState<string>("");

const onPress = () => {
handleSubmit(answer);
setAnswer("");
};

return (
Expand Down
32 changes: 32 additions & 0 deletions apps/expo/src/screens/play-test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const PlayTestScreen: FC<RootStackScreenProps<"PlayTest">> = ({
const [modalType, setModalType] = useState<"correct" | "incorrect">(
"incorrect",
);
const [answer, setAnswer] = useState<string>("");
const [index, setIndex] = useState<number>(0);
const [isDone, setIsDone] = useState<boolean>(false);
const [points, setPoints] = useState<number>(0);
Expand Down Expand Up @@ -405,6 +406,7 @@ export const PlayTestScreen: FC<RootStackScreenProps<"PlayTest">> = ({
setChoices(getSelectedChoices(singleQuestion));
}

setAnswer("");
upperBarRef.current?.hide();
setIsDone(false);
setChoiceStatus([false, false, false, false]);
Expand All @@ -425,6 +427,34 @@ export const PlayTestScreen: FC<RootStackScreenProps<"PlayTest">> = ({
question.type === "identification" ||
question.type === "multi_select"
) {
if (question.type === "identification") {
const isCorrectAnswer = question.choices.some(
(choice) => choice.text === answer,
);
if (isCorrectAnswer) {
const elapsedTime =
countdownTimerRef.current?.elapsedTime ?? question.time;
setPoints((prevPoints) => prevPoints + question.points);
setTime((prevTime) => prevTime + elapsedTime);
setModalType("correct");
if (isEffectsPlaying) {
playEffects({
sound: correctSoundInstance,
music: correctSound,
});
}
} else {
setModalType("incorrect");
if (isEffectsPlaying) {
playEffects({ sound: wrongSoundInstance, music: wrongSound });
}
const errorResult = getErrorMessage("times-up");
setErrorMessage(errorResult);
}
showUpperBar();
setIsDone(true);
return;
}
if (question.type === "multi_select") {
const allCorrect = choices.every(
(choice) => choiceStatus[choice.id] === choice.isCorrect,
Expand Down Expand Up @@ -586,6 +616,7 @@ export const PlayTestScreen: FC<RootStackScreenProps<"PlayTest">> = ({
))
.with("identification", () => (
<IdentificationCard
answer={answer}
choices={question?.choices?.map((choice, id) => {
return {
id,
Expand All @@ -597,6 +628,7 @@ export const PlayTestScreen: FC<RootStackScreenProps<"PlayTest">> = ({
})}
isDone={isDone}
handleSubmit={handleSubmitIdentification}
setAnswer={setAnswer}
/>
))
.with(undefined, () => <></>)
Expand Down

0 comments on commit ec23f8f

Please sign in to comment.