/**
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { BaseControl, Button } from '@wordpress/components';
import classnames from 'classnames';
import { Component, Fragment } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { ESCAPE } from '@wordpress/keycodes';
import { get } from 'lodash';
import { withDispatch } from '@wordpress/data';
import { ITEMS_STORE_NAME } from '@woocommerce/data';
import { Link, ProductImage } from '@woocommerce/components';
import { getSetting } from '@woocommerce/wc-admin-settings';
import { recordEvent } from '@woocommerce/tracks';
/**
* Internal dependencies
*/
import { ActivityCard } from '../../activity-card';
class ProductStockCard extends Component {
constructor( props ) {
super( props );
this.state = {
quantity: props.product.stock_quantity,
editing: false,
edited: false,
};
this.beginEdit = this.beginEdit.bind( this );
this.cancelEdit = this.cancelEdit.bind( this );
this.onQuantityChange = this.onQuantityChange.bind( this );
this.handleKeyDown = this.handleKeyDown.bind( this );
this.onSubmit = this.onSubmit.bind( this );
}
recordStockEvent( eventName, eventProps = {} ) {
recordEvent( `activity_panel_stock_${ eventName }`, eventProps );
}
beginEdit() {
const { product } = this.props;
this.setState(
{
editing: true,
quantity: product.stock_quantity,
},
() => {
if ( this.quantityInput ) {
this.quantityInput.focus();
}
}
);
this.recordStockEvent( 'update_stock' );
}
cancelEdit() {
const { product } = this.props;
this.setState( {
editing: false,
quantity: product.stock_quantity,
} );
this.recordStockEvent( 'cancel' );
}
handleKeyDown( event ) {
if ( event.keyCode === ESCAPE ) {
this.cancelEdit();
}
}
onQuantityChange( event ) {
this.setState( { quantity: event.target.value } );
}
async onSubmit() {
const { product, updateProductStock, createNotice } = this.props;
const { quantity } = this.state;
this.setState( { editing: false, edited: true } );
const results = await updateProductStock( product, quantity );
if ( results.success ) {
createNotice(
'success',
sprintf(
__( '%s stock updated.', 'woocommerce-admin' ),
product.name
)
);
} else {
createNotice(
'error',
sprintf(
__( '%s stock could not be updated.', 'woocommerce-admin' ),
product.name
)
);
}
this.recordStockEvent( 'save', {
quantity,
} );
}
getActions() {
const { editing } = this.state;
if ( editing ) {
return [
,
,
];
}
return [
,
];
}
getBody() {
const { product } = this.props;
const { editing, quantity } = this.state;
if ( editing ) {
return (