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

const FieldService =  {
	getField: function(id) {
		return {
		  "label": "Sales region",
		  "required": false,
		  "choices": [
			"Asia",
			"Australia",
			"Western Europe",
			"North America",
			"Eastern Europe",
			"Latin America",
			"Middle East and Africa"
		  ],
		  "displayAlpha": true,
		  "default": "North America"
		}
	},
	saveField: function (fieldJson) {
		// Add the code here to call the API (or temporarily, just log fieldJson to the console)
    console.log('sending post with payload ', fieldJson);
    fetch("https://www.mocky.io/v2/566061f21200008e3aabd919", {
      method: "POST",
      body: JSON.stringify(fieldJson)
    });
	}
}



class FieldBuilder extends React.Component {
    constructor(props) {
        //= ({label, type, defaultVal, choices, order}) =>
        super(props);
        this.state = {
            label: "",
            required: false,
            defaultValue: "",
            dupChoices: [],
            uniqueChoices: [],
            choices: "",
            order: ""
        };
    }
    
    componentDidMount(){
    	const initialValues = FieldService.getField(1);
      this.setState({
        		label: initialValues.label,
            required: initialValues.required,
            defaultValue: initialValues.default,
            choices: initialValues.choices.join('\n'),
            uniqueChoices: initialValues.choices,
            order: initialValues.displayAlpha
      });
    }

    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) {
               ...