-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquestion.html
97 lines (94 loc) · 3.99 KB
/
question.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Question:</title>
</head>
<script>
document.addEventListener("DOMContentLoaded", async function () {
qID = getQuestionID(window.location.href);
console.log(qID);
if (qID == null) {
renderError("Question ID missing")
return;
}
try {
data = await getData(qID);
} catch(e) {
renderError("Something went wrong, we are working on it. If error persists, please <a href='https://righttoask.democracydevelopers.org.au/contact-us' target='_blank'>contact us</a>")
return
}
console.log("Data", data)
if (data.Ok != null) {
renderQuestion(data.Ok);
} else {
renderError(`Question with ID ${qID} not found`)
return
}
document.querySelector("#content").style.display = ""
});
function getQuestionID(url) {
const u = new URL(url);
return u.searchParams.get("id");
}
async function getData(qID) {
const url = `https://righttoasktestserver2.democracydevelopers.org.au/get_question?question_id=${qID}`;
const response = await fetch(url);
return response.json();
}
function renderQuestion(q) {
document.querySelector("#title").innerText += q.question_text;
document.querySelector("#author").innerText += q.author;
document.querySelector("#vote").innerText += q.net_votes;
document.querySelector("#dateCreated").innerText += new Date(q.timestamp*1000).toLocaleDateString();
asker = document.querySelector("#whoShouldAsk");
if (q.mp_who_should_ask_the_question && q.mp_who_should_ask_the_question.length > 0) {
renderMP(asker, q.mp_who_should_ask_the_question[0].MP);
} else {
asker.style.display = "none"
}
answerer = document.querySelector("#whoShouldAnswer");
if (q.entity_who_should_answer_the_question && q.entity_who_should_answer_the_question.length > 0) {
renderMP(answerer, q.entity_who_should_answer_the_question[0].MP);
} else {
answerer.style.display = "none"
}
}
function renderMP(node, mp) {
console.log(mp)
node.querySelector(".firstName").innerText += mp.surname;
node.querySelector(".surname").innerText += mp.first_name;
node.querySelector(".chamber").innerText += mp.electorate.chamber;
node.querySelector(".region").innerText += mp.electorate.region;
}
function renderError(m) {
document.querySelector("#error").innerHTML = m
document.querySelector("#error").style.display = ""
}
</script>
<body>
<div id="error" style="display: none;"></div>
<div id="content" style="display: none;">
<div id="title">Title: </div>
<div id="author">Author: </div>
<div id="dateCreated"></div>
<div id="vote">Vote: </div>
<div id="whoShouldAsk">
Who should ask?
<div class="firstName">firstname: </div>
<div class="surname">surname: </div>
<div class="chamber">chamber: </div>
<div class="region">region: </div>
</div>
<div id="whoShouldAnswer">
Who should answer?
<div class="firstName">firstname: </div>
<div class="surname">surname: </div>
<div class="chamber">chamber: </div>
<div class="region">region: </div>
</div>
</div>
</body>
</html>