From f1c10da31955a9eac9f4fb45958e01776cd7c56a Mon Sep 17 00:00:00 2001
From: Sandro Lutz <lutzsa@student.ethz.ch>
Date: Tue, 19 Feb 2019 12:26:37 +0100
Subject: [PATCH] Add textfield as additional field for events

---
 src/events/editEvent.js | 83 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 75 insertions(+), 8 deletions(-)

diff --git a/src/events/editEvent.js b/src/events/editEvent.js
index a636fd6..38b7a33 100644
--- a/src/events/editEvent.js
+++ b/src/events/editEvent.js
@@ -44,9 +44,18 @@ export default class newEvent extends EditView {
     // read additional_fields to make it editable
     if (this.form.data.additional_fields) {
       const copy = JSON.parse(this.form.data.additional_fields);
-      this.form.data.add_fields_sbb = 'SBB_Abo' in copy.properties;
-      this.form.data.add_fields_food = 'Food' in copy.properties;
+      this.form.data.add_fields_sbb = 'sbb_abo' in copy.properties;
+      this.form.data.add_fields_food = 'food' in copy.properties;
       this.form.data.additional_fields = null;
+      let i = 0;
+      while (`text${i}` in copy.properties) {
+        this.form.data[`add_fields_text${i}`] = copy.properties[`text${i}`].title;
+        i += 1;
+      }
+      // TODO: find a better solution to keep track of the additional textfields
+      this.add_fields_text_index = i;
+    } else {
+      this.add_fields_text_index = 0;
     }
 
     // price can either not be set or set to null
@@ -77,23 +86,40 @@ export default class newEvent extends EditView {
       required: [],
     };
     if (this.form.data.add_fields_sbb) {
-      additionalFields.properties.SBB_Abo = {
+      additionalFields.properties.sbb_abo = {
         type: 'string',
+        title: 'SBB Abonnement',
         enum: ['None', 'GA', 'Halbtax', 'Gleis 7'],
       };
-      additionalFields.required.push('SBB_Abo');
+      additionalFields.required.push('sbb_abo');
     }
 
     if (this.form.data.add_fields_food) {
-      additionalFields.properties.Food = {
+      additionalFields.properties.food = {
         type: 'string',
+        title: 'Food',
         enum: ['Omnivor', 'Vegi', 'Vegan', 'Other'],
       };
-      additionalFields.properties['Special Food Requirements'] = {
+      additionalFields.properties.food_special = {
+        type: 'string',
+        title: 'Special Food Requirements',
+      };
+      additionalFields.required.push('food');
+    }
+
+    let i = 0;
+    while (`add_fields_text${i}` in this.form.data) {
+      const fieldName = `text${i}`;
+      additionalFields.properties[fieldName] = {
         type: 'string',
+        minLength: 1,
+        title: this.form.data[`add_fields_text${i}`],
       };
-      additionalFields.required.push('Food');
+      additionalFields.required.push(fieldName);
+      delete this.form.data[`add_fields_text${i}`];
+      i += 1;
     }
+
     if ('add_fields_sbb' in this.form.data) delete this.form.data.add_fields_sbb;
     if ('add_fields_food' in this.form.data) delete this.form.data.add_fields_food;
 
@@ -227,6 +253,33 @@ export default class newEvent extends EditView {
       return false;
     }).includes(true));
 
+    const addFieldsText = [];
+    let i = 0;
+    while (`add_fields_text${i}` in this.form.data) {
+      addFieldsText.push(this.form._renderField(`add_fields_text${i}`, {
+        type: 'string',
+        label: `Label for Textfield ${i}`,
+      }));
+      const fieldIndex = i;
+      addFieldsText.push(m(Button, {
+        label: `Remove Textfield ${i}`,
+        className: 'red-row-button',
+        events: {
+          onclick: () => {
+            let index = fieldIndex;
+            while (`add_fields_text${index + 1}` in this.form.data) {
+              this.form.data[`add_fields_text${index}`] =
+                this.form.data[`add_fields_text${index + 1}`];
+              index += 1;
+            }
+            delete this.form.data[`add_fields_text${index}`];
+            this.add_fields_text_index = index;
+          },
+        },
+      }));
+      i += 1;
+    }
+
     // checks currentPage and selects the fitting page
     return this.layout([
       // navigation bar
@@ -317,7 +370,21 @@ export default class newEvent extends EditView {
           }),
           this.hasregistration && this.form._renderField('add_fields_sbb', {
             type: 'boolean',
-            label: 'SBB Abbonement',
+            label: 'SBB Abonnement',
+          }),
+          m('br'),
+          ...this.hasregistration && addFieldsText,
+          m('br'),
+          this.hasregistration && m(Button, {
+            label: 'Additional Textfield',
+            className: 'blue-button',
+            border: true,
+            events: {
+              onclick: () => {
+                this.form.data[`add_fields_text${this.add_fields_text_index}`] = '';
+                this.add_fields_text_index += 1;
+              },
+            },
           }),
           m('br'),
           ...this.hasregistration && this.form.renderSchema(['allow_email_signup']),
-- 
GitLab