Skip to content

Commit

Permalink
Fixed the code:
Browse files Browse the repository at this point in the history
-The call to our inner API works
-Needed to add security changes in order to fullfil the CORS mechanism and allow to call our API from the webapp port
  • Loading branch information
Mister-Mario committed Mar 1, 2024
1 parent ff0f998 commit 6b86c5e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
17 changes: 17 additions & 0 deletions backend/wiq/src/main/java/com/wiq/wiq/CustomConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.wiq.wiq;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CustomConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:5500")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*");
}
}
21 changes: 9 additions & 12 deletions webapp/src/components/questionView/Question.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
class Question{
Question(json){
question = "";
answers = [];
parseQuestion(json)
constructor(json){
this.question = "";
this.answers = [];
this.parseQuestion(json)
}

parseQuestion(json){
this.question = json.question;
for(const answer in json.answers){
this.answers[0] = answer.correct;
for(let i = 0; i < answer.wrong.length; i++)
this.anwers[i + 1] = answer.wrong[i];
}
this.answers[0] = json.answers.correct;
for(let i = 0; i < json.answers.wrong.length; i++)
this.answers[i + 1] = json.answers.wrong[i];

}

getQuestion(){
Expand All @@ -22,6 +21,4 @@ class Question{
return this.answers;
}

}

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


class QuestionGenerator{

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

}

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

}

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

4 changes: 2 additions & 2 deletions webapp/src/components/questionView/test.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<script type="module" src="./QuestionGenerator.js"></script>
<script src="./Question.js"></script>
<script src="./QuestionGenerator.js"></script>
</head>
<body>
<h1>Hola</h1>
<script>q.generateQuestion();</script>
</body>

</html>

0 comments on commit 6b86c5e

Please sign in to comment.