summaryrefslogtreecommitdiff
path: root/static/new_form.html
blob: 7022d5ace220a8c5a5aa6b3642195b35078809dc (plain)
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
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <nav></nav>
    <div class="main">

      <div>
        <label for="type">Type:</label>
        <select name="type" onchange="update_select()">
          <option value="multiple_select">multiple_select</option>
          <option value="number">number</option>
          <option value="range">range</option>
          <option value="textarea">textarea</option>
          <option value="text">text</option>
        </select>
      </div>

      <div>
        <label for="prompt">prompt</label>
        <input name="prompt" type="text"></input>
      </div>
      <div>
        <label for="prompt_id">prompt_id</label>
        <input name="prompt_id" type="text"></input>
      </div>

      <div class="range">
        <div>
          <label for="min">min</label>
          <input name="min" type="text"></input>
        </div>
        <div>
          <label for="max">max</label>
          <input name="max" type="text"></input>
        </div>
      </div>

      <div><button onclick="on_submit()">Submit</button></div>
      <script>
        async function enqueue_data(path, payload, do_alert=false, timestamp=true){
          if(timestamp){
            payload["timestamp"] =  Date.now()
          }
          await fetch(path, {method: "POST", body: JSON.stringify(payload)})
          if(do_alert){
            alert("saved!")
          }
        }
        function update_select(){
          let v = document.querySelectorAll("select")[0].value
          document.querySelectorAll(".range").forEach(el => {
            el.style.display = v == "range" ? "block" : "none"
          })
        }
        function on_submit(){
          let payload = {
            "type": document.querySelectorAll("select")[0].value,
          }
          document.querySelectorAll("input").forEach(el => {
            if(el.value.length > 0){
              payload[el.name] = el.value
            }
          })
          enqueue_data("/submit_new_form", payload, do_alert=true, timestamp=false)
        }
        window.onload = function(){
            update_select()
        }
      </script>
    </div>
  </body>
</html>