From 5b429e97693531d9a478c92ee42f3ad63aff6378 Mon Sep 17 00:00:00 2001 From: Max <ḿax.seyfried@gmx.de> Date: Mon, 6 Jul 2020 10:16:22 +0200 Subject: [PATCH] add new component addPlant removed functionality before --- imports/ui/AddPlant.jsx | 68 +++++++++++++++++++++++ imports/ui/Settings.jsx | 118 ++++------------------------------------ 2 files changed, 79 insertions(+), 107 deletions(-) create mode 100644 imports/ui/AddPlant.jsx diff --git a/imports/ui/AddPlant.jsx b/imports/ui/AddPlant.jsx new file mode 100644 index 0000000..a1961ce --- /dev/null +++ b/imports/ui/AddPlant.jsx @@ -0,0 +1,68 @@ +import React from 'react' + +import { Button, Form, Container, Row, Col, Table } from 'react-bootstrap'; + +class AddPlant extends React.Component{ + + constructor(props){ + super(props); + this.state = { + name: '', + type: '' + } + } + + handleChange = (e) => { + // console.log(e.target.id, e.target.value); + this.setState({ + [e.target.id]: e.target.value + }); + } + + // search for arrow function for further syntax knowledge + handleSubmit = (e) => { + e.preventDefault(); + console.log(this.state) + //this.props.addPlant + } + + + + render (){ + return ( + <> +
+ + Name: + {this.nameInput=(input)}} + placeholder="plant" + onChange={(e)=>this.setState({name:e.target.value})} /> + + + Type: + this.setState({type:e.target.value})} + > + + {this.props.typeArray.map((type, index) =>{ + return + })} + + + +
+ + ); + } +} + +export default AddPlant; + diff --git a/imports/ui/Settings.jsx b/imports/ui/Settings.jsx index e1cad0c..6f298e9 100644 --- a/imports/ui/Settings.jsx +++ b/imports/ui/Settings.jsx @@ -1,5 +1,5 @@ import React from 'react' - +import AddPlant from './AddPlant' import { Button, Form, Container, Row, Col, Table } from 'react-bootstrap'; class Settings extends React.Component{ @@ -7,20 +7,20 @@ class Settings extends React.Component{ constructor(props){ super(props); this.state = { - name: '', - type: '', - dirt: '', plants: [], types: ['Chile', 'Mint'], // hardcoded for now - dirts: ['Vegetable Soil', 'Potting Soil'], // hardcoded for now - updateId: -1, - btnName: 'submit' } } - componentDidMount(){ - // fetch data from db here - + // pass this function to the addplant comp as a prop + addPlant = (plant) => { + plant.id = Math.random(); // rework + + let plants = [...this.state.plants, plant]; // create a copy of the array and add the new plant to it + + this.setState({ + plants: plants + }) } render(){ @@ -38,50 +38,7 @@ class Settings extends React.Component{

- { - this.state.updateId >= 0 | this.state.plants.length === 0? // show the form only if we edit the plant or there is no plant set yet -
- - Name - {this.nameInput=(input)}} - placeholder="plant" - onChange={(e)=>this.setState({name:e.target.value})} /> - - - Type - this.setState({type:e.target.value})} - > - - {this.state.types.map((type, index) => { - return - })} - - - - Dirt - this.setState({dirt:e.target.value})} - > - - {this.state.dirts.map((dirt, index) => { - return - })} - - - -
- :null - } + @@ -109,8 +66,6 @@ class Settings extends React.Component{ {plant[0]} {plant[1]} {plant[2]} - - }) } @@ -125,57 +80,6 @@ class Settings extends React.Component{ ); } - // search for arrow function for further syntax knowledge - handleSubmit = (e) => { - e.preventDefault(); - if (this.state.updateId >= 0) { // check if it is in edit mode - this.state.plants[this.state.updateId][0] = this.state.name; - this.state.plants[this.state.updateId][1] = this.state.type; - this.state.plants[this.state.updateId][2] = this.state.dirt; // updates the name of the plant - this.setState({ - updateId:-1, // Todo check docu - name:'', // reset inputtext again - btnName:'submit' // reset button to submit again - }) - } else { - let newDataElement = [ - this.state.name, - this.state.type, - this.state.dirt - ] - console.log("name " + newDataElement[0]) - console.log("type " + newDataElement[1]) - console.log("dirt " + newDataElement[2]) - this.setState(prevState =>({ - plants:[...prevState.plants, newDataElement], // adds a new plant array - name:'' //to reset the inputfield - })) - return this.state.plants; - } - - } - - - removeItem = (value) => { - this.setState({ - plants:this.state.plants.filter((plant) => { // filter function add docu - return plant !== value; - }), - name:'' - }) - } - - editItem = (editedName, editedType, editedDirt, index) => { - this.setState({ - name:editedName, - type:editedType, - dirt:editedDirt, - btnName:'update', - updateId:index // should be edited to a boolean - }) - } - - } export default Settings;