2019-06-28 19:00:29 +02:00
|
|
|
// @flow
|
|
|
|
|
2020-06-30 07:51:15 +02:00
|
|
|
import React from 'react';
|
2019-06-28 19:00:29 +02:00
|
|
|
import FileSelector from 'component/common/file-selector';
|
2019-10-07 22:02:32 +02:00
|
|
|
import { SPEECH_URLS } from 'lbry-redux';
|
2020-06-30 07:51:15 +02:00
|
|
|
import { FormField, Form } from 'component/common/form';
|
|
|
|
import Button from 'component/button';
|
|
|
|
import Card from 'component/common/card';
|
|
|
|
import { generateThumbnailName } from 'util/generate-thumbnail-name';
|
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
2019-06-28 19:00:29 +02:00
|
|
|
|
2019-10-07 22:02:32 +02:00
|
|
|
const accept = '.png, .jpg, .jpeg, .gif';
|
2019-06-28 19:00:29 +02:00
|
|
|
const SPEECH_READY = 'READY';
|
|
|
|
const SPEECH_UPLOADING = 'UPLOADING';
|
2020-06-29 21:54:07 +02:00
|
|
|
|
2019-06-28 19:00:29 +02:00
|
|
|
type Props = {
|
|
|
|
assetName: string,
|
|
|
|
currentValue: ?string,
|
|
|
|
onUpdate: string => void,
|
|
|
|
recommended: string,
|
2020-06-30 07:51:15 +02:00
|
|
|
title: string,
|
|
|
|
onDone?: () => void,
|
2019-06-28 19:00:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function SelectAsset(props: Props) {
|
2020-06-30 07:51:15 +02:00
|
|
|
const { onUpdate, onDone, assetName, recommended, title } = props;
|
|
|
|
const [pathSelected, setPathSelected] = React.useState('');
|
|
|
|
const [fileSelected, setFileSelected] = React.useState<any>(null);
|
|
|
|
const [uploadStatus, setUploadStatus] = React.useState(SPEECH_READY);
|
|
|
|
const [useUrl, setUseUrl] = usePersistedState('thumbnail-upload:mode', false);
|
|
|
|
const [url, setUrl] = React.useState('');
|
|
|
|
const [error, setError] = React.useState();
|
2019-06-28 19:00:29 +02:00
|
|
|
|
2020-06-30 07:51:15 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (pathSelected && fileSelected) {
|
|
|
|
doUploadAsset();
|
|
|
|
}
|
|
|
|
}, [pathSelected, fileSelected]);
|
|
|
|
|
|
|
|
function doUploadAsset() {
|
2019-06-28 19:00:29 +02:00
|
|
|
const uploadError = (error = '') => {
|
2020-03-13 20:54:31 +01:00
|
|
|
setError(error);
|
2019-06-28 19:00:29 +02:00
|
|
|
};
|
|
|
|
|
2020-06-30 07:51:15 +02:00
|
|
|
const onSuccess = thumbnailUrl => {
|
2019-06-28 19:00:29 +02:00
|
|
|
setUploadStatus(SPEECH_READY);
|
2020-06-30 07:51:15 +02:00
|
|
|
onUpdate(thumbnailUrl);
|
|
|
|
|
|
|
|
if (onDone) {
|
|
|
|
onDone();
|
|
|
|
}
|
2019-06-28 19:00:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
setUploadStatus(SPEECH_UPLOADING);
|
|
|
|
|
|
|
|
const data = new FormData();
|
2020-06-30 07:51:15 +02:00
|
|
|
const name = generateThumbnailName();
|
2019-06-28 19:00:29 +02:00
|
|
|
data.append('name', name);
|
2020-06-30 07:51:15 +02:00
|
|
|
data.append('file', fileSelected);
|
2019-06-28 19:00:29 +02:00
|
|
|
|
2019-10-07 22:02:32 +02:00
|
|
|
return fetch(SPEECH_URLS.SPEECH_PUBLISH, {
|
2019-06-28 19:00:29 +02:00
|
|
|
method: 'POST',
|
|
|
|
body: data,
|
|
|
|
})
|
|
|
|
.then(response => response.json())
|
2020-06-30 07:51:15 +02:00
|
|
|
.then(json => (json.success ? onSuccess(`${json.data.serveUrl}`) : uploadError(json.message)))
|
|
|
|
.catch(err => {
|
|
|
|
uploadError(err.message);
|
|
|
|
});
|
2019-06-28 19:00:29 +02:00
|
|
|
}
|
2020-03-13 20:54:31 +01:00
|
|
|
|
2019-06-28 19:00:29 +02:00
|
|
|
return (
|
2020-06-30 07:51:15 +02:00
|
|
|
<Card
|
|
|
|
title={title || __('Choose Image')}
|
|
|
|
actions={
|
|
|
|
<Form onSubmit={onDone}>
|
|
|
|
{error && <div className="error__text">{error}</div>}
|
|
|
|
{useUrl ? (
|
2020-06-29 21:54:07 +02:00
|
|
|
<FormField
|
2020-06-30 07:51:15 +02:00
|
|
|
autoFocus
|
2020-06-29 21:54:07 +02:00
|
|
|
type={'text'}
|
|
|
|
name={'thumbnail'}
|
2020-06-30 07:51:15 +02:00
|
|
|
label={`${assetName} ${recommended}`}
|
2020-06-29 21:54:07 +02:00
|
|
|
placeholder={'https://example.com/image.png'}
|
2020-06-30 07:51:15 +02:00
|
|
|
value={url}
|
2020-06-29 21:54:07 +02:00
|
|
|
onChange={e => {
|
2020-06-30 07:51:15 +02:00
|
|
|
setUrl(e.target.value);
|
2020-06-29 21:54:07 +02:00
|
|
|
onUpdate(e.target.value);
|
|
|
|
}}
|
|
|
|
/>
|
2020-06-30 07:51:15 +02:00
|
|
|
) : (
|
|
|
|
<FileSelector
|
2020-07-02 19:39:29 +02:00
|
|
|
autoFocus
|
2020-06-30 07:51:15 +02:00
|
|
|
disabled={uploadStatus === SPEECH_UPLOADING}
|
|
|
|
label={uploadStatus === SPEECH_UPLOADING ? __('Uploading...') : __('File to upload')}
|
|
|
|
name="assetSelector"
|
|
|
|
currentPath={pathSelected}
|
|
|
|
onFileChosen={file => {
|
|
|
|
if (file.name) {
|
|
|
|
setFileSelected(file);
|
|
|
|
// file.path is undefined in web but available in electron
|
|
|
|
setPathSelected(file.name || file.path);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
accept={accept}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div className="section__actions">
|
|
|
|
{onDone && (
|
|
|
|
<Button button="primary" type="submit" label={__('Done')} disabled={uploadStatus === SPEECH_UPLOADING} />
|
|
|
|
)}
|
|
|
|
<FormField
|
|
|
|
name="toggle-upload"
|
|
|
|
type="checkbox"
|
2020-07-02 19:39:29 +02:00
|
|
|
label={__('Use a URL')}
|
2020-06-30 07:51:15 +02:00
|
|
|
checked={useUrl}
|
|
|
|
onChange={() => setUseUrl(!useUrl)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
}
|
|
|
|
/>
|
2019-06-28 19:00:29 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SelectAsset;
|