css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              .recipe
  border: 1px solid gray
  background-color: lightgray
  border-radius: 3px
  padding: 3px
  margin: 3px
  
.recipe-name
  font-weight: bold

            
          
!
            
              class RecipeBox extends React.Component {
  constructor(props) {
    super(props);

    // some dummy recipes to start out
    const initRecipes = [];
    // put them in state
    this.state = {
      recipes: initRecipes,
      foodName: "test",
      ingredientNames: "test2",
      numRemove: 0,
      numChange: 0
    };

    // bind *this* to our class method
    this.handleRemove = this.handleRemove.bind(this);
    this.handleAdd = this.handleAdd.bind(this);
    this.handleFoodChange = this.handleFoodChange.bind(this);
    this.handleIngredChange = this.handleIngredChange.bind(this);
    this.handleNumRemove = this.handleNumRemove.bind(this);
    this.handleNumChange = this.handleNumChange.bind(this);
    this.handleEdit = this.handleEdit.bind(this);
    this.showIngred = this.showIngred.bind(this);
  }

  showIngred(x) {
    var index = x;

    
    
    if (
      document
        .getElementsByClassName("recipe-name")
        [index].innerHTML.indexOf(this.state.recipes[index].ingredients) == -1
    )
      document.getElementsByClassName("recipe-name")[
        index
      ].innerHTML += this.state.recipes[index].ingredients;
    else {
      document.getElementsByClassName("recipe-name")[
        index
      ].innerHTML = (index+1+" ")+ this.state.recipes[index].name + ":";
    }
  }

  handleAdd(event) {
    var newArray = this.state.recipes.slice();
    newArray.push({
      name:  this.state.foodName,
      ingredients: this.state.ingredientNames
    });

    alert(this.state.foodName);

    this.setState({ recipes: newArray });

    localStorage.setItem("_gphalen_array", JSON.stringify(newArray));

    event.preventDefault();
  }

  handleFoodChange(event) {
    this.setState({ foodName: event.target.value });

    event.preventDefault();
  }

  handleIngredChange(event) {
    this.setState({ ingredientNames: event.target.value });

    event.preventDefault();
  }

  handleNumRemove(event) {
    this.setState({ numRemove: event.target.value });

    event.preventDefault();
  }

  handleNumChange(event) {
    this.setState({ numChange: event.target.value });

    event.preventDefault();
  }

  handleRemove() {
    // create a new copy of the recipes so we can change it
       console.log('handleRemove')
    let newRecipes = this.state.recipes.slice();
  console.log('recipes before', newRecipes)
    newRecipes.splice(this.state.numRemove - 1, 1);
     console.log('recipes after', newRecipes)
    // put it into state
    this.setState({ recipes: newRecipes });
        localStorage.setItem("_gphalen_array", JSON.stringify(newRecipes));

    localStorage.setItem("_gphalen_array", JSON.stringify(newRecipes));
  }

  handleEdit() {
    var editArray = this.state.recipes;

    editArray[this.state.numChange - 1].ingredients = document.getElementById(
      "area"
    ).value;

    localStorage.setItem("_gphalen_array", JSON.stringify(editArray));
  }
  componentDidMount() {
    let lsData = JSON.parse(localStorage.getItem("_gphalen_array"));
    if (lsData != null) this.setState({ recipes: lsData });
  }
  render() {
    // just a simple way of changing out array of data into an array of JSX
    // I could have put it directly in the JSX, but this is cleaner

    const renderRecipes = this.state.recipes.map((recipe, i) => (
      
      <div className="recipe">
        <span className="recipe-name">{i+1}{" "+recipe.name}:</span> &nbsp;
      </div>
        
        
    ));

    return (
      <div>
        <div onClick={() => this.showIngred(event.target.innerText[0]-1 )}>
          {renderRecipes}
        </div>
        <br />
        <div>
          <button
            onClick={this.handleRemove}
            disabled={this.state.recipes.length === 0}
          >
            Remove recipe:
          </button>

          <label>
            <input
              type="text"
              value={this.state.numRemove}
              onChange={this.handleNumRemove}
            />
          </label>
        </div>
        <br />
        <div>
          <button
            onClick={this.handleEdit}
            disabled={this.state.recipes.length === 0}
          >
            Edit recipe:
          </button>

          <label>
            <input
              type="text"
              value={this.state.numChange}
              onChange={this.handleNumChange}
            />
          </label>
        </div>
        <textarea id="area">Place new ingredients here. </textarea>
        <br />
        <form onSubmit={this.handleAdd}>
          <label>
            Food:
            <input
              type="text"
              value={this.state.foodName}
              name="food"
              onChange={this.handleFoodChange}
            />
          </label>
          <label>
            Ingredients
            <input
              type="text"
              name="ingredients"
              value={this.state.ingredientNames}
              onChange={this.handleIngredChange}
            />
          </label>
          <input type="submit" value="Submit" />
        </form>
      </div>
    );
  }
}
ReactDOM.render(<RecipeBox />, document.getElementById("root"));

            
          
!
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