/** @format */ /** * External dependencies */ import { __, sprintf } from '@wordpress/i18n'; import { Button, TextControl } from '@wordpress/components'; import { Component, Fragment } from '@wordpress/element'; import { compose } from '@wordpress/compose'; import { withDispatch } from '@wordpress/data'; /** * WooCommerce dependencies */ import { Link, ProductImage } from '@woocommerce/components'; /** * Internal dependencies */ import { ActivityCard } from '../../activity-card'; class ProductStockCard extends Component { constructor( props ) { super( props ); this.state = { quantity: props.product.stock_quantity, editing: false, }; this.beginEdit = this.beginEdit.bind( this ); this.cancelEdit = this.cancelEdit.bind( this ); this.onQuantityChange = this.onQuantityChange.bind( this ); this.updateStock = this.updateStock.bind( this ); } beginEdit() { this.setState( { editing: true } ); } cancelEdit() { this.setState( { editing: false, quantity: this.props.product.stock_quantity, } ); } onQuantityChange( quantity ) { this.setState( { quantity } ); } updateStock() { const { product, updateItem } = this.props; this.setState( { editing: false }, () => { let itemType = 'product'; const data = { stock_quantity: this.state.quantity, }; if ( 'variation' === product.type ) { itemType = 'variation'; data.parent_id = product.parent_id; } updateItem( itemType, product.id, data ); } ); } getActions() { const { editing } = this.state; if ( editing ) { return [ , , ]; } return [ , ]; } getBody() { const { editing, quantity } = this.state; if ( editing ) { return ( { __( 'in stock', 'woocommerce-admin' ) } ); } return ( { sprintf( __( '%d in stock', 'woocommerce-admin' ), quantity ) } ); } render() { const { product } = this.props; const title = ( { product.name } ); return ( } actions={ this.getActions() } > { this.getBody() } ); } } export default compose( withDispatch( dispatch => { const { updateItem } = dispatch( 'wc-api' ); return { updateItem, }; } ) )( ProductStockCard );