Use client-side image preview instead of server response

This commit is contained in:
Paul Kaplan 2021-05-14 16:00:15 -04:00
parent 8984f2cedc
commit 5be42442f6
2 changed files with 17 additions and 2 deletions

View file

@ -194,6 +194,18 @@ const mutateStudioImage = input => ((dispatch, getState) => {
const error = normalizeError(err, body, res);
dispatch(completeMutation('image', error ? currentImage : body.thumbnail_url, error));
});
// Return a promise with the data-url of the uploaded image
return new Promise((resolve, reject) => {
try {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = reject;
reader.readAsDataURL(input.files[0]);
} catch (e) {
reject(e);
}
});
});
const mutateStudioCommentsAllowed = shouldAllow => ((dispatch, getState) => {

View file

@ -25,11 +25,13 @@ const blankImage = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAA
const StudioImage = ({
imageError, isFetching, isMutating, image, canEditInfo, handleUpdate
}) => {
const [uploadPreview, setUploadPreview] = React.useState(null);
const fieldClassName = classNames('studio-info-section', {
'mod-fetching': isFetching,
'mod-mutating': isMutating
});
const src = isMutating ? blankImage : (image || blankImage);
let src = image || blankImage;
if (uploadPreview && !imageError) src = uploadPreview;
return (
<div className={fieldClassName}>
<img
@ -43,7 +45,8 @@ const StudioImage = ({
type="file"
accept="image/*"
onChange={e => {
handleUpdate(e.target);
handleUpdate(e.target)
.then(dataUrl => setUploadPreview(dataUrl));
e.target.value = '';
}}
/>