@material-ui/icons#DeleteForever JavaScript Examples

The following examples show how to use @material-ui/icons#DeleteForever. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EditableImageList.js    From beluga with GNU General Public License v3.0 5 votes vote down vote up
EditableImageList = ({ storeConfig, products, handleChange }) => {

  const onSort = list => {
    const sorted = list.sort((a, b) => a.rank - b.rank)
      .map(p => p.product.stripe_id);
    handleChange("products", sorted)
  }

  const removeProduct = i => {
    let newProducts = [...products];
    newProducts.splice(i, 1);
    handleChange("products", newProducts)
  }

  const imageList = [...products].map((id, i) => {
    const product = storeConfig.products.find(p => p.stripe_id === id);

    if (!product) return (null);

    const img_url = product.photos && !!product.photos.length ?
      `url("${process.env.PUBLIC_URL}/assets/${id}/${product.photos[0].name}")` :
      null;

    return ({
      content: (
        <IMGWrapper key={`image${i}`}>
          <IMG style={{ backgroundImage: img_url }} />
          <div style={{ fontSize: "12px" }}>{product.name}</div>
          <IconButton
            color="inherit" aria-label="Delete"
            className="drag-handle"
          >
            <DragIndicator />
          </IconButton>
          <IconButton
            color="inherit" aria-label="Delete"
            className="delete no-drag"
            onClick={() => removeProduct(i)}
          >
            <DeleteForever />
          </IconButton>
        </IMGWrapper>
      ),
      product
    })
  })

  return (
    <div style={{ minHeight: imageList.length ? "150px" : 0, margin: "20px 0" }}>
      <DragSortableList items={imageList} onSort={onSort} type="horizontal" />
    </div>
  );
}
Example #2
Source File: ProductStep4.js    From beluga with GNU General Public License v3.0 4 votes vote down vote up
function ProductStep4(props) {
  const { product, handleChange, saveConfig } = props;
  const { photos = [] } = product;

  const uploadImage = event => {
    const formData = new FormData()
    // removing all special characters except the last period
    let filename = event.target.files[0].name.replace(/[.](?=.*[.])/g, "").replace(/[^a-zA-Z0-9-_.]/g, '')
    formData.append("file", event.target.files[0], filename)

    fetch(`/upload-image/${product.stripe_id}`, {
        method: 'POST',
        body: formData
      }).then(response => response.json())
      .then(result => {
        if (!result.success) return;

        const photoArray = [...photos];
        photoArray.push({
          name: filename,
          width: result.width,
          height: result.height
        });
        handleChange("photos", photoArray)
        saveConfig();
      })
      .catch(error => console.log(error));
  };

  const deleteImage = (name, index) => {
    fetch(`/delete-image/${product.stripe_id}/${name}`, {
        method: 'GET'
      }).then(response => response.json())
      .then(result => {
        if (!result.success) return;

        const photoArray = [...photos];
        photoArray.splice(index, 1);
        handleChange("photos", photoArray)
        saveConfig();
      })
      .catch(error => console.log(error));
  }

  const onSort = list => {
    const sorted = list.sort((a, b) => a.rank - b.rank).map(p => p.photo);
    handleChange("photos", sorted)
    saveConfig();
  }

  const list = photos.map((p, i) => {
    const aspect = getAspect(p);
    return ({
      content: (
        <IMGWrapper key={`image${i}`} 
          style={{
            width: aspect !== "vertical" ? 200 : 150,
            height: aspect !== "horizontal" ? 200 : 150,
            marginTop: aspect !== "horizontal" ? 0 : 25
          }}
        >
          <IMG style={{ backgroundImage: `url("${process.env.PUBLIC_URL}/assets/${product.stripe_id}/${p.name}")` }}/>
          <IconButton
            color="inherit" aria-label="Delete"
            className="drag-handle"
          >
            <DragIndicator />
          </IconButton>
          <IconButton
            color="inherit" aria-label="Delete"
            className="delete no-drag"
            onClick={() => deleteImage(p,i.name)}
          >
            <DeleteForever />
          </IconButton>
        </IMGWrapper>
      ),
      photo: p
    })
  })

  return (
    <Wrapper>
      <DragSortableList items={list} onSort={onSort} type="horizontal" />
      <div>
        <Button
          component="label" variant="contained" color="secondary"
          style={{ marginTop: "40px" }}
        >
          <input
            accept="image/*"
            type="file"
            onChange={uploadImage}
            style={{ display: "none" }}
          />
          Upload Image
        </Button>
      </div>
    </Wrapper>
  );
}