2019-07-08 02:54:26 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
|
|
|
import { compose } from '@wordpress/compose';
|
2020-02-14 02:23:21 +00:00
|
|
|
import {
|
|
|
|
DropZoneProvider,
|
|
|
|
DropZone,
|
|
|
|
FormFileUpload,
|
|
|
|
} from '@wordpress/components';
|
2019-07-08 02:54:26 +00:00
|
|
|
import Gridicon from 'gridicons';
|
|
|
|
import { noop } from 'lodash';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { withDispatch } from '@wordpress/data';
|
|
|
|
import { Card, H, Spinner } from '@woocommerce/components';
|
|
|
|
|
|
|
|
class ThemeUploader extends Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
isUploading: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.handleFilesUpload = this.handleFilesUpload.bind( this );
|
|
|
|
this.handleFilesDrop = this.handleFilesDrop.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFilesDrop( files ) {
|
|
|
|
const file = files[ 0 ];
|
|
|
|
this.uploadTheme( file );
|
|
|
|
}
|
|
|
|
|
|
|
|
handleFilesUpload( e ) {
|
|
|
|
const file = e.target.files[ 0 ];
|
|
|
|
this.uploadTheme( file );
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadTheme( file ) {
|
2019-07-23 03:26:46 +00:00
|
|
|
const { createNotice, onUploadComplete } = this.props;
|
2019-07-08 02:54:26 +00:00
|
|
|
this.setState( { isUploading: true } );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const body = new window.FormData();
|
2019-07-08 02:54:26 +00:00
|
|
|
body.append( 'pluginzip', file );
|
|
|
|
|
2019-11-12 18:15:55 +00:00
|
|
|
return apiFetch( { path: '/wc-admin/themes', method: 'POST', body } )
|
2020-02-14 02:23:21 +00:00
|
|
|
.then( ( response ) => {
|
2019-07-08 02:54:26 +00:00
|
|
|
onUploadComplete( response );
|
|
|
|
this.setState( { isUploading: false } );
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice( response.status, response.message );
|
2019-07-08 02:54:26 +00:00
|
|
|
} )
|
2020-02-14 02:23:21 +00:00
|
|
|
.catch( ( error ) => {
|
2019-07-08 02:54:26 +00:00
|
|
|
this.setState( { isUploading: false } );
|
|
|
|
if ( error && error.message ) {
|
2019-07-23 03:26:46 +00:00
|
|
|
createNotice( 'error', error.message );
|
2019-07-08 02:54:26 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { className } = this.props;
|
|
|
|
const { isUploading } = this.state;
|
|
|
|
|
|
|
|
const classes = classnames( 'woocommerce-theme-uploader', className, {
|
|
|
|
'is-uploading': isUploading,
|
|
|
|
} );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Card className={ classes }>
|
|
|
|
<DropZoneProvider>
|
|
|
|
{ ! isUploading ? (
|
|
|
|
<Fragment>
|
2020-02-14 02:23:21 +00:00
|
|
|
<FormFileUpload
|
|
|
|
accept=".zip"
|
|
|
|
onChange={ this.handleFilesUpload }
|
|
|
|
>
|
2019-07-08 02:54:26 +00:00
|
|
|
<Gridicon icon="cloud-upload" />
|
|
|
|
<H className="woocommerce-theme-uploader__title">
|
2020-02-14 02:23:21 +00:00
|
|
|
{ __(
|
|
|
|
'Upload a theme',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-07-08 02:54:26 +00:00
|
|
|
</H>
|
2020-02-14 02:23:21 +00:00
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
'Drop a theme zip file here to upload',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</p>
|
2019-07-08 02:54:26 +00:00
|
|
|
</FormFileUpload>
|
|
|
|
<DropZone
|
2020-02-14 02:23:21 +00:00
|
|
|
label={ __(
|
|
|
|
'Drop your theme zip file here',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
2019-07-08 02:54:26 +00:00
|
|
|
onFilesDrop={ this.handleFilesDrop }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
) : (
|
|
|
|
<Fragment>
|
|
|
|
<Spinner />
|
|
|
|
<H className="woocommerce-theme-uploader__title">
|
|
|
|
{ __( 'Uploading theme', 'woocommerce-admin' ) }
|
|
|
|
</H>
|
2020-02-14 02:23:21 +00:00
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
'Your theme is being uploaded',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</p>
|
2019-07-08 02:54:26 +00:00
|
|
|
</Fragment>
|
|
|
|
) }
|
|
|
|
</DropZoneProvider>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ThemeUploader.propTypes = {
|
|
|
|
/**
|
|
|
|
* Additional class name to style the component.
|
|
|
|
*/
|
|
|
|
className: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* Function called when an upload has finished.
|
|
|
|
*/
|
|
|
|
onUploadComplete: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
ThemeUploader.defaultProps = {
|
|
|
|
onUploadComplete: noop,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default compose(
|
2020-02-14 02:23:21 +00:00
|
|
|
withDispatch( ( dispatch ) => {
|
2019-07-23 03:26:46 +00:00
|
|
|
const { createNotice } = dispatch( 'core/notices' );
|
|
|
|
return { createNotice };
|
2019-07-08 02:54:26 +00:00
|
|
|
} )
|
|
|
|
)( ThemeUploader );
|