Skip to content

Instantly share code, notes, and snippets.

@babie
Last active October 5, 2017 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save babie/7ddf1db5b848f3f8fc9a5c6bb547004d to your computer and use it in GitHub Desktop.
Save babie/7ddf1db5b848f3f8fc9a5c6bb547004d to your computer and use it in GitHub Desktop.
ダサい。助けてくれ……できれば async/await で……→こんな感じか?
export function encode (blob) {
return new Promise((resolve) => {
const r = new FileReader()
r.onload = () => {
const dataURL = r.result
const base64 = dataURL.split(',', 2)[1]
resolve(base64)
}
r.readAsDataURL(blob)
})
}
export default {
encode
}
import Base64 from './base64'
function createProduct (values) {
return async (dispatch, getState) => {
const tempfiles = getState().productsReducer.tempfiles
let imageObjects = []
for (const f of tempfiles) {
imageObjects.push({
contentType: f.type,
base64Str: await Base64.encode(f)
})
}
const body = {
...values,
price: Number(values.price),
imageObjects: imageObjects
}
return API.post('/api/products', body)
.then(json => dispatch({
type: 'CREATE_PRODUCT',
payload: json
})).catch(err => dispatch({
type: 'CREATE_PRODUCT_ERROR',
payload: err,
error: true
}))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment