-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-set.html
201 lines (167 loc) · 7.74 KB
/
create-set.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
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
<!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>Flash Study</title>
<link rel="stylesheet" href="main.css?4">
<style>
input {
border: 1px solid #d9dedd;
border-radius: 8px;
padding: 10px;
color: #d9dedd;
outline: none;
background-color:transparent;
width: 160px;
margin-top: 10px;
}
input::placeholder {
color: #d9dedd;
}
button {
margin-top: 20px;
position: relative;
bottom: 10px;
}
.questions {
display: inline-block;
}
#main {
text-align: center;
width: 400px;
transition: 0.3s;
background-color: rgba(255, 255, 255, 0.21);
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
border: 1px solid rgba(255, 255, 255, 0.24);
backdrop-filter: blur(3px);
border-radius: 8px;
margin-left: 50%;
margin-top: 50px;
transform: translate(-50%);
}
#errors {
text-align: center;
font-size: 15pt;
color: #a8514b;
}
#name {
width: 350px;
}
#description {
width: 350px;
}
#new-question {
cursor: pointer;
margin-left: 50%;
transform: translate(-50%);
width: 150px;
}
#new-question:hover {
text-decoration: underline;
}
#profile {
float: right;
margin-top: -43px;
}
</style>
</head>
<body>
<header>
<svg onclick="location.href = 'index.html'" id="back" xmlns="http://www.w3.org/2000/svg" width="40" height="40" fill="currentColor" class="bi bi-arrow-left-short" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M12 8a.5.5 0 0 1-.5.5H5.707l2.147 2.146a.5.5 0 0 1-.708.708l-3-3a.5.5 0 0 1 0-.708l3-3a.5.5 0 1 1 .708.708L5.707 7.5H11.5a.5.5 0 0 1 .5.5"/>
</svg>
<h1 id="title">Create New Flash Set</h1>
<svg onclick="location.href = 'login.html'" id="profile" xmlns="http://www.w3.org/2000/svg" width="36" height="36" fill="currentColor" class="bi bi-person" viewBox="0 0 16 16">
<path d="M8 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6Zm2-3a2 2 0 1 1-4 0 2 2 0 0 1 4 0Zm4 8c0 1-1 1-1 1H3s-1 0-1-1 1-4 6-4 6 3 6 4Zm-1-.004c-.001-.246-.154-.986-.832-1.664C11.516 10.68 10.289 10 8 10c-2.29 0-3.516.68-4.168 1.332-.678.678-.83 1.418-.832 1.664h10Z"/>
</svg>
</header>
<div id="main">
<input id="name" type="text" placeholder="flash set name">
<input id="description" type="text" placeholder="flash set description">
<br><br>
<input id="q" type="text" placeholder="list name (ex: states)">
<input id="a" type="text" placeholder="list name (ex: capitals)">
<br><br>
<span class="questions">
<input type="text" placeholder="question (ex: California)">
<input type="text" placeholder="answer (ex: Sacramento)">
</span>
<span class="questions">
<input type="text" placeholder="question">
<input type="text" placeholder="answer">
</span>
<span class="questions">
<input type="text" placeholder="question">
<input type="text" placeholder="answer">
</span>
<span class="questions">
<input type="text" placeholder="question">
<input type="text" placeholder="answer">
</span>
<span class="questions">
<input type="text" placeholder="question">
<input type="text" placeholder="answer">
</span>
<p id="new-question" onclick="addQuestion();">add another question</p>
<button onclick="createStudySet();">Create</button>
</div>
<p id="errors"></p>
<script src="main.js?v1"></script>
<script>
window.addEventListener('load', async () => {
let profile = document.getElementById('profile');
let response = await fetcher(`/auth/check`);
let result = await response.text();
if (result == 'A token is required for authentication' || result == 'Invalid Token') {
alert('You must be logged in to access this.');
location.assign('/login.html');
} else {
profile.setAttribute('onclick', `location.href = 'profile.html'`);
}
});
async function createStudySet() {
const questionsElements = document.querySelectorAll('.questions');
const main = document.getElementById('main');
const name = main.children[0].value;
const description = main.children[1].value;
const q = main.children[4].value;
const a = main.children[5].value;
let questions = [];
let answers = [];
questionsElements.forEach((question) => {
if (question.children[0].value == null || question.children[0].value == '' || question.children[1].value == null || question.children[1].value == '') {
return document.getElementById('errors').innerText = 'one of your question answer values is empty. this question will not be included';
}
questions.push(question.children[0].value);
answers.push(question.children[1].value);
});
const data = { "name": name, "description": description, "q": q, "a": a, "questions": questions, "answers": answers };
let res = await fetcher(`/sets/create`, { method: 'post', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json' }});
let text = await res.text();
if (res.status == 201) {
// success
location.href = `study.html?id=${text}`;
} else {
//failed to create set
document.getElementById('errors').innerText = text;
}
}
function addQuestion() {
const main = document.getElementById('main');
let ele = document.createElement('span');
ele.className = 'questions';
ele.innerHTML = `<input type="text" placeholder="question"> <input type="text" placeholder="answer">`;
main.insertBefore(ele, document.getElementById('new-question'))
}
window.addEventListener('keydown', (e) => {
switch (e.keyCode) {
case 9: // tab key
if (document.activeElement.getAttribute('placeholder') == 'answer' && Array.from(document.activeElement.parentElement.parentElement.children).indexOf(document.activeElement.parentElement) == document.activeElement.parentElement.parentElement.children.length - 3) addQuestion();
break;
}
});
</script>
</body>
</html>