Field Builder

by Matthew Vasallo

HTML

<div id="app"></div>
<!--
  Tickets:
  
  -Display the current weather in fahrenheit
  -Include humidity
  
  -Hook it up to live data using OpenWeatherMap
    -Docs: https://openweathermap.org/api
    -API Key: df8f9d34fbbc4d4a81fc7469e821616b
>

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-style: solid;
  border-color: #c6ebf3;
  border-radius: 4px;
  transition: all 0.2s;
  height: 100vh;
}

h2 {
  font-weight: bold;
  color: #2C7297;
  padding: 10px 0px 10px 10px;
  background: #D9EEF7;
  border: 1px solid #c6ebf3;
}

input[type=text],
select,
textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  resize: vertical;
  font-family: Helvetica;
  font-size: 15px;
}

input[type=checkbox] {
  margin: 0px 5px 0px 10px;
}

input[type=submit] {
  background-color: #4CAF50;
  color: white;
  padding: 12px;
  margin-left: 30%;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  float: left;
}

label {
  padding: 12px 12px 12px 0;
  display: inline-block;
}

select {
  background-color: rgb(248, 248, 248);
  border-width: 1px;
  border-style: solid;
  border-color: rgb(166, 166, 166);
  border-image: initial;
}

input[type=submit]:hover {
  background-color: #45a049;
}

.container {
  width: 60%;
  border-radius: 5px;
  padding: 20px 20px 20px 20px;
}

.col-25 {
  float: left;
  width: 30%;
  margin-top: 6px;
}

.col-75 {
  float: left;
  width: 70%;
  margin-top: 6px;
}

/* This is our style for the invalid fields */

input:invalid {
  border-color: #900;
  background-color: #FDD;
}

.error {
  border-color: #900;
  background-color: #FDD;
}

.cancel{
 color: red;
}

/* Clear floats after the columns */

.row:after {
  content: "";
  display: table;
  clear: both;
}

/* Responsive layout - when the screen is less than 600px wide, make the two columns stack on top of each other instead of next to each other */

@media screen and (max-width: 600px) {
  .col-25,
  .col-75,
  input[type=submit] {
    width: 100%;
    margin-top: 0;
  }
}

React

class FieldBuilder extends React.Component {
    constructor(props) {
        //= ({label, type, defaultVal, choices, order}) =>
        super(props);
        this.state = {
            label: "",
            valueRequired: false,
            defaultValue: "",
            dupChoices: [],
            uniqueChoices: [],
            choices: "",
            order: ""
        };
    }

    checkDupChoices = () => {
        const choices = this.state.choices.split("\n");
        const existingChoices = {};
        const dupChoices = [];
        const uniqueChoices = [];
        for (let i = 0; i < choices.length; i++) {
            if (existingChoices[choices[i]] > 0) {
                existingChoices[choices[i]]++;
            }
            else {
                existingChoices[choices[i]] = 1;
                uniqueChoices.push(choices[i]);
            }

            if (existingChoices[choices[i]] === 2) {
                dupChoices.push(choices[i]);
            }
        }
        this.setState({dupChoices, uniqueChoices});
        console.log('got here', existingChoices, this.duplicateChoices);
    };

    choicesAreValid = () => this.state.dupChoices.length <= 0 && this.state.uniqueChoices.length <= 49;


    handleSubmit = event => {
    		if(this.state.uniqueChoices.indexOf(this.state.defaultValue) === -1){
        const newChoices = [...this.state.uniqueChoices, this.state.defaultValue];
                  console.log(this.state.uniqueChoices);
        	this.setState({
          uniqueChoices: newChoices,
          choices: this.state.choices + '\n' + this.state.defaultValue
          });
        }
        const payload = {
            label: this.state.label,
            valueRequired: this.state.valueRequired,
            default: this.state.defaultValue,
            choices: this.state.uniqueChoices,
            order: this.state.order
        };
        console.log('sending post with payload ', payload);
        event.preventDefault();
       ...