Last active
August 31, 2018 22:45
-
-
Save funador/54656598d450dca2b0795393c19de9e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import Spinner from './Spinner' | |
import Images from './Images' | |
import Buttons from './Buttons' | |
import { API_URL } from './config' | |
import './App.css' | |
export default class App extends Component { | |
state = { | |
uploading: false, | |
images: [] | |
} | |
onChange = e => { | |
const files = Array.from(e.target.files) | |
this.setState({ uploading: true }) | |
const formData = new FormData() | |
files.forEach((file, i) => { | |
formData.append(i, file) | |
}) | |
fetch(`${API_URL}/image-upload`, { | |
method: 'POST', | |
body: formData | |
}) | |
.then(res => res.json()) | |
.then(images => { | |
this.setState({ | |
uploading: false, | |
images | |
}) | |
}) | |
} | |
removeImage = id => { | |
this.setState({ | |
images: this.state.images.filter(image => image.public_id !== id) | |
}) | |
} | |
render() { | |
const { uploading, images } = this.state | |
const content = () => { | |
switch(true) { | |
case uploading: | |
return <Spinner /> | |
case images.length > 0: | |
return <Images images={images} removeImage={this.removeImage} /> | |
default: | |
return <Buttons onChange={this.onChange} /> | |
} | |
} | |
return ( | |
<div> | |
<div className='buttons'> | |
{content()} | |
</div> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment