-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion.html
More file actions
216 lines (199 loc) · 6.19 KB
/
Copy pathquestion.html
File metadata and controls
216 lines (199 loc) · 6.19 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!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>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #1a1a1a;
color: #fff;
}
.container {
max-width: 980px;
margin: 25px auto;
background-color: #2a2a2a;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
small a {
color: #fff;
font-weight: 600;
text-decoration: none;
display: inline-block;
padding: 8px 12px;
background-color: #0077cc;
border-radius: 10px;
}
small button {
padding: 8px 12px;
background-color: rgb(232, 48, 48);
color: #fff;
border: none;
border-radius: 10px;
cursor: pointer;
font-size: 14px;
}
.container__top {
display: flex;
justify-content: space-between;
align-items: center;
}
h2#question {
font-size: 24px;
margin: 20px 0px;
}
textarea {
width: 100%;
padding: 15px;
background-color: #444;
color: #fff;
border: 1px solid #444;
border-radius: 3px;
resize: vertical;
}
button#postbtn {
padding: 8px 16px;
background-color: #0077cc;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
font-size: 14px;
margin-top: 10px;
transition: background-color 0.3s;
}
button#postbtn:hover {
background-color: #005299;
}
ul#answer {
list-style-type: none;
padding: 0;
margin-top: 20px;
}
ul#answer li {
margin-left: 18px;
white-space: pre-wrap;
margin-top: 15px;
background-color: #333;
padding: 15px;
border-radius: 5px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
position: relative;
}
ul#answer li button {
padding: 5px;
position: absolute;
top: 10px;
right: 10px;
background-color: rgb(232, 48, 48);
color: #fff;
border: none;
border-radius: 10px;
cursor: pointer;
font-size: 14px;
}
ul#answer li:not(:last-child) {
margin-bottom: 15px;
}
</style>
</head>
<body>
<div class="container">
<div class="container__top">
<small>
<a href="/">< back</a>
</small>
<small>
<button id="deletequestion">delete</button>
</small>
</div>
<h2 id="question">
</h2>
<textarea style="width: 100%;padding: 15px;" rows="6" type="text" id="answerinput"></textarea>
<button id="postbtn">Publish</button>
<br>
<ul id="answer">
</ul>
</div>
<script>
const question = document.getElementById("question")
const answer = document.getElementById("answer")
const fetchQuestion = async () => {
let res = await fetch(`${document.location.href}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
})
let data = await res.json();
console.log(data)
question.innerText = data.question
for (let i = 0; i < data.answers.length; i++) {
let ans = data.answers[i]
answer.innerHTML += `<li id=${ans.id}>${ans.answer} <button onclick="deleteanswer(${ans.id})">del</button></li>`
}
}
fetchQuestion()
const answerinput = document.getElementById("answerinput")
const postbtn = document.getElementById("postbtn")
postbtn.addEventListener("click", async () => {
if (answerinput.value != "") {
let res = await fetch(`/answerqs`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: document.location.search.substring(1).split("=")[1],
ans: answerinput.value
})
})
answerinput.value = ""
window.location.reload()
}
})
// delete question
const deletequestionbtn = document.getElementById("deletequestion")
deletequestionbtn.addEventListener("click", async () => {
const consent = confirm("Are you sure you want to delete?")
if (consent) {
let res = await fetch(`/deleteqs`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: document.location.search.substring(1).split("=")[1],
})
})
window.location.replace("/")
}
})
// delete answer
const deleteanswer = async (ansid) => {
const consent = confirm("Are you sure you want to delete?")
if (consent) {
let res = await fetch(`/deleteans`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
ansid
})
})
window.location.reload()
}
}
</script>
</body>
</html>