Use image preview to display the cover image recently uploaded. (#7647)

This commit is contained in:
Franco Montenegro 2022-07-13 20:09:11 -04:00 committed by GitHub
parent c69826a887
commit 38200b9912
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 10 deletions

View file

@ -94,10 +94,11 @@ function ChannelForm(props: Props) {
const [nameError, setNameError] = React.useState(undefined); const [nameError, setNameError] = React.useState(undefined);
const [bidError, setBidError] = React.useState(''); const [bidError, setBidError] = React.useState('');
const [isUpload, setIsUpload] = React.useState({ cover: false, thumbnail: false }); const [isUpload, setIsUpload] = React.useState({ cover: false, thumbnail: false });
const [coverError, setCoverError] = React.useState(false);
const [thumbError, setThumbError] = React.useState(false); const [thumbError, setThumbError] = React.useState(false);
const { claim_id: claimId } = claim || {}; const { claim_id: claimId } = claim || {};
const [params, setParams]: [any, (any) => void] = React.useState(getChannelParams()); const [params, setParams]: [any, (any) => void] = React.useState(getChannelParams());
const [coverError, setCoverError] = React.useState(false);
const [coverPreview, setCoverPreview] = React.useState(params.coverUrl);
const { channelName } = parseURI(uri); const { channelName } = parseURI(uri);
const name = params.name; const name = params.name;
const isNewChannel = !uri; const isNewChannel = !uri;
@ -206,7 +207,8 @@ function ChannelForm(props: Props) {
setThumbError(false); setThumbError(false);
} }
function handleCoverChange(coverUrl: string, uploadSelected: boolean) { function handleCoverChange(coverUrl: string, uploadSelected: boolean, preview: ?string) {
setCoverPreview(preview || '');
setParams({ ...params, coverUrl }); setParams({ ...params, coverUrl });
setIsUpload({ ...isUpload, cover: uploadSelected }); setIsUpload({ ...isUpload, cover: uploadSelected });
setCoverError(false); setCoverError(false);
@ -259,7 +261,7 @@ function ChannelForm(props: Props) {
} }
}, [hasClaimedInitialRewards, claimInitialRewards]); }, [hasClaimedInitialRewards, claimInitialRewards]);
const coverSrc = coverError ? ThumbnailBrokenImage : params.coverUrl; const coverSrc = coverError ? ThumbnailBrokenImage : coverPreview;
let thumbnailPreview; let thumbnailPreview;
if (!params.thumbnailUrl) { if (!params.thumbnailUrl) {
@ -281,7 +283,7 @@ function ChannelForm(props: Props) {
title={__('Cover')} title={__('Cover')}
onClick={() => onClick={() =>
openModal(MODALS.IMAGE_UPLOAD, { openModal(MODALS.IMAGE_UPLOAD, {
onUpdate: (coverUrl, isUpload) => handleCoverChange(coverUrl, isUpload), onUpdate: (coverUrl, isUpload, preview) => handleCoverChange(coverUrl, isUpload, preview),
title: __('Edit Cover Image'), title: __('Edit Cover Image'),
helpText: __('(6.25:1)'), helpText: __('(6.25:1)'),
assetName: __('Cover Image'), assetName: __('Cover Image'),

View file

@ -15,15 +15,35 @@ const SPEECH_UPLOADING = 'UPLOADING';
type Props = { type Props = {
assetName: string, assetName: string,
currentValue: ?string, currentValue: ?string,
onUpdate: (string, boolean) => void, onUpdate: (string, boolean, ?string) => void,
recommended: string, recommended: string,
title: string, title: string,
onDone?: () => void, onDone?: () => void,
inline?: boolean, inline?: boolean,
// When uploading pictures, the upload service
// can return success but the image isn't ready
// to be displayed yet. This is when a local preview
// comes in handy. The preview (base 64) will be
// passed to the onUpdate function after the
// upload service returns success.
buildImagePreview?: boolean,
}; };
function filePreview(file) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.onload = () => {
resolve(reader.result.toString());
};
reader.onerror = () => {
resolve(undefined);
};
reader.readAsDataURL(file);
});
}
function SelectAsset(props: Props) { function SelectAsset(props: Props) {
const { onUpdate, onDone, assetName, currentValue, recommended, title, inline } = props; const { onUpdate, onDone, assetName, currentValue, recommended, title, inline, buildImagePreview } = props;
const [pathSelected, setPathSelected] = React.useState(''); const [pathSelected, setPathSelected] = React.useState('');
const [fileSelected, setFileSelected] = React.useState<any>(null); const [fileSelected, setFileSelected] = React.useState<any>(null);
const [uploadStatus, setUploadStatus] = React.useState(SPEECH_READY); const [uploadStatus, setUploadStatus] = React.useState(SPEECH_READY);
@ -36,9 +56,15 @@ function SelectAsset(props: Props) {
setError(error); setError(error);
}; };
const onSuccess = (thumbnailUrl) => { const onSuccess = async (thumbnailUrl) => {
let preview;
setUploadStatus(SPEECH_READY); setUploadStatus(SPEECH_READY);
onUpdate(thumbnailUrl, !useUrl);
if (buildImagePreview) {
preview = await filePreview(fileSelected);
}
onUpdate(thumbnailUrl, !useUrl, preview);
if (onDone) { if (onDone) {
onDone(); onDone();

View file

@ -8,7 +8,7 @@ type Props = {
currentValue: string, currentValue: string,
title: string, title: string,
helpText: string, helpText: string,
onUpdate: (string, boolean) => void, onUpdate: (string, boolean, ?string) => void,
assetName: string, assetName: string,
}; };
@ -18,11 +18,12 @@ function ModalImageUpload(props: Props) {
return ( return (
<Modal isOpen type="card" onAborted={closeModal} contentLabel={title}> <Modal isOpen type="card" onAborted={closeModal} contentLabel={title}>
<SelectAsset <SelectAsset
onUpdate={(a, b) => onUpdate(a, b)} onUpdate={onUpdate}
currentValue={currentValue} currentValue={currentValue}
assetName={assetName} assetName={assetName}
recommended={helpText} recommended={helpText}
onDone={closeModal} onDone={closeModal}
buildImagePreview
/> />
</Modal> </Modal>
); );