Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 0696a64

Browse files
committed
migrate to typescript
1 parent 2b79d3e commit 0696a64

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

assets/js/blocks/products/all-products/edit.js renamed to assets/js/blocks/products/all-products/edit.tsx

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* External dependencies
33
*/
44
import { __ } from '@wordpress/i18n';
5-
import { createBlock } from '@wordpress/blocks';
5+
import { createBlock, BlockInstance } from '@wordpress/blocks';
66
import {
77
BlockControls,
88
InnerBlocks,
@@ -20,7 +20,6 @@ import {
2020
} from '@wordpress/components';
2121
import { Component } from '@wordpress/element';
2222
import { compose } from '@wordpress/compose';
23-
import PropTypes from 'prop-types';
2423
import { Icon, grid } from '@wordpress/icons';
2524
import GridLayoutControl from '@woocommerce/editor-components/grid-layout-control';
2625
import {
@@ -48,46 +47,58 @@ import { getSharedContentControls, getSharedListControls } from '../edit';
4847
import Block from './block';
4948
import './editor.scss';
5049

50+
type EditorAttributes = {
51+
columns: number;
52+
rows: number;
53+
alignButtons: boolean;
54+
contentVisibility: object;
55+
orderby: string;
56+
layoutConfig: Array< number >;
57+
isPreview: boolean;
58+
};
59+
60+
interface EditorProps {
61+
block: BlockInstance;
62+
attributes: EditorAttributes;
63+
debouncedSpeak: ( label: string ) => void;
64+
setAttributes: ( attributes: Record< string, unknown > ) => void;
65+
replaceInnerBlocks: (
66+
rootClientId: string,
67+
blocks: BlockInstance[],
68+
updateSelection?: boolean
69+
) => void;
70+
}
71+
72+
interface EditorState {
73+
isEditing: boolean;
74+
innerBlocks: BlockInstance[];
75+
}
76+
5177
/**
5278
* Component to handle edit mode of "All Products".
5379
*/
54-
class Editor extends Component {
55-
static propTypes = {
56-
/**
57-
* The attributes for this block.
58-
*/
59-
attributes: PropTypes.object.isRequired,
60-
/**
61-
* A callback to update attributes.
62-
*/
63-
setAttributes: PropTypes.func.isRequired,
64-
/**
65-
* From withSpokenMessages.
66-
*/
67-
debouncedSpeak: PropTypes.func.isRequired,
68-
};
69-
80+
class Editor extends Component< EditorProps, EditorState > {
7081
state = {
7182
isEditing: false,
7283
innerBlocks: [],
7384
};
7485

7586
blockMap = getBlockMap( 'woocommerce/all-products' );
7687

77-
componentDidMount = () => {
88+
componentDidMount = (): void => {
7889
const { block } = this.props;
7990
this.setState( { innerBlocks: block.innerBlocks } );
8091
};
8192

82-
getTitle = () => {
93+
getTitle = (): string => {
8394
return __( 'All Products', 'woo-gutenberg-products-block' );
8495
};
8596

86-
getIcon = () => {
97+
getIcon = (): JSX.Element => {
8798
return <Icon icon={ grid } />;
8899
};
89100

90-
togglePreview = () => {
101+
togglePreview = (): void => {
91102
const { debouncedSpeak } = this.props;
92103

93104
this.setState( { isEditing: ! this.state.isEditing } );
@@ -102,7 +113,7 @@ class Editor extends Component {
102113
}
103114
};
104115

105-
getInspectorControls = () => {
116+
getInspectorControls = (): JSX.Element => {
106117
const { attributes, setAttributes } = this.props;
107118
const { columns, rows, alignButtons } = attributes;
108119

@@ -139,7 +150,7 @@ class Editor extends Component {
139150
);
140151
};
141152

142-
getBlockControls = () => {
153+
getBlockControls = (): JSX.Element => {
143154
const { isEditing } = this.state;
144155

145156
return (
@@ -180,7 +191,7 @@ class Editor extends Component {
180191

181192
const onReset = () => {
182193
const { block, replaceInnerBlocks } = this.props;
183-
const newBlocks = [];
194+
const newBlocks: BlockInstance[] = [];
184195
DEFAULT_PRODUCT_LIST_LAYOUT.map( ( [ name, attributes ] ) => {
185196
newBlocks.push( createBlock( name, attributes ) );
186197
return true;
@@ -193,6 +204,7 @@ class Editor extends Component {
193204
template: this.props.attributes.layoutConfig,
194205
templateLock: false,
195206
allowedBlocks: Object.keys( this.blockMap ),
207+
renderAppender: true,
196208
};
197209

198210
if ( this.props.attributes.layoutConfig.length !== 0 ) {
@@ -222,6 +234,7 @@ class Editor extends Component {
222234
<ProductDataContextProvider
223235
product={ previewProducts[ 0 ] }
224236
>
237+
{ /* @ts-expect-error: `InnerBlocks` is a component that is typed in WordPress core*/ }
225238
<InnerBlocks { ...InnerBlockProps } />
226239
</ProductDataContextProvider>
227240
</li>
@@ -276,6 +289,7 @@ class Editor extends Component {
276289

277290
return (
278291
<Disabled>
292+
{ /* @ts-expect-error: `Block` is a component that is typed in WordPress core*/ }
279293
<Block attributes={ attributes } />
280294
</Disabled>
281295
);

assets/js/previews/products.js renamed to assets/js/previews/products.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
*/
44
import { __ } from '@wordpress/i18n';
55
import { WC_BLOCKS_IMAGE_URL } from '@woocommerce/block-settings';
6+
import { ProductResponseItem } from '@woocommerce/types';
67

78
const shortDescription = __(
89
'Fly your WordPress banner with this beauty! Deck out your office space or add it to your kids walls. This banner will spruce up any space it’s hung!',
910
'woo-gutenberg-products-block'
1011
);
1112

12-
export const previewProducts = [
13+
export const previewProducts: Array< ProductResponseItem > = [
1314
{
1415
id: 1,
1516
name: 'WordPress Pennant',
@@ -33,7 +34,7 @@ export const previewProducts = [
3334
sizes: '',
3435
},
3536
],
36-
average_rating: 5,
37+
average_rating: '5',
3738
categories: [
3839
{
3940
id: 1,
@@ -58,6 +59,10 @@ export const previewProducts = [
5859
add_to_cart: {
5960
text: __( 'Add to cart', 'woo-gutenberg-products-block' ),
6061
description: __( 'Add to cart', 'woo-gutenberg-products-block' ),
62+
url: '',
63+
minimum: 1,
64+
maximum: 99,
65+
multiple_of: 1,
6166
},
6267
has_options: false,
6368
is_purchasable: true,

0 commit comments

Comments
 (0)