2019-06-28 19:00:29 +02:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { FormField } from 'component/common/form';
|
|
|
|
import FileSelector from 'component/common/file-selector';
|
|
|
|
import Button from 'component/button';
|
2019-10-07 22:02:32 +02:00
|
|
|
import { SPEECH_URLS } from 'lbry-redux';
|
2019-07-02 19:54:42 +02:00
|
|
|
import uuid from 'uuid/v4';
|
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 SOURCE_URL = 'url';
|
|
|
|
const SOURCE_UPLOAD = 'upload';
|
|
|
|
const SPEECH_READY = 'READY';
|
|
|
|
const SPEECH_UPLOADING = 'UPLOADING';
|
|
|
|
type Props = {
|
|
|
|
assetName: string,
|
|
|
|
currentValue: ?string,
|
|
|
|
onUpdate: string => void,
|
|
|
|
recommended: string,
|
|
|
|
};
|
|
|
|
|
|
|
|
function SelectAsset(props: Props) {
|
|
|
|
const { onUpdate, assetName, currentValue, recommended } = props;
|
|
|
|
const [assetSource, setAssetSource] = useState(SOURCE_URL);
|
|
|
|
const [pathSelected, setPathSelected] = useState('');
|
2019-11-15 19:52:21 +01:00
|
|
|
const [fileSelected, setFileSelected] = useState<any>(null);
|
2019-06-28 19:00:29 +02:00
|
|
|
const [uploadStatus, setUploadStatus] = useState(SPEECH_READY);
|
|
|
|
|
2019-10-07 22:02:32 +02:00
|
|
|
function doUploadAsset(file) {
|
2019-06-28 19:00:29 +02:00
|
|
|
const uploadError = (error = '') => {
|
|
|
|
console.log('error', error);
|
|
|
|
};
|
|
|
|
|
|
|
|
const setUrl = path => {
|
|
|
|
setUploadStatus(SPEECH_READY);
|
|
|
|
onUpdate(path);
|
|
|
|
setAssetSource(SOURCE_URL);
|
|
|
|
};
|
|
|
|
|
|
|
|
setUploadStatus(SPEECH_UPLOADING);
|
|
|
|
|
|
|
|
const data = new FormData();
|
2019-07-02 19:54:42 +02:00
|
|
|
const name = uuid();
|
2019-06-28 19:00:29 +02:00
|
|
|
data.append('name', name);
|
|
|
|
data.append('file', file);
|
|
|
|
|
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())
|
|
|
|
.then(json => (json.success ? setUrl(`${json.data.serveUrl}`) : uploadError(json.message)))
|
|
|
|
.catch(err => uploadError(err.message));
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<fieldset-section>
|
|
|
|
<fieldset-group className="fieldset-group--smushed">
|
|
|
|
<FormField
|
|
|
|
type="select"
|
|
|
|
name={assetName}
|
|
|
|
value={assetSource}
|
|
|
|
onChange={e => setAssetSource(e.target.value)}
|
|
|
|
label={__(assetName + ' source')}
|
|
|
|
>
|
|
|
|
<option key={'lmmnop'} value={'url'}>
|
|
|
|
URL
|
|
|
|
</option>
|
|
|
|
<option key={'lmmnopq'} value={'upload'}>
|
|
|
|
UPLOAD
|
|
|
|
</option>
|
|
|
|
</FormField>
|
|
|
|
{assetSource === SOURCE_UPLOAD && (
|
|
|
|
<>
|
|
|
|
{!pathSelected && (
|
|
|
|
<FileSelector
|
|
|
|
label={'File to upload'}
|
|
|
|
name={'assetSelector'}
|
2019-10-07 22:02:32 +02:00
|
|
|
onFileChosen={file => {
|
|
|
|
if (file.name) {
|
2019-11-15 19:52:21 +01:00
|
|
|
setPathSelected(file.path || file.name);
|
2019-10-07 22:02:32 +02:00
|
|
|
setFileSelected(file);
|
|
|
|
}
|
2019-06-28 19:00:29 +02:00
|
|
|
}}
|
2019-10-07 22:02:32 +02:00
|
|
|
accept={accept}
|
2019-06-28 19:00:29 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{pathSelected && (
|
|
|
|
<div>
|
|
|
|
{`...${pathSelected.slice(-18)}`} {uploadStatus}{' '}
|
2019-10-07 22:02:32 +02:00
|
|
|
<Button button={'primary'} onClick={() => doUploadAsset(fileSelected)}>
|
2019-06-28 19:00:29 +02:00
|
|
|
Upload
|
|
|
|
</Button>{' '}
|
|
|
|
<Button
|
|
|
|
button={'secondary'}
|
|
|
|
onClick={() => {
|
|
|
|
setPathSelected('');
|
2019-11-15 19:52:21 +01:00
|
|
|
setFileSelected(null);
|
2019-06-28 19:00:29 +02:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
Clear
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{assetSource === SOURCE_URL && (
|
|
|
|
<FormField
|
|
|
|
type={'text'}
|
|
|
|
name={'thumbnail'}
|
|
|
|
label={__(assetName + ' ' + recommended)}
|
|
|
|
placeholder={__('https://example.com/image.png')}
|
|
|
|
disabled={false}
|
|
|
|
value={currentValue}
|
|
|
|
onChange={e => {
|
|
|
|
onUpdate(e.target.value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</fieldset-group>
|
|
|
|
</fieldset-section>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SelectAsset;
|