Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id='app'>
</div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Roboto:300,400');
@import url('//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css');

$accent-01: #536DFE;
$confirm-green: #43A047;

body{
  font-family: 'Roboto', sans-serif;
  margin:0px;
}

.Footer {
  box-sizing:border-box;
  width:100%;
  background:#424242;
  padding:8px;
  position:relative;
  z-index:99;
  text-align:center;
  p{color:white; opacity:0.2;font-weight:300;}
}

.ProductsTab {
  box-sizing:border-box;
  width:100%;
  .productTable, td, th{
    border:1px solid black;
  }
  .productTable{
    width:100%;
    border-collapse:collapse;
    font-size:14px;
    td{
      text-align:center;
      padding:2px 4px;
    }
    .editButton{
      opacity:0.4;
      transition:0.2s;
      &:hover{
        opacity:1.0;
        cursor:pointer;
        color:$accent-01;
      }
    }
  }
}

.ProductCard{
  display:flex;
  flex-direction:column;
  border:1px solid grey;
  border-radius: 3px;
  width:100%;
  max-width:202px;
  box-sizing:border-box;
  box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
  .category{
    display:block;
    opacity:0.9; 
    font-size:12px;
    padding: 8px;
    padding-bottom: 4px;
    color:$accent-01;
    transition:0.2s;
    &:hover{
      cursor:pointer;
      color:lighten($accent-01,10%);
    }
  }
  .name{
    font-weight:bold;
    padding: 0px 8px;
    padding-bottom: 4px;
  }
  .price{
    text-align:right;
    padding:4px;
    font-weight:300;
    span{font-weight:bold;}
  }
  p{margin:0px;padding:0px;}
  img{width:200px; height:200px;}
}

.NewItemTab {
  display:flex;
  flex-wrap:wrap;
  .formCheck-err{
    border-color:red;
  }
  button{
    border:1px solid black;
    margin-top:24px;
    margin-left:20%;
    border-radius:3px;
    background:inherit;
    padding:8px 16px;
    outline:none;
    font-weight:bold;
    font-family:inherit;
    transition:0.2s;
    &:hover{
      cursor:pointer;
      color: $confirm-green;
      border-color:$confirm-green;
    }
  }
  label{
    display:block;
    font-weight:bold;
    letter-spacing:1px;
    font-size:12px;
    margin-bottom:4px;
  }
  input{
    font-family:inherit;
    padding:4px 8px;
    border:1px solid #BDBDBD;
    border-radius:3px;
    outline:none;
    &:focus{
      border-color: $accent-01;
    }
  }
  select{
    font-family:inherit;
    outline:none;
    padding:4px 8px;
    border-radius:3px;
    &:focus{
      border-color: $accent-01;
    }
    option:hover{
      font-family:inherit;
      background-color:$accent-01;
      padding:4px 8px;
    }
  }
  .newItem-input{
    box-sizing:border-box;
    min-width:200px;
    width:30%;
    margin-bottom:32px;
    padding-right:24px;
  }
  .newItem-preview{
    box-sizing:border-box;
    min-width:202px;
    width:50%;
  }
}


.InventoryManagementApp{
  min-height:80vh;
  .header{
    box-sizing:border-box;
    width:100%;
    background:#424242;
    margin:0px;
    padding:8px;
    font-size: 20px;
    color:white;
    font-weight:300;
    .bold-header{font-weight:400;}
  }
  .title{
    box-sizing:border-box;
    width:100%;
    background:#3D5AFE;
    color:white;
    margin:0px;
    padding:16px 8px;
    position:relative;
    z-index:99;
  }
  
  
  .app-body{
    display:flex;
    height:100%;
    min-height:80vh;
  }
  
}

.Sidebar{
  width:100%;
  max-width:200px;
  background:#ECEFF1;
  ul{
    margin:0px;
    padding:0px;
    list-style-type:none;
    .add-new-item{
      background:$confirm-green;
      color:white;
      font-weight:400;
      opacity:1;
      position:relative;
      z-index:3;
      &:hover{
        background:lighten($confirm-green, 10%);
      }
    }
    .active{
      opacity:1;
      background:#fff;
      position:relative;
      z-index:3;
      &:hover{
        background:#fff;
        cursor:default;
      }
    }
    li{
      box-sizing:border-box;
      text-align:right;
      width:100%;
      padding:16px;
      transition:0.2s all;
      font-weight:300;
      &:hover{
        background:#CFD8DC;
        cursor:pointer;
      }
    }
  }
}

.MyRouter{
  width:100%;
  box-shadow: -3px 0px 6px rgba(0,0,0,0.16);
  box-sizing:border-box;
  padding:16px 24px;
  position:relative;
  z-index:2;
  overflow:hidden;
  h1{opacity:0.9;font-size:24px;}
}
              
            
!

JS

              
                
class ProductCard extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      category: props.product.category,
      name: props.product.name,
      imageURL: 'http://via.placeholder.com/200x200',
      checkURL: props.product.imageURL,
      price: props.product.price,
    }
    this.checkImage.bind(this);
    this.checkImage();
  }
  
  componentWillReceiveProps(nextProps){
    if(nextProps.product.category != this.state.category){
      this.setState({category: nextProps.product.category});
    }
    if(nextProps.product.name != this.state.name){
      this.setState({name: nextProps.product.name});
    }
    if(nextProps.product.price != this.state.price){
      this.setState({price: nextProps.product.price});
    }
    if(nextProps.product.imageURL != this.state.imageURL){
      this.setState({checkURL: nextProps.product.imageURL}, () => this.checkImage());
    }
  }

  checkImage(){
    let image = new Image();
    image.onerror = () =>{
      this.setState({imageURL: 'http://via.placeholder.com/200x200'});
    }
    image.onload = () =>{
      this.setState({imageURL: this.state.checkURL});
    }
    image.src = this.state.checkURL;
  }
  
  render(){
    return(
      <div className = 'ProductCard'>
        <p className='category'>Products &#187; {this.state.category}</p>
        <p className='name'>{this.state.name}</p>
        <img src={this.state.imageURL}></img>
        <p className='price'>from <span>${this.state.price}</span></p>
      </div>
    );
  }
} 


class NewItemTab extends React.Component {
  constructor() {
    super();
    this.state = {
      formErrors: {
        category: false,
        name: false,
        price: false,
        imageURL: false
      }
    }
  }
  
  checkForm(){
    let category = document.getElementById('newItemForm-category').value;
    let name = document.getElementById('newItemForm-name').value;
    let price = document.getElementById('newItemForm-price').value;
    let imageURL = document.getElementById('newItemForm-imageURL').value;
    let product = {category: category, name: name, price: price, imageURL: imageURL};
    let errors = this.state.formErrors;
    category.length == 0 ? errors.category = true : errors.category = false;
    name.length == 0 ? errors.name = true : errors.name = false;
    price.length == 0 ? errors.price = true : errors.price = false;
    
    let image = new Image();
    image.onerror = () =>{
      this.finalizeForm(false, product);
    }
    image.onload = () =>{
      this.finalizeForm(true, product);
    }
    this.setState({formErrors: errors});
    image.src = imageURL;
  }
  
  finalizeForm(isImageURLValid, product){
    
    if(isImageURLValid == false){
      let errors = this.state.formErrors;
      errors.imageURL = true;
      this.setState({formErrors: errors});
    } else {
      this.props.addNewProduct(product);
    }
  }
  
  renderCategorySelections(inventory){
    const categoryKeys = Object.keys(inventory.categories);
    const CKLength = categoryKeys.length;
    let options = [];
    
    const capitalize = (string) => {
      return string.charAt(0).toUpperCase() + string.slice(1);
    }
    for(let i = 0; i<CKLength; i++){
      options.push(<option>{capitalize(categoryKeys[i])}</option>);
    }
    return options;
  }
  
  updateForm(){
    let category = document.getElementById('newItemForm-category').value;
    let name = document.getElementById('newItemForm-name').value;
    let price = document.getElementById('newItemForm-price').value;
    let imageURL = document.getElementById('newItemForm-imageURL').value;
    let errors = this.state.formErrors;
    if(this.props.formData.category != category){
      errors.category = false;
    }
    if(this.props.formData.name != name){
      errors.name = false;
    }
    if(this.props.formData.price != price){
      errors.price = false;
    }
    if(this.props.formData.imageURL != imageURL){
      errors.imageURL= false;
    }
    this.setState({formErrors: errors});
    
    this.props.changeForm({category: category, name: name, price: price, imageURL: imageURL});
  }
  
  render(){
    return(
      <div className='NewItemTab'>
        <div className='newItem-input'>
          <h1>Add A New Item</h1>
          <p>
            <label>Category</label>
            <select className={this.state.formErrors.category == true ? 'formCheck-err' : ''} id='newItemForm-category' value={this.props.formData.category} onChange={() => this.updateForm()}>
              <option></option>{this.renderCategorySelections(this.props.inventory)}
            </select>
          </p>
          <p>
            <label>Product Name</label>
            <input className={this.state.formErrors.name == true ? 'formCheck-err' : ''} type='text' required id='newItemForm-name' value={this.props.formData.name} onChange={() => this.updateForm()}></input>
          </p>
          <p>
            <label>Price per Unit</label>
            <input className={this.state.formErrors.price == true ? 'formCheck-err' : ''} type='number' required id='newItemForm-price' value={this.props.formData.price} onChange={() => this.updateForm()}></input>
          </p>
          <p>
            <label>Image URL</label>
            <input className={this.state.formErrors.imageURL == true ? 'formCheck-err' : ''} type='text' required id='newItemForm-imageURL' value={this.props.formData.imageURL} onChange={() => this.updateForm()} placeholder='Paste link here'></input>
          </p>
          <button onClick={() => this.checkForm()}>Add Product</button>
        </div>
        <div className='newItem-preview'>
          <h1>Preview</h1>
          <ProductCard product={this.props.formData} />
          
        </div>
        
      </div>
    );
  }
}


const ProductTableRow = (props) => {
  console.log()
  return (
    <tr>
      <td>{props.product.name}</td>
      <td>${props.product.price}</td>
      <td>{props.product.category}</td>
      <td><a target="_blank" href={props.product.imageURL}>View</a></td>
      <td className='editButton'>edit</td>
    </tr>
  );
}

class ProductsTab extends React.Component {
  
  renderTableRows(inventory){
    const categoryKeys = Object.keys(inventory.categories);
    const CKLength = categoryKeys.length;
    let listOfProducts = [];
    
    for(let i = 0; i<CKLength; i++){
      let category = categoryKeys[i];
      listOfProducts = listOfProducts.concat(inventory.categories[category]);
    }
    
    let LOPlength = listOfProducts.length;
    if( LOPlength == 0){
      return <div><p>There are currently no items in the inventory</p></div>
    } else {
      let rows = [
       <tr>
        <th>Product Name</th>
        <th>Price</th>
        <th>Category</th>
        <th>Image</th>
      </tr>
      ];
      
      for(let i = 0; i<LOPlength; i++){
        rows.push(<ProductTableRow product={listOfProducts[i]}/>);
      }
      
      return rows;
    }
  }
  
  render(){
    return(
      <div className='ProductsTab'>
        <h1>Available Products List</h1>
        <p>Showing all available products:</p>
        <table className='productTable'>
          {this.renderTableRows(this.props.inventory)}
        </table>
      </div>
    );
  }
  
}


class MyRouter extends React.Component {
  
  route(active){
    switch(active){
      case 0:
        return <NewItemTab 
                 inventory={this.props.inventory}
                 formData={this.props.newItemFormData}
                 changeForm={this.props.changeNewItemForm}
                 addNewProduct={this.props.addNewProduct}
                 />
        break;
      case 1:
        return <ProductsTab inventory={this.props.inventory}/>;
        break;
    }
  }
  
  render(){
    return(
      <div className='MyRouter'>
        {this.route(this.props.activeTab)}
      </div>
    );
  }
}

const Sidebar = (props) => {
  return(
    <div className='Sidebar'>
      <ul>
        <li className='add-new-item' onClick={() => props.changeTab(0)}><span>Add New Item</span></li>
        <li className={props.activeTab == 1 ? 'active':''} onClick={() => props.changeTab(1)}>Products</li>
        <li className={props.activeTab == 2 ? 'active':''} onClick={() => props.changeTab(2)}>Categories</li>
        <li className={props.activeTab == 3 ? 'active':''} onClick={() => props.changeTab(3)}>Item Archive</li>
      </ul>
    </div>
  );
}

const Footer = () => {
  return(
    <div className='Footer'>
      <p>Current work-in-progress for an e-commerce dashboard.</p>
    </div>
  );
}


class InventoryManagementApp extends React.Component{
  constructor(){
    super();
    this.state = {
      activeTab: 1,
      inventory: {
        categories:{
          dresses:[],
          shirts:[
{category: "shirts", name: "Blue T-Shirt", price: "16.99", imageURL: "https://cdn.shopify.com/s/files/1/0797/0169/products/mockup-c6d64730_1024x1024.jpg"}],
          pants:[],
          accessories:[]
        }
      },
      newItemForm: {
        category: '',
        name: '',
        price: '',
        imageURL: ''
      }
    };
  }
  
  changeActiveTab(index){
    this.setState({activeTab: index});
  }
  
  changeNewItemForm(formData){
    this.setState({newItemForm: formData});
  }
  
  addNewProduct(product){
    
    this.setState({newItemForm: {category: '',name: '',price: '',imageURL: ''}});
    
    const decapitalize = (string) => {
      return string.charAt(0).toLowerCase() + string.slice(1);
    }
    
    product.category = decapitalize(product.category);
    let inventory = this.state.inventory;
    inventory.categories[product.category].push(product);
    
    this.setState({inventory:inventory});
  }
  
  render(){
    return(
      <div className='InventoryManagementApp'>
        <h2 className='header'><i className="icon-th-list"></i> Inventory Management Application Demo</h2>
        <h1 className='title' onClick={() => this.changeActiveTab(1)}>Inventory</h1>
        <div className='app-body'>
          <Sidebar activeTab={this.state.activeTab} changeTab={this.changeActiveTab.bind(this)}/>
          <MyRouter 
            activeTab={this.state.activeTab} 
            inventory={this.state.inventory}
            newItemFormData={this.state.newItemForm}
            changeNewItemForm={this.changeNewItemForm.bind(this)}
            addNewProduct={this.addNewProduct.bind(this)}
            />
        </div>
        <Footer/>
      </div>
    );
  }
}


ReactDOM.render(
  <InventoryManagementApp/>,
  document.getElementById('app')
);
              
            
!
999px

Console