Skip to content

Commit 3d85712

Browse files
committed
Check for group existence on landing page.
1 parent a21134d commit 3d85712

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

static/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ <h1 id="title" class="navbar-brand">Galène</h1>
1818
<form id="groupform">
1919
<label for="group">Group:</label>
2020
<input id="group" type="text" name="group" class="form-control form-control-inline"/>
21-
<input type="submit" value="Join" class="btn btn-default btn-large"/><br/>
21+
<input id='submitbutton' type="submit" value="Join" class="btn btn-default btn-large"/><br/>
2222
</form>
23+
24+
<p id="errormessage"></p>
2325

2426
<div id="public-groups" class="groups">
2527
<h2>Public groups</h2>

static/mainpage.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ body {
44
flex-direction: column;
55
}
66

7+
#errormessage {
8+
color: red;
9+
font-weight: bold;
10+
height: 12px;
11+
}
12+
713
.groups {
814
}
915

static/mainpage.js

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,63 @@
2020

2121
'use strict';
2222

23-
document.getElementById('groupform').onsubmit = function(e) {
23+
document.getElementById('groupform').onsubmit = async function(e) {
2424
e.preventDefault();
25-
let group = document.getElementById('group').value.trim();
26-
if(group !== '')
27-
location.href = '/group/' + group + '/';
25+
clearError();
26+
let groupinput = document.getElementById('group')
27+
let button = document.getElementById('submitbutton');
28+
29+
let group = groupinput.value.trim();
30+
if(group === '')
31+
return;
32+
let url = '/group/' + group + '/';
33+
34+
try {
35+
groupinput.disabled = true;
36+
button.disabled = true;
37+
try {
38+
let resp = await fetch(url, {
39+
method: 'HEAD',
40+
});
41+
if(!resp.ok) {
42+
if(resp.status === 404)
43+
displayError('No such group');
44+
else
45+
displayError(`The server said: ${resp.status} ${resp.statusText}`);
46+
return;
47+
}
48+
} catch(e) {
49+
displayError(`Coudln't connect: ${e.toString()}`);
50+
return;
51+
}
52+
} finally {
53+
groupinput.disabled = false;
54+
button.disabled = false;
55+
}
56+
57+
location.href = url;
2858
};
2959

60+
var clearErrorTimeout = null;
61+
62+
function displayError(message) {
63+
clearError();
64+
let p = document.getElementById('errormessage');
65+
p.textContent = message;
66+
clearErrorTimeout = setTimeout(() => {
67+
let p = document.getElementById('errormessage');
68+
p.textContent = '';
69+
clearErrorTimeout = null;
70+
}, 2500);
71+
}
72+
73+
function clearError() {
74+
if(clearErrorTimeout != null) {
75+
clearTimeout(clearErrorTimeout);
76+
clearErrorTimeout = null;
77+
}
78+
}
79+
3080
async function listPublicGroups() {
3181
let div = document.getElementById('public-groups');
3282
let table = document.getElementById('public-groups-table');

0 commit comments

Comments
 (0)