forked from polito-WA1-AW1-2023/wa1-iz-weeks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
code developed in the second hour of class
- Loading branch information
1 parent
b845ff7
commit e5277e8
Showing
5 changed files
with
67 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,25 @@ | ||
/* Add the API calls here */ | ||
import { Answer, Question } from "./QAModels"; | ||
const SERVER_URL = 'http://localhost:3001'; | ||
|
||
const getQuestions = async () => { | ||
const response = await fetch(SERVER_URL + '/api/questions'); | ||
if(response.ok) { | ||
const questionsJson = await response.json(); | ||
return questionsJson.map(q => new Question(q.id, q.text, q.author, q.date)); | ||
} | ||
else | ||
throw new Error('Internal server error'); | ||
} | ||
|
||
const getAnswers = async (questionId) => { | ||
const response = await fetch(SERVER_URL + `/api/questions/${questionId}/answers`); | ||
const answersJson = await response.json(); | ||
if(response.ok) { | ||
return answersJson.map(ans => new Answer(ans.id, ans.text, ans.author, ans.date, ans.score)); | ||
} | ||
else | ||
throw answersJson; | ||
} | ||
|
||
const API = {getQuestions, getAnswers}; | ||
export default API; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 27 additions & 2 deletions
29
week11/react-qa/src/components/SingleQuestionComponent.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters