-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-set.html
More file actions
269 lines (231 loc) · 10.9 KB
/
create-set.html
File metadata and controls
269 lines (231 loc) · 10.9 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<!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="icon" type="image/x-icon" href="favicon.png">
<link rel="stylesheet" href="main.css?12">
<style>
input {
border: var(--content-border);
padding: 10px;
color: var(--dark-color);
outline: none;
background-color:transparent;
width: calc(50% - 34px);
max-width: 162px;
margin-top: 10px;
}
button {
margin-top: 20px;
position: relative;
bottom: 10px;
}
.questions {
display: inline-block;
width: 100%;
}
#main {
text-align: center;
max-width: 400px;
width: calc(100% - 37px);
margin-left: 50%;
margin-top: 75px;
transform: translate(-50%);
}
#errors {
text-align: center;
font-size: 15pt;
color: #c25f46;
}
#name {
max-width: 350px;
width: calc(100% - 40px);
}
#description {
max-width: 350px;
width: calc(100% - 40px);
}
#new-question {
cursor: pointer;
margin-left: 50%;
transform: translate(-50%);
width: 150px;
}
#new-question:hover {
text-decoration: underline;
}
.chars {
margin-top: 80px;
margin-bottom: -80px;
text-align: center;
width: 100%;
display: none;
}
.char {
font-size: 20pt;
border: var(--content-border);
backdrop-filter: blur(3px);
width: 40px;
margin: 3px;
position: relative;
}
#q, #a {
margin-top: 35px;
margin-bottom: 10px;
}
#title {
overflow: hidden;
width: calc(100vw - 120px);
white-space: nowrap;
}
@media (max-width: 600px) {
#title {
font-size: 14pt;
margin-top: 15px;
white-space: nowrap !important;
}
}
</style>
</head>
<body>
<header>
<svg onclick="history.back()" 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 class="chars" id="spanishChars">
<button class="char" onclick="addChar(event, 'á');">á</button>
<button class="char" onclick="addChar(event, 'é');">é</button>
<button class="char" onclick="addChar(event, 'í');">í</button>
<button class="char" onclick="addChar(event, 'ó');">ó</button>
<button class="char" onclick="addChar(event, 'ú');">ú</button>
<button class="char" onclick="addChar(event, 'ü');">ü</button>
<button class="char" onclick="addChar(event, 'ñ');">ñ</button>
<button class="char" onclick="addChar(event, '¡');">¡</button>
<button class="char" onclick="addChar(event, '¿');">¿</button>
</div>
<div id="main">
<input id="name" type="text" placeholder="flash set name">
<input id="description" type="text" placeholder="flash set description">
<input id="q" type="text" placeholder="list name (ex: states)">
<input id="a" type="text" placeholder="list name (ex: capitals)">
<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>
<span style=" width: 180px; text-align: left; display: block; margin-left: 50%; transform: translate(-50%);">
<input checked type="checkbox" id="case_sensitive" style="width: 20px;">
<label for="case_sensitive">Case Sensitive</label>
<br>
<input type="checkbox" id="accent_sensitive" style="width: 20px;">
<label for="accent_sensitive">Accent Sensitive</label>
<br>
<input type="checkbox" id="spanish" style="width: 20px;" onclick="toggleSpanishAccents(event);">
<label for="spanish">Show Spanish Accents</label>
<br><br>
</span>
<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[2].value;
const a = main.children[3].value;
const caseSensitive = document.getElementById('case_sensitive').checked;
const accentSensitive = document.getElementById('accent_sensitive').checked;
const spanish = document.getElementById('spanish').checked;
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, caseSensitive: caseSensitive, accentSensitive: accentSensitive, spanish: spanish };
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 = `view-set.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'));
}
let activeInput = null;
window.addEventListener('mousedown', (e) => {
if (e.target.tagName == 'INPUT' && e.target.type == 'text') {
activeInput = e.target;
}
});
function toggleSpanishAccents(e) {
if (e.target.checked) {
document.getElementById('spanishChars').style.display = 'block';
} else {
document.getElementById('spanishChars').style.display = 'none';
}
}
function addChar(e, char) {
if (activeInput.type != 'text') return;
activeInput.value += char;
activeInput.focus();
}
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>