cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="container">
  
  
</div>
            
          
!
            
              body {
  background-color: #F5F5F5;
/*   font-family: sans-serif; */
  margin-top: 70px;
}

.well {
  background-color: #E0E0E0;
  border: 2px #ECEFF1 solid;
}


            
          
!
            
              let { Well, Panel, PanelGroup, Accordion, ListGroup, ListGroupItem, Modal,Button, ButtonToolbar, Popover, Tooltip, OverlayTrigger, FormGroup, FormControl, ControlLabel} = ReactBootstrap;

let userRecipes = [
        {name: "Apple Pie", ingredients: ["1,2,3"]},
        {name: "Something", ingredients: ["4,5,6"]}, {name:"xD", ingredients: ["7,8,9"]}, {name:":PPPPPP", ingredients: ["10,11,12"]}];

let recipes = [];

if(!localStorage._user_recipes){
  
  localStorage.setItem('_user_recipes', JSON.stringify(userRecipes));

  
} 


recipes = JSON.parse(localStorage.getItem('_user_recipes'));

console.log(recipes);

class IngredientsRow extends React.Component 
{
  
  render(){
    
      return (
    <ListGroupItem>{this.props.val}</ListGroupItem>
   );
  }
}


class IngredientsBox extends React.Component{
  
  
  render(){
    

    const content = [];
   // Yes, I know it looks weird.. o.o 
 [...this.props.ingredients].join('').split(',').forEach((val, i) => {
      content.push(<IngredientsRow key={i}val={val}/>)
    });
      return (
        
               <div>
      
       {content}
        
      </div>
    
    );
   
  
}
}



class RecipeRow extends React.Component {
  constructor(props){
    super(props);
    this.state={
      display: true,
      ingredients: this.props.ingredients,
      show: false,
      name: this.props.name,
      recipes: this.props.recipes
      
    }
    
     this.handleShow = this.handleShow.bind(this);
    this.handleClose = this.handleClose.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.deleteRow = this.deleteRow.bind(this);
  }
  
  
   handleClose() {
    this.setState({ show: false });
  }
  
handleShow() {
    this.setState({ show: true });
  }
  
   handleSubmit(event){
    if(event) event.preventDefault();
     let nm = this.state.name;
     let ing = this.state.ingredients;
     debugger
     recipes = recipes.map((val, i) => {
       if(val.name == nm){
         recipes[i].name = this.name.value; 
     recipes[i].ingredients = this.ingredients.value; 
       } 
         return recipes[i];
       
     });
     
      localStorage.setItem('_user_recipes', JSON.stringify(recipes));
     
    this.setState({
      name: this.name.value,
  ingredients: this.ingredients.value
}, () => {
      this.setState({ show: false });
    });
    
     
}
  
  
  deleteRow(){
    this.setState({
      display: false
    })
    recipes = recipes.filter((val) => {
      return val.name !== this.state.name;
    })
     localStorage.setItem('_user_recipes', JSON.stringify(recipes));
    
  }
  render(){
 
    if(this.state.display){
       return(
      <div>
      <Panel id="collapsible-panel"  > 
          <Panel.Heading>
            <Panel.Title toggle >
              {this.state.name}
              
            </Panel.Title>
          </Panel.Heading>
          <Panel.Collapse>
            <Panel.Body>
               <h3 className="text-center">Ingredients</h3>
             <hr/>
                  <ListGroup>
                   <IngredientsBox ingredients={this.state.ingredients}/>
                    <br/>
        <Button bsStyle="danger" bsSize="lg" onClick={this.deleteRow}>Delete</Button>
        <Button 
          onClick={this.handleShow} bsStyle="primary" bsSize="lg">Edit </Button>
                   </ListGroup>
            </Panel.Body>
          </Panel.Collapse>
        </Panel>
           {/********************************* MODAL STARTS HERE **************************************************/}
           <Modal show={this.state.show} onHide={this.handleClose}>
          <Modal.Header closeButton>
            <Modal.Title>Add a Recipe</Modal.Title>
          </Modal.Header>
          <Modal.Body>
            {/*modal data update*/}
                    <form onSubmit={this.handleSubmit} >
                            <FormGroup controlId="fformBasicText" >
                              <ControlLabel>Edit or remove recipe names</ControlLabel>
                              <FormControl defaultValue={this.state.name} type="text"placeholder="Enter text"
                               inputRef={(ref) => {this.name = ref}}
                                />
                              
                              <br/>
                              <ControlLabel>...and ingredients seperated by commas e.g. butter, milk, eyeball, shoes</ControlLabel>
                              <FormControl defaultValue={this.state.ingredients} type="text"placeholder="Enter text"
                                inputRef={(ref) => {this.ingredients = ref}}
                                />
                             </FormGroup>
                      <Button onClick={this.handleClose} type="submit">Submit</Button>
              </form>
           </Modal.Body>
          <Modal.Footer>
            <Button onClick={this.handleClose}>Close</Button>
          </Modal.Footer>
        </Modal> 
      
      
      
      </div>
    
    
    );
       }
  else {
    return(<div></div>);
  }
  }
}


class RecipeBox extends React.Component {
  constructor(props){
    super(props);
    
  }
  render() {
    
    const arrOfRecipes = [];
    
    this.props.recipes.forEach((val, i) => {
      arrOfRecipes.push( <RecipeRow key={i} name={val.name} ingredients={val.ingredients}/>);

    });
    
    return (
      <div>
     
        {arrOfRecipes}
        
        

      </div>
    );
  }
}


class Result extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      show: false,
      recipes: this.props.recipes
    }
 this.handleShow = this.handleShow.bind(this);
    this.handleClose = this.handleClose.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
   }
  
  
  handleClose() {
    this.setState({ show: false });
  }
  
handleShow() {
    this.setState({ show: true });
  }
   handleSubmit(event){
    if(event) event.preventDefault();
    let name = this.name.value;
    let ingredients = this.ingredients.value;
    let obj = {name: name, ingredients: ingredients};
     
     
    this.setState({
  recipes: [...this.state.recipes, obj ]
}, () => {
      this.setState({ show: false });
      
      localStorage.setItem('_user_recipes', JSON.stringify([...recipes, obj]));
      
    });
}
  render(){
   return (
<div>
      <Well bsSize="large">
        
  <RecipeBox recipes={this.state.recipes}/>
      </Well>
  {/*MODAL HEREEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE*/}
  <Button bsStyle="primary" bsSize="large" onClick={this.handleShow} >
          Add more
        </Button>
      
      <Modal show={this.state.show} onHide={this.handleClose}>
          <Modal.Header closeButton>
            <Modal.Title>Add a Recipe</Modal.Title>
          </Modal.Header>
          <Modal.Body>
            {/*modal data update*/}
                    <form onSubmit={this.handleSubmit} >
                            <FormGroup controlId="fformBasicText" >
                              <ControlLabel>Add recipe name</ControlLabel>
                              <FormControl type="text"placeholder="Enter text"
                               inputRef={(ref) => {this.name = ref}}
                                />
                              
                              <br/>
                              <ControlLabel>...and ingredients seperated by commas e.g. butter, milk, eyeball, shoes</ControlLabel>
                              <FormControl type="text"placeholder="Enter text"
                                inputRef={(ref) => {this.ingredients = ref}}
                                />
                             </FormGroup>
                      <Button onClick={this.handleClose} type="submit">Submit</Button>
              </form>
           </Modal.Body>
          <Modal.Footer>
            <Button onClick={this.handleClose}>Close</Button>
          </Modal.Footer>
        </Modal> 
      </div>
    );
  }
}


ReactDOM.render(
  <Result recipes={recipes}/>,
  document.querySelector('.container')
);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console