/** @format */ /** * 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'; import { DropZoneProvider, DropZone, FormFileUpload } from '@wordpress/components'; import Gridicon from 'gridicons'; import { noop } from 'lodash'; import PropTypes from 'prop-types'; import { withDispatch } from '@wordpress/data'; /** * WooCommerce dependencies */ 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 ) { const { addNotice, onUploadComplete } = this.props; this.setState( { isUploading: true } ); const body = new FormData(); body.append( 'pluginzip', file ); return apiFetch( { path: '/wc-admin/v1/themes', method: 'POST', body } ) .then( response => { onUploadComplete( response ); this.setState( { isUploading: false } ); addNotice( { status: response.status, message: response.message } ); } ) .catch( error => { this.setState( { isUploading: false } ); if ( error && error.message ) { addNotice( { status: 'error', message: error.message } ); } } ); } render() { const { className } = this.props; const { isUploading } = this.state; const classes = classnames( 'woocommerce-theme-uploader', className, { 'is-uploading': isUploading, } ); return ( { ! isUploading ? ( { __( 'Upload a theme', 'woocommerce-admin' ) }

{ __( 'Drop a theme zip file here to upload', 'woocommerce-admin' ) }

) : ( { __( 'Uploading theme', 'woocommerce-admin' ) }

{ __( 'Your theme is being uploaded', 'woocommerce-admin' ) }

) }
); } } 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( withDispatch( dispatch => { const { addNotice } = dispatch( 'wc-admin' ); return { addNotice }; } ) )( ThemeUploader );