Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented validation for GraphInput #53 #58

Merged
merged 3 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions src/components/shared/ExerciseSide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,30 @@ ExerciseSideProps): JSX.Element {
{
textArray: [
{ type: 'text', text: 'turtle.goto(' },
{ type: 'input', width: 2 },
{ type: 'text', text: ', -1)' },
{ type: 'input', width: 2, id: 0, answer: '2' },
{ type: 'text', text: ', 1)' },
],
},
{
textArray: [
{ type: 'text', text: 'turtle.setheading(' },
{ type: 'input', width: 4 },
{ type: 'input', width: 4, id: 1, answer: '225' },
{ type: 'text', text: ')' },
],
},
{
textArray: [
{ type: 'text', text: 'turtle.' },
{ type: 'input', width: 8 },
{ type: 'input', width: 8, id: 2, answer: 'forward' },
{ type: 'text', text: '()' },
],
},
{
textArray: [
{ type: 'text', text: 'turtle.goto(' },
{ type: 'input', width: 2 },
{ type: 'input', width: 2, id: 3, answer: '0' },
{ type: 'text', text: ', ' },
{ type: 'input', width: 2 },
{ type: 'input', width: 2, id: 4, answer: '-1' },
{ type: 'text', text: ')' },
],
},
Expand Down Expand Up @@ -189,25 +189,6 @@ ExerciseSideProps): JSX.Element {
</div>
</div>
);

/*<div className="exercise-box">
<AxisParent
axisMarkers={[
[-2, -1, 0, 1, 2],
[-1, 0, 1, 2],
]}
axisLabels={[
['A', '', '', 'B', 'C'],
['A', '', '', 'B'],
]}
toNextExercise={() => {
setCompleteExercises(completeExercises + 1);
incrementExercise();
return;
}}
/>
</div>
);*/
} else if (exercises[displayExercise] == 'axis') {
curExercise = (
<div className="exercise-box">
Expand Down
123 changes: 85 additions & 38 deletions src/components/shared/Exercises/GraphInput.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,76 @@
//import { useState } from 'react';
//import { useState } from 'react';
import '../../../styles/Exercises/GraphInput.scss';

interface GraphQuestionElement {
interface GraphQuestionData {
type: string;
text?: string;
width?: number;
answer?: string;
id?: number;
}

interface GraphQuestion {
textArray: GraphQuestionElement[];
interface GraphQuestionGrouping {
questionData: GraphQuestionData;
setCorrect: (id: number, value: boolean) => void;
}

interface GraphLineGrouping {
data: GraphLineData;
setCorrect: (id: number, value: boolean) => void;
}

interface GraphInputProps {
questionArray: GraphQuestion[];
questionArray: GraphLineData[];
nextExercise: () => void;
}

function GraphStringElement({ type, text, width }: GraphQuestionElement) {
if (type == 'text') {
return <p id="graphinput-check-question">{text}</p>;
interface GraphLineData {
textArray: GraphQuestionData[];
}

function GraphStringElement({
questionData,
setCorrect,
}: GraphQuestionGrouping) {
if (questionData.type == 'text') {
return <p id="graphinput-check-question">{questionData?.text ?? ''}</p>;
} else {
//const [inputText, setInputText] = useState("");
const handleChange = (event: { target: { value: string } }) => {
//console.log(`input text: ${event.target.value}`);
//console.log(questionData?.answer ?? '');
if (event.target.value == (questionData?.answer ?? '')) {
setCorrect(questionData?.id ?? -1, true);
} else {
setCorrect(questionData?.id ?? -1, false);
}
//setInputText(event.target.value);
};
return (
<input
type="text"
className="graphinput-check-box"
style={{ width: `${width * 16}px` }}
/>
<div>
<input
type="text"
className="graphinput-check-box"
style={{ width: `${(questionData?.width ?? 3) * 16}px` }}
onChange={handleChange}
/>
</div>
);
}
}

function GraphLine({ textArray }: GraphQuestion) {
function GraphLine({ data, setCorrect }: GraphLineGrouping) {
//Map all elements of TextArray
const makeElement = (elementData: GraphQuestionElement): JSX.Element => {
const makeElement = (elementData: GraphQuestionData): JSX.Element => {
return (
<GraphStringElement
type={elementData?.type}
text={elementData?.text}
width={elementData?.width}
answer={elementData?.answer}
/>
<GraphStringElement questionData={elementData} setCorrect={setCorrect} />
);
};
return (
<div id={'graphinput-question-container'}>{textArray.map(makeElement)}</div>
<div id={'graphinput-question-container'}>
{data.textArray.map(makeElement)}
</div>
);
/*return (
<div id="graphinput-question-container">
Expand All @@ -58,36 +85,56 @@ function GraphInput({
questionArray,
nextExercise,
}: GraphInputProps): JSX.Element {
const makeLine = (lineArray: GraphQuestion): JSX.Element => {
return <GraphLine textArray={lineArray.textArray} />;
const valueMap = new Map<number, boolean>();
for (const question of questionArray) {
for (const item of question.textArray) {
if (item.type == 'input') {
valueMap.set(item.id ?? -1, false);
}
}
}
const setValueCorrect = (id: number, value: boolean): void => {
//console.log(`Setting ${id} to ${value}`);
valueMap.set(id, value);
};

const checkCorrect = (): void => {
//console.log('Checking correct');
const valuesArray = Array.from(valueMap.values());
for (const i of valuesArray) {
//console.log(i);
if (!i) {
//console.log(`${i} is incorrect`);
//setWrong(true);
return;
}
}
//console.log('all right!');
nextExercise();
};

const makeLine = (
data: GraphLineData,
setCorrect: (id: number, value: boolean) => void
): JSX.Element => {
return <GraphLine data={data} setCorrect={setCorrect} />;
};
return (
<div id="graphinput-container">
<div id="graphinput-question">
Fill in the blanks to move the cursor along the path!
</div>
<div id="graphinput-check-question"></div>
<div id="graphinput-question-box">{questionArray.map(makeLine)}</div>
<div id="graphinput-question-box">
{questionArray.map((x) => makeLine(x, setValueCorrect))}
</div>
<div id="graphinput-check-button-container">
<button id="graphinput-check-button" onClick={nextExercise}>
<button id="graphinput-check-button" onClick={checkCorrect}>
Check
</button>
</div>
</div>
);
/*
return (
<div id="graphinput-container">
<div id="graphinput-question-box">
<GraphLine
textArray={[
{ text: 'Hi!', type: 'text' },
{ type: 'input', width: 10 },
]}
/>
</div>
</div>
);*/
}

export default GraphInput;
Loading