Skip to content

Commit

Permalink
Added Question's View Need to improve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
uo289267 committed Mar 2, 2024
1 parent 6b86c5e commit 074fff6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
5 changes: 4 additions & 1 deletion webapp/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CssBaseline from '@mui/material/CssBaseline';
import Container from '@mui/material/Container';
import Typography from '@mui/material/Typography';
import Link from '@mui/material/Link';
import QuestionView from './components/questionView/QuestionView';

function App() {
const [showLogin, setShowLogin] = useState(true);
Expand All @@ -14,6 +15,8 @@ function App() {
};

return (
<QuestionView />
/*
<Container component="main" maxWidth="xs">
<CssBaseline />
<Typography component="h1" variant="h5" align="center" sx={{ marginTop: 2 }}>
Expand All @@ -31,7 +34,7 @@ function App() {
</Link>
)}
</Typography>
</Container>
</Container>*/
);
}

Expand Down
4 changes: 3 additions & 1 deletion webapp/src/components/questionView/Question.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ class Question{
return this.answers;
}

}
}

export default Question;
13 changes: 7 additions & 6 deletions webapp/src/components/questionView/QuestionGenerator.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import Question from './Question';
class QuestionGenerator{

constructor(){
this.apiUrl = "http://localhost:8090/test";
this.questions = [];

}

generateQuestions(){
//Deberíamos recoger el json
fetch(this.apiUrl)
.then(response => response.json()) // Convertir la respuesta a JSON
.then(receivedQuestions => { // La respuesta convertida a JSON se pasa como argumento
.then(response => response.json())
.then(receivedQuestions => {
let i = 0;
var questions = [];
for(const question in receivedQuestions){//To have a condition, no legth or size
this.questions[i] = new Question(receivedQuestions[i]);
questions[i] = new Question(receivedQuestions[i]);
i += 1;
}
return questions;
});
}

}

var q = new QuestionGenerator();
q.generateQuestions();
export default QuestionGenerator;

38 changes: 38 additions & 0 deletions webapp/src/components/questionView/QuestionView.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Question from './Question';
import QuestionGenerator from './QuestionGenerator';
import { useState } from 'react';

const questionGenerator = new QuestionGenerator();
var questions = questionGenerator.generateQuestions();

function QuestionView(){
const [numQuestion, setnumQuestion] = useState(0);

function handleClick(){
setnumQuestion(numQuestion + 1);
}
return (<div>
{/*Nav*/}
<QuestionComponent numQuestion={numQuestion} handleClick={handleClick}/>

</div>);
}

function QuestionComponent({numQuestion, handleClick}){
return (
<div>
<p>questions[numQuestion].getQuestion()</p>
{questions[numQuestion].getAnswers().map((item, index) => (
<Answer key={index} text={item} onClick={handleClick}/>
))}
</div>
);
}

function Answer({text, handleClick}){
return (
<button onClick={handleClick}>{text}</button>
);
}

export default QuestionView;

0 comments on commit 074fff6

Please sign in to comment.