Last active
October 5, 2017 18:57
-
-
Save babie/7ddf1db5b848f3f8fc9a5c6bb547004d to your computer and use it in GitHub Desktop.
ダサい。助けてくれ……できれば async/await で……→こんな感じか?
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
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 | |
} |
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 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