aboutsummaryrefslogtreecommitdiff
path: root/contrib/fcfsd/fcfsd-index.html
blob: 3fa71d7c89215f87c7787ced56f5815d8dc88b80 (plain) (blame)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>GNUnet FCFS Authority Name Registration Service</title>
    <style>
      html {
          box-sizing: border-box;
          font-family: sans-serif;
      }

      *, *:before, *:after {
          box-sizing: inherit;
      }

      header {
          width: 800px;
          margin: 0 auto;
      }

      main {
          width: 800px;
          margin: 0 auto;
      }

      section h4 {
          text-align: center;
          width: 100%;
      }

      section input {
          width: 100%;
          padding: 8px 17px;
          font-size: 1rem;
          border: 1px solid #aaa;
          border-radius: 7px;
          background-color: white;
          margin-bottom: 7px;
      }

      section input:focus {
          box-shadow: 0px 0px 5px 3px lightblue;
      }

      section button {
          font-size: 1rem;
          font-weight: bold;
          background-color: #8b008b;
          color: white;
          border: none;
          padding: 7px;
      }

      section button:hover {
          background-color: #bf00bf;
      }

      section button:disabled {
          background-color: gray;
      }

      section h3 {
          text-align: center;
          width: 100%;
      }

      section small {
          display: block;
          margin-bottom: 5px;
      }

      .error-message {
          color: red;
      }

      .success-message {
          color: green;
      }

      @media screen and (max-width: 991px) {
          header, main {
              width: 100%;
          }
      }

      footer {
          margin-top: 30px;
          text-align: center;
      }

      nav {
          border-bottom: 1px solid black;
      }

      nav button {
          font-size: 1rem;
          font-weight: bold;
          background-color: #ccc;
          border: 1px solid black;
          border-bottom: none;
          border-top-right-radius: 7px;
          border-top-left-radius: 7px;
          padding: 7px;
      }

      nav button:hover {
          background-color: #f0f0f0;
          cursor: pointer;
      }

      nav button.selected {
          background-color: #f0f0f0;
      }
    </style>
  </head>
  <body>
    <header>
      <h1>Name Registration Service</h1>
      <p>Here you can register a name for your zone as part of this service's
        delegated names.</p>
      <p>The registration is based on a <em>First Come First Served</em>
        policy, meaning a name is given to the first user requesting it.</p>
      <p>Use the search bar below to see if your desired name is available and
      then use the form to submit your registration request.</p>
    </header>
    <main>
      <div class="form-container">
        <nav>
          <button id="tab-search">Search</button>
          <button id="tab-register">Register</button>
        </nav>
        <section id="search-form">
          <h4>Is your name available?</h4>
          <h3 id="search-result-message"></h3>
          <input id="search-name"
                 name="search-name"
                 type="text"
                 placeholder="Your name..."
                 autocomplete="name"
                 maxlength="63"
                 minlength="1">
          <small class="error-message" id="search-name-error"></small>
          <button>Search</button>
        </section>
        <section id="submit-form">
          <h4>Submit a registration request</h4>
          <h3 id="submit-result-message"></h3>
          <input id="register-name"
                 name="register-name"
                 type="text"
                 placeholder="Your name..."
                 autocomplete="off"
                 maxlength="63"
                 minlength="1">
          <input id="register-value"
                 name="register-value"
                 type="text"
                 placeholder="Your zone key..."
                 autocomplete="off"
                 minlength="1">
          <small class="error-message" id="submit-error"></small>
          <button>Submit</button>
        </section>
      </div>
    </main>
    <footer>
      <a href="https://gnunet.org">GNUnet homepage</a>
    </footer>
    <script>
      const buttons = document.querySelectorAll('nav button');
      for (let i=0; i<buttons.length; ++i) {
          buttons[i].onclick = function (e) {
              let selected = document.querySelector('nav button.selected');
              if (selected) {
                  selected.classList.toggle('selected');
              }
              e.target.classList.toggle('selected');

              let show = '';
              let hide = '';
              if (e.target.id === 'tab-search') {
                  show = 'search-form';
                  hide = 'submit-form';
              } else {
                  show = 'submit-form';
                  hide = 'search-form'
              }

              document.getElementById(hide).style.display = 'none';
              document.getElementById(show).style.display = 'block';
          };
      }

      buttons[0].click({target: buttons[0]});

      const searchbutton = document.querySelector('#search-form button');
      const submitbutton = document.querySelector('#submit-form button');

      document.getElementById('search-name').onkeydown = function (e) {
          if (e.key !== 'Enter') {
              return;
          }

          searchbutton.click();
      };

      for (let n of ['register-name', 'register-value']) {
          document.getElementById(n).onkeydown = function (e) {
              if (e.key !== 'Enter') {
                  return;
              }

              submitbutton.click();
          };
      }

      searchbutton.onclick = function (e) {
          const searchname = document.getElementById('search-name');
          const errormsg = document.getElementById('search-name-error');
          const resultmsg = document.getElementById('search-result-message');

          if (0 === searchname.value.length) {
              errormsg.innerText = 'The field can not be empty';
              searchname.setCustomValidity('The field can not be empty');
              return;
          }

          if (-1 !== searchname.value.indexOf('.')) {
              errormsg.innerText = 'The name can not contain dots';
              searchname.setCustomValidity('The name can not contain dots');
              return;
          }

          searchname.setCustomValidity('');
          errormsg.innerText = '';

          const name = searchname.value.toLowerCase();

          searchbutton.disabled = true;
          submitbutton.disabled = true;

          fetch(`/search?name=${name}`)
              .then(function (response) {
                  if (!response.ok) {
                      throw 'error';
                  }

                  return response.json()
              })
              .then(function (data) {
                  if ("true" === data.free) {
                      resultmsg.innerText = `'${name}' is available!`;
                      resultmsg.classList.add('success-message');
                      resultmsg.classList.remove('error-message');
                  } else {
                      resultmsg.innerText = `'${name}' is not available`;
                      resultmsg.classList.remove('success-message');
                      resultmsg.classList.add('error-message');
                  }
                  searchbutton.disabled = false;
                  submitbutton.disabled = false;
              })
              .catch(function (error) {
                  resultmsg.innerText = 'An error occurred while processing your query';
                  resultmsg.classList.remove('success-message');
                  resultmsg.classList.add('error-message');
                  console.error(error);
                  searchbutton.disabled = false;
                  submitbutton.disabled = false;
              });
      };

      submitbutton.onclick = function (e) {
          const registername = document.getElementById('register-name');
          const registervalue = document.getElementById('register-value');
          const errormsg = document.getElementById('submit-error');
          const resultmsg = document.getElementById('submit-result-message');

          let errors = 0;
          let errs = [];

          if (0 === registername.value.length) {
              errs.push('The name field can not be empty');
              registername.setCustomValidity('The name field can not be empty');
              ++errors;
          }
          if (-1 !== registername.value.indexOf('.')) {
              errs.push('The name can not contain dots');
              registername.setCustomValidity('The name can not contain dots');
              ++errors;
          }
          if (0 === registervalue.value.length) {
              errs.push('The value field can not be empty');
              registervalue.setCustomValidity('The value field can not be empty');
              ++errors;
          }

          if (0 < errors) {
              errormsg.innerHTML = 'The form contains invalid values:';
              for (let e of errs) {
                  errormsg.innerHTML += '<br/>' + e;
              }
              return;
          }

          searchbutton.disabled = true;
          submitbutton.disabled = true;

          fetch('/register', {
              method: 'POST',
              cache: 'no-cache',
              headers: {
                  'Content-Type': 'application/json',
              },
              body: JSON.stringify({
                  name: registername.value,
                  key: registervalue.value,
              }),
          }).then(function (response) {
              return response.json();
          }).then(function (data) {
              if (data.error === "false") {
                  resultmsg.innerText = `'${registername.value}' was registered successfully!`;
                  resultmsg.classList.add('success-message');
                  resultmsg.classList.remove('error-message');
              } else {
                  resultmsg.innerText = `'${registername.value}' could not be registered! (${data.message})`;
                  resultmsg.classList.remove('success-message');
                  resultmsg.classList.add('error-message');
              }
              searchbutton.disabled = false;
              submitbutton.disabled = false;
          }).catch(function (error) {
              resultmsg.innerText = 'An error occurred while processing your query';
              resultmsg.classList.remove('success-message');
              resultmsg.classList.add('error-message');
              console.error(error);
              searchbutton.disabled = false;
              submitbutton.disabled = false;
          });
      };
    </script>
  </body>
</html>