Skip to content

Commit

Permalink
fix #1310: FeedbackForm calls makeResult
Browse files Browse the repository at this point in the history
  • Loading branch information
BeritJanssen committed Oct 15, 2024
1 parent 4061f91 commit f7d169b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
32 changes: 7 additions & 25 deletions frontend/src/components/FeedbackForm/FeedbackForm.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { useState, useRef } from "react";
import { useState } from "react";

import Question from "../Question/Question";
import Button from "../Button/Button";
import IQuestion from "@/types/Question";
import { OnResultType } from "@/hooks/useResultHandler";
import { submitResultType } from "@/hooks/useResultHandler";

interface FeedbackFormProps {
formActive: boolean;
form: IQuestion[];
buttonLabel: string;
skipLabel: string;
isSkippable: boolean;
onResult: OnResultType
onNext: () => void;
submitResult: submitResultType
emphasizeTitle?: boolean;
}

Expand All @@ -23,35 +22,18 @@ const FeedbackForm = ({
buttonLabel,
skipLabel,
isSkippable,
onResult,
onNext,
submitResult,
emphasizeTitle = false,
}: FeedbackFormProps) => {
const isSubmitted = useRef(false);
const showSubmitButtons =
form.filter((formElement) => formElement.submits).length === 0;

const [formValid, setFormValid] = useState(false);

const onSubmit = async () => {
// Prevent double submit
if (isSubmitted.current) {
return;
}
isSubmitted.current = true;

// Callback onResult with question data
await onResult({
form,
});

onNext();
};

const onChange = (value: string | number | boolean, question_index: number) => {
form[question_index].value = value;
if (form[question_index].submits) {
onSubmit();
submitResult();
}
// for every non-skippable question, check that we have a value
const validFormElements = form.filter(formElement => {
Expand Down Expand Up @@ -93,15 +75,15 @@ const FeedbackForm = ({
// skip button
<Button
onClick={() => {
onSubmit();
submitResult();
}}
className={"btn-gray col-4 align-self-start"
}
title={skipLabel}
/>)}
<Button
onClick={() => {
onSubmit();
submitResult();
}}
className={
"submit col-4"
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/components/Trial/Trial.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ vi.mock("../Playback/Playback", () => ({
)),
}));
vi.mock("../FeedbackForm/FeedbackForm", () => ({
default: vi.fn(({ onResult, onNext }) => (
<div data-testid="mock-feedback-form" onClick={() => { onResult(); onNext(); }}>Mock Feedback Form</div>
default: vi.fn(({ submitResult }) => (
<div data-testid="mock-feedback-form" onClick={() => { submitResult(); }}>Mock Feedback Form</div>
)),
}));
vi.mock("../HTML/HTML", () => ({
Expand Down Expand Up @@ -43,6 +43,7 @@ const defaultConfig = {
describe('Trial', () => {
const mockOnNext = vi.fn();
const mockOnResult = vi.fn();
const mockMakeResult = vi.fn();

beforeEach(() => {
vi.clearAllMocks();
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/components/Trial/Trial.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ const Trial = (props: TrialProps) => {
buttonLabel={feedback_form.submit_label}
skipLabel={feedback_form.skip_label}
isSkippable={feedback_form.is_skippable}
onResult={onResult}
onNext={onNext}
submitResult={makeResult}
// emphasizeTitle={feedback_form.is_profile}
// TODO: if we want left-aligned text with a pink divider,
// make this style option available again (used in Question.scss)
Expand Down

0 comments on commit f7d169b

Please sign in to comment.