-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
header.tsx
66 lines (58 loc) · 1.51 KB
/
header.tsx
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
/**
* External dependencies
*/
import { Button } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { createElement } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
export type HeaderProps = {
productId: number;
title: string;
};
const DEFAULT_PRODUCT_NAME = 'AUTO-DRAFT';
export function Header( { productId, title }: HeaderProps ) {
const { isProductLocked, isSaving } = useSelect(
( select ) => {
const { isSavingEntityRecord } = select( 'core' );
const { isPostSavingLocked } = select( 'core/editor' );
return {
isProductLocked: isPostSavingLocked(),
isSaving: isSavingEntityRecord(
'postType',
'product',
productId
),
};
},
[ productId ]
);
const isDisabled = isProductLocked || isSaving;
const { saveEditedEntityRecord } = useDispatch( 'core' );
function handleSave() {
saveEditedEntityRecord( 'postType', 'product', productId );
}
return (
<div
className="woocommerce-product-header"
role="region"
aria-label={ __( 'Product Editor top bar.', 'woocommerce' ) }
tabIndex={ -1 }
>
<h1 className="woocommerce-product-header__title">
{ title === DEFAULT_PRODUCT_NAME
? __( 'Add new product', 'woocommerce' )
: title }
</h1>
<div className="woocommerce-product-header__actions">
<Button
onClick={ handleSave }
variant="primary"
isBusy={ isSaving }
disabled={ isDisabled }
>
{ __( 'Save', 'woocommerce' ) }
</Button>
</div>
</div>
);
}