File size: 5,351 Bytes
1b19224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c3007d
1b19224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e13004b
1b19224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Team Registration - JSON Editor</title>
  <!-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/json-editor@latest/dist/jsoneditor.min.css"> -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/spectre.css@latest/dist/spectre.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/spectre.css@latest/dist/spectre-exp.min.css">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">


  <style>
    body { font-family: Arial, sans-serif; padding: 20px; }
    #editor_holder { margin-bottom: 20px; }
    .btn-group-custom {
      display: flex;
      justify-content: center;
      gap: 1rem;
      margin-top: 3rem;
    }
  </style>
</head>
<body>
  <h1>Team Registration Form</h1>
  <div id="editor_holder"></div>
  <div class="btn-group btn-group-custom ">
    <button id="submit" class="btn btn-primary">Submit</button>
    <button id="cancel" class="btn btn-error">Cancel</button>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
</body>

<script>
const schema = {
  "title": `All the information you provide in the form must be accurate; otherwise, your registration will be rejected. If you are a student participant, please check the "Is Student" box and fill in the relevant information.`,
  "type": "object",
  "definitions": {
    "student": {
    "type": "object",
    "id": "student",
    "properties": {
      "majar": {
        "type": "string",
        "title": "Majar",
        "minLength": 1,
      },
      "degree": {
        "type": "string",
        "title": "Degree",
        "enum": ["Bachelor", "Master", "PhD"]
      },
      "advising_professor": {
        "type": "string",
        "title": "Advising Professo",
        "minLength": 1,
      },
      "grade": {
        "type": "string",
        "title": "Grade",
        "minLength": 1,
      },
      "expected_graduation_year": {
        "type": "string",
        "title": "Expected Graduation Year",
        "minLength": 1,
      }
    }
  }
  },
  "properties": {
    "team_name": {
      "type": "string",
      "title": "Team Name",
      "minLength": 1,
    },
    "team_members": {
      "type": "array",
      "title": "Team Members",
      "minItems": 1,
      "maxItems": 12,
      "format": "tabs",
      "items": {
        "type": "object",
        "title": "Member",
        "properties": {
          "name": {
            "type": "string",
            "title": "Name",
            "minLength": 1
          },
          "email": {
            "type": "string",
            "title": "Email",
            "format": "email",
            "minLength": 1
          },
          "institution": {
            "type": "string",
            "title": "Institution",
            "minLength": 1
          },
            "student_info": {
            "title": "Is Student",
                "oneOf": [
                    {
                        "title": "no",
                        "type": "null"
                    },
                    {
                        "title": "yes",
                        "$ref": "#/definitions/student"
                    }
                ]
            }
        }
      }
    }
  }
}

const editor = new JSONEditor(document.getElementById('editor_holder'), {
    schema: schema,
    theme: 'spectre',
    iconlib: 'fontawesome5',
    disable_array_delete_all_rows: true,
    disable_array_delete_last_row: true,
    disable_collapse: true,
    disable_edit_json: true,
    disable_properties: true,
    disable_array_reorder: true,
    prompt_before_delete: false,
    required_by_default: true,
    array_controls_top: true,
    remove_button_labels: true,
    show_errors: "change",
});

submitButton = document.getElementById('submit');
cancelButton = document.getElementById('cancel');

submitButton.addEventListener('click', () => {
  const errors = editor.validate();
  editor.showValidationErrors(errors);
  if (errors.length > 0) {
    return;
  }
  const regsiterData = editor.getValue();
  regsiterData.team_members = regsiterData.team_members.map(member => {
    return {
      name: member.name,
      email: member.email,
      institution: member.institution,
      is_student: member.student_info !== null,
      ...member.student_info
    };
  });
  
  submitButton.textContent = "Submitting...";
  submitButton.classList.add('disabled');
  cancelButton.classList.add('disabled');
  fetch('/register', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(regsiterData) })
      .then(response => {
        if (response.status !== 200) {
          throw new Error('Network response was not ok');
        }
        return response.json();
      })
      .then(data => {
          if (data.success) {
            window.location.href = "/"
          } else {
            alert("Registration failed: " + data.response);
          }
      })
      .finally(() => {
          setTimeout(() => {
            submitButton.textContent = "Submit";
            submitButton.classList.remove('disabled');
            cancelButton.classList.remove('disabled');
          }, 2000);
      })
});
cancelButton.addEventListener('click', () => {
  window.location.href = "/";
});
</script>
</html>