This repository was archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathcard.js
More file actions
285 lines (254 loc) · 6.65 KB
/
card.js
File metadata and controls
285 lines (254 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { BaseControl, Button } from '@wordpress/components';
import classnames from 'classnames';
import { Component, Fragment } from '@wordpress/element';
import { ESCAPE } from '@wordpress/keycodes';
import { get } from 'lodash';
import { Link, ProductImage } from '@woocommerce/components';
import { recordEvent } from '@woocommerce/tracks';
import moment from 'moment';
/**
* Internal dependencies
*/
import { ActivityCard } from '~/activity-panel/activity-card';
import { getAdminSetting } from '~/utils/admin-settings';
export 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 = parseInt( this.state.quantity, 10 );
// Bail on an actual update if the quantity is unchanged.
if ( product.stock_quantity === quantity ) {
this.setState( { editing: false } );
return;
}
this.setState( { editing: false, edited: true } );
const success = await updateProductStock( product, quantity );
if ( success ) {
createNotice(
'success',
sprintf(
/* translators: %s = name of the product having stock updated */
__( '%s stock updated', 'woocommerce-admin' ),
product.name
),
{
actions: [
{
label: __( 'Undo', 'woocommerce-admin' ),
onClick: () => {
updateProductStock(
product,
product.stock_quantity
);
this.recordStockEvent( 'undo' );
},
},
],
}
);
} else {
createNotice(
'error',
sprintf(
/* translators: %s = name of the product having stock updated */
__( '%s stock could not be updated', 'woocommerce-admin' ),
product.name
)
);
}
this.recordStockEvent( 'save', {
quantity,
} );
}
getActions() {
const { editing } = this.state;
if ( editing ) {
return [
<Button key="save" type="submit" variant="primary">
{ __( 'Save', 'woocommerce-admin' ) }
</Button>,
<Button key="cancel" type="reset">
{ __( 'Cancel', 'woocommerce-admin' ) }
</Button>,
];
}
return [
<Button key="update" variant="secondary" onClick={ this.beginEdit }>
{ __( 'Update stock', 'woocommerce-admin' ) }
</Button>,
];
}
getBody() {
const { product } = this.props;
const { editing, quantity } = this.state;
if ( editing ) {
return (
<Fragment>
<BaseControl className="woocommerce-stock-activity-card__edit-quantity">
<input
className="components-text-control__input"
type="number"
value={ quantity }
onKeyDown={ this.handleKeyDown }
onChange={ this.onQuantityChange }
ref={ ( input ) => {
this.quantityInput = input;
} }
/>
</BaseControl>
<span>{ __( 'in stock', 'woocommerce-admin' ) }</span>
</Fragment>
);
}
return (
<span
className={ classnames(
'woocommerce-stock-activity-card__stock-quantity',
{
'out-of-stock': product.stock_quantity < 1,
}
) }
>
{ sprintf(
/* translators: %d = stock quantity of the product being updated */
__( '%d in stock', 'woocommerce-admin' ),
product.stock_quantity
) }
</span>
);
}
render() {
const { product } = this.props;
const { edited, editing } = this.state;
const notifyLowStockAmount = getAdminSetting(
'notifyLowStockAmount',
0
);
const lowStockAmount = Number.isFinite( product.low_stock_amount )
? product.low_stock_amount
: notifyLowStockAmount;
const isLowStock = product.stock_quantity <= lowStockAmount;
const lastOrderDate = product.last_order_date
? sprintf(
/* translators: %s = time since last product order. e.g.: "10 minutes ago" - translated. */
__( 'Last ordered %s', 'woocommerce-admin' ),
moment.utc( product.last_order_date ).fromNow()
)
: null;
// Hide cards that are not in low stock and have not been edited.
// This allows clearing cards which are no longer in low stock after
// closing & opening the panel without having to make another request.
if ( ! isLowStock && ! edited ) {
return null;
}
const title = (
<Link
href={
'post.php?action=edit&post=' +
( product.parent_id || product.id )
}
onClick={ () => this.recordStockEvent( 'product_name' ) }
type="wp-admin"
>
{ product.name }
</Link>
);
let subtitle = null;
if ( product.type === 'variation' ) {
subtitle = Object.values( product.attributes )
.map( ( attr ) => attr.option )
.join( ', ' );
}
const productImage =
get( product, [ 'images', 0 ] ) || get( product, [ 'image' ] );
const productImageClasses = classnames(
'woocommerce-stock-activity-card__image-overlay__product',
{
'is-placeholder': ! productImage || ! productImage.src,
}
);
const icon = (
<div className="woocommerce-stock-activity-card__image-overlay">
<div className={ productImageClasses }>
<ProductImage product={ product } />
</div>
</div>
);
const activityCardClasses = classnames(
'woocommerce-stock-activity-card',
{
'is-dimmed': ! editing && ! isLowStock,
}
);
const activityCard = (
<ActivityCard
className={ activityCardClasses }
title={ title }
subtitle={ subtitle }
icon={ icon }
date={ lastOrderDate }
actions={ this.getActions() }
>
{ this.getBody() }
</ActivityCard>
);
if ( editing ) {
return (
<form onReset={ this.cancelEdit } onSubmit={ this.onSubmit }>
{ activityCard }
</form>
);
}
return activityCard;
}
}