@wordpress/list-reusable-blocks 3.0.16 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 3.1.0 (2022-01-27)
6
+
5
7
  ## 3.0.0 (2021-07-29)
6
8
 
7
9
  ### Breaking Change
package/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ## Gutenberg
2
2
 
3
- Copyright 2016-2021 by the contributors
3
+ Copyright 2016-2022 by the contributors
4
4
 
5
5
  **License for Contributions (on and after April 15, 2021)**
6
6
 
@@ -24,63 +24,47 @@ var _import = _interopRequireDefault(require("../../utils/import"));
24
24
  /**
25
25
  * Internal dependencies
26
26
  */
27
- class ImportForm extends _element.Component {
28
- constructor() {
29
- super(...arguments);
30
- this.state = {
31
- isLoading: false,
32
- error: null,
33
- file: null
34
- };
35
- this.isStillMounted = true;
36
- this.onChangeFile = this.onChangeFile.bind(this);
37
- this.onSubmit = this.onSubmit.bind(this);
38
- }
39
-
40
- componentWillUnmount() {
41
- this.isStillMounted = false;
42
- }
43
-
44
- onChangeFile(event) {
45
- this.setState({
46
- file: event.target.files[0],
47
- error: null
48
- });
49
- }
50
-
51
- onSubmit(event) {
27
+ function ImportForm(_ref) {
28
+ let {
29
+ instanceId,
30
+ onUpload
31
+ } = _ref;
32
+ const inputId = 'list-reusable-blocks-import-form-' + instanceId;
33
+ const formRef = (0, _element.useRef)();
34
+ const [isLoading, setIsLoading] = (0, _element.useState)(false);
35
+ const [error, setError] = (0, _element.useState)(null);
36
+ const [file, setFile] = (0, _element.useState)(null);
37
+
38
+ const onChangeFile = event => {
39
+ setFile(event.target.files[0]);
40
+ setError(null);
41
+ };
42
+
43
+ const onSubmit = event => {
52
44
  event.preventDefault();
53
- const {
54
- file
55
- } = this.state;
56
- const {
57
- onUpload
58
- } = this.props;
59
45
 
60
46
  if (!file) {
61
47
  return;
62
48
  }
63
49
 
64
- this.setState({
50
+ setIsLoading({
65
51
  isLoading: true
66
52
  });
67
53
  (0, _import.default)(file).then(reusableBlock => {
68
- if (!this.isStillMounted) {
54
+ if (!formRef) {
69
55
  return;
70
56
  }
71
57
 
72
- this.setState({
73
- isLoading: false
74
- });
58
+ setIsLoading(false);
75
59
  onUpload(reusableBlock);
76
- }).catch(error => {
77
- if (!this.isStillMounted) {
60
+ }).catch(errors => {
61
+ if (!formRef) {
78
62
  return;
79
63
  }
80
64
 
81
65
  let uiMessage;
82
66
 
83
- switch (error.message) {
67
+ switch (errors.message) {
84
68
  case 'Invalid JSON file':
85
69
  uiMessage = (0, _i18n.__)('Invalid JSON file');
86
70
  break;
@@ -93,51 +77,36 @@ class ImportForm extends _element.Component {
93
77
  uiMessage = (0, _i18n.__)('Unknown error');
94
78
  }
95
79
 
96
- this.setState({
97
- isLoading: false,
98
- error: uiMessage
99
- });
80
+ setIsLoading(false);
81
+ setError(uiMessage);
100
82
  });
101
- }
102
-
103
- onDismissError() {
104
- this.setState({
105
- error: null
106
- });
107
- }
108
-
109
- render() {
110
- const {
111
- instanceId
112
- } = this.props;
113
- const {
114
- file,
115
- isLoading,
116
- error
117
- } = this.state;
118
- const inputId = 'list-reusable-blocks-import-form-' + instanceId;
119
- return (0, _element.createElement)("form", {
120
- className: "list-reusable-blocks-import-form",
121
- onSubmit: this.onSubmit
122
- }, error && (0, _element.createElement)(_components.Notice, {
123
- status: "error",
124
- onRemove: () => this.onDismissError()
125
- }, error), (0, _element.createElement)("label", {
126
- htmlFor: inputId,
127
- className: "list-reusable-blocks-import-form__label"
128
- }, (0, _i18n.__)('File')), (0, _element.createElement)("input", {
129
- id: inputId,
130
- type: "file",
131
- onChange: this.onChangeFile
132
- }), (0, _element.createElement)(_components.Button, {
133
- type: "submit",
134
- isBusy: isLoading,
135
- disabled: !file || isLoading,
136
- variant: "secondary",
137
- className: "list-reusable-blocks-import-form__button"
138
- }, (0, _i18n._x)('Import', 'button label')));
139
- }
140
-
83
+ };
84
+
85
+ const onDismissError = () => {
86
+ setError(null);
87
+ };
88
+
89
+ return (0, _element.createElement)("form", {
90
+ className: "list-reusable-blocks-import-form",
91
+ onSubmit: onSubmit,
92
+ ref: formRef
93
+ }, error && (0, _element.createElement)(_components.Notice, {
94
+ status: "error",
95
+ onRemove: () => onDismissError()
96
+ }, error), (0, _element.createElement)("label", {
97
+ htmlFor: inputId,
98
+ className: "list-reusable-blocks-import-form__label"
99
+ }, (0, _i18n.__)('File')), (0, _element.createElement)("input", {
100
+ id: inputId,
101
+ type: "file",
102
+ onChange: onChangeFile
103
+ }), (0, _element.createElement)(_components.Button, {
104
+ type: "submit",
105
+ isBusy: isLoading,
106
+ disabled: !file || isLoading,
107
+ variant: "secondary",
108
+ className: "list-reusable-blocks-import-form__button"
109
+ }, (0, _i18n._x)('Import', 'button label')));
141
110
  }
142
111
 
143
112
  var _default = (0, _compose.withInstanceId)(ImportForm);
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/list-reusable-blocks/src/components/import-form/index.js"],"names":["ImportForm","Component","constructor","arguments","state","isLoading","error","file","isStillMounted","onChangeFile","bind","onSubmit","componentWillUnmount","event","setState","target","files","preventDefault","onUpload","props","then","reusableBlock","catch","uiMessage","message","onDismissError","render","instanceId","inputId"],"mappings":";;;;;;;;;AAGA;;AACA;;AACA;;AACA;;AAKA;;AAXA;AACA;AACA;;AAMA;AACA;AACA;AAGA,MAAMA,UAAN,SAAyBC,kBAAzB,CAAmC;AAClCC,EAAAA,WAAW,GAAG;AACb,UAAO,GAAGC,SAAV;AACA,SAAKC,KAAL,GAAa;AACZC,MAAAA,SAAS,EAAE,KADC;AAEZC,MAAAA,KAAK,EAAE,IAFK;AAGZC,MAAAA,IAAI,EAAE;AAHM,KAAb;AAMA,SAAKC,cAAL,GAAsB,IAAtB;AACA,SAAKC,YAAL,GAAoB,KAAKA,YAAL,CAAkBC,IAAlB,CAAwB,IAAxB,CAApB;A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QAAAA,KAAK,EAAEiB;AAA3B,OAAf;AACA,KA3BF;AA4BA;;AAEDE,EAAAA,cAAc,GAAG;AAChB,SAAKX,QAAL,CAAe;AAAER,MAAAA,KAAK,EAAE;AAAT,KAAf;AACA;;AAEDoB,EAAAA,MAAM,GAAG;AACR,UAAM;AAAEC,MAAAA;AAAF,QAAiB,KAAKR,KAA5B;AACA,UAAM;AAAEZ,MAAAA,IAAF;AAAQF,MAAAA,SAAR;AAAmBC,MAAAA;AAAnB,QAA6B,KAAKF,KAAxC;AACA,UAAMwB,OAAO,GAAG,sCAAsCD,UAAtD;AACA,WACC;AACC,MAAA,SAAS,EAAC,kCADX;AAEC,MAAA,QAAQ,EAAG,KAAKhB;AAFjB,OAIGL,KAAK,IACN,4BAAC,kBAAD;AACC,MAAA,MAAM,EAAC,OADR;AAEC,MAAA,QAAQ,EAAG,MAAM,KAAKmB,cAAL;AAFlB,OAIGnB,KAJH,CALF,EAYC;AACC,MAAA,OAAO,EAAGsB,OADX;AAEC,MAAA,SAAS,EAAC;AAFX,OAIG,cAAI,MAAJ,CAJH,CAZD,EAkBC;AACC,MAAA,EAAE,EAAGA,OADN;AAEC,MAAA,IAAI,EAAC,MAFN;AAGC,MAAA,QAAQ,EAAG,KAAKnB;AAHjB,MAlBD,EAuBC,4BAAC,kBAAD;AACC,MAAA,IAAI,EAAC,QADN;AAEC,MAAA,MAAM,EAAGJ,SAFV;AAGC,MAAA,QAAQ,EAAG,CAAEE,IAAF,IAAUF,SAHtB;AAIC,MAAA,OAAO,EAAC,WAJT;AAKC,MAAA,SAAS,EAAC;AALX,OAOG,cAAI,QAAJ,EAAc,cAAd,CAPH,CAvBD,CADD;AAmCA;;AAvGiC;;eA0GpB,6BAAgBL,UAAhB,C","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { Component } from '@wordpress/element';\nimport { withInstanceId } from '@wordpress/compose';\nimport { __, _x } from '@wordpress/i18n';\nimport { Button, Notice } from '@wordpress/components';\n\n/**\n * Internal dependencies\n */\nimport importReusableBlock from '../../utils/import';\n\nclass ImportForm extends Component {\n\tconstructor() {\n\t\tsuper( ...arguments );\n\t\tthis.state = {\n\t\t\tisLoading: false,\n\t\t\terror: null,\n\t\t\tfile: null,\n\t\t};\n\n\t\tthis.isStillMounted = true;\n\t\tthis.onChangeFile = this.onChangeFile.bind( this );\n\t\tthis.onSubmit = this.onSubmit.bind( this );\n\t}\n\n\tcomponentWillUnmount() {\n\t\tthis.isStillMounted = false;\n\t}\n\n\tonChangeFile( event ) {\n\t\tthis.setState( { file: event.target.files[ 0 ], error: null } );\n\t}\n\n\tonSubmit( event ) {\n\t\tevent.preventDefault();\n\t\tconst { file } = this.state;\n\t\tconst { onUpload } = this.props;\n\t\tif ( ! file ) {\n\t\t\treturn;\n\t\t}\n\t\tthis.setState( { isLoading: true } );\n\t\timportReusableBlock( file )\n\t\t\t.then( ( reusableBlock ) => {\n\t\t\t\tif ( ! this.isStillMounted ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tthis.setState( { isLoading: false } );\n\t\t\t\tonUpload( reusableBlock );\n\t\t\t} )\n\t\t\t.catch( ( error ) => {\n\t\t\t\tif ( ! this.isStillMounted ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tlet uiMessage;\n\t\t\t\tswitch ( error.message ) {\n\t\t\t\t\tcase 'Invalid JSON file':\n\t\t\t\t\t\tuiMessage = __( 'Invalid JSON file' );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'Invalid Reusable block JSON file':\n\t\t\t\t\t\tuiMessage = __( 'Invalid Reusable block JSON file' );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tuiMessage = __( 'Unknown error' );\n\t\t\t\t}\n\n\t\t\t\tthis.setState( { isLoading: false, error: uiMessage } );\n\t\t\t} );\n\t}\n\n\tonDismissError() {\n\t\tthis.setState( { error: null } );\n\t}\n\n\trender() {\n\t\tconst { instanceId } = this.props;\n\t\tconst { file, isLoading, error } = this.state;\n\t\tconst inputId = 'list-reusable-blocks-import-form-' + instanceId;\n\t\treturn (\n\t\t\t<form\n\t\t\t\tclassName=\"list-reusable-blocks-import-form\"\n\t\t\t\tonSubmit={ this.onSubmit }\n\t\t\t>\n\t\t\t\t{ error && (\n\t\t\t\t\t<Notice\n\t\t\t\t\t\tstatus=\"error\"\n\t\t\t\t\t\tonRemove={ () => this.onDismissError() }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ error }\n\t\t\t\t\t</Notice>\n\t\t\t\t) }\n\t\t\t\t<label\n\t\t\t\t\thtmlFor={ inputId }\n\t\t\t\t\tclassName=\"list-reusable-blocks-import-form__label\"\n\t\t\t\t>\n\t\t\t\t\t{ __( 'File' ) }\n\t\t\t\t</label>\n\t\t\t\t<input\n\t\t\t\t\tid={ inputId }\n\t\t\t\t\ttype=\"file\"\n\t\t\t\t\tonChange={ this.onChangeFile }\n\t\t\t\t/>\n\t\t\t\t<Button\n\t\t\t\t\ttype=\"submit\"\n\t\t\t\t\tisBusy={ isLoading }\n\t\t\t\t\tdisabled={ ! file || isLoading }\n\t\t\t\t\tvariant=\"secondary\"\n\t\t\t\t\tclassName=\"list-reusable-blocks-import-form__button\"\n\t\t\t\t>\n\t\t\t\t\t{ _x( 'Import', 'button label' ) }\n\t\t\t\t</Button>\n\t\t\t</form>\n\t\t);\n\t}\n}\n\nexport default withInstanceId( ImportForm );\n"]}
1
+ {"version":3,"sources":["@wordpress/list-reusable-blocks/src/components/import-form/index.js"],"names":["ImportForm","instanceId","onUpload","inputId","formRef","isLoading","setIsLoading","error","setError","file","setFile","onChangeFile","event","target","files","onSubmit","preventDefault","then","reusableBlock","catch","errors","uiMessage","message","onDismissError"],"mappings":";;;;;;;;;AAGA;;AACA;;AACA;;AACA;;AAKA;;AAXA;AACA;AACA;;AAMA;AACA;AACA;AAGA,SAASA,UAAT,OAAgD;AAAA,MAA3B;AAAEC,IAAAA,UAAF;AAAcC,IAAAA;AAAd,G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,KA5BF;AA6BA,GAnCD;;AAqCA,QAAME,cAAc,GAAG,MAAM;AAC5Bf,IAAAA,QAAQ,CAAE,IAAF,CAAR;AACA,GAFD;;AAIA,SACC;AACC,IAAA,SAAS,EAAC,kCADX;AAEC,IAAA,QAAQ,EAAGO,QAFZ;AAGC,IAAA,GAAG,EAAGX;AAHP,KAKGG,KAAK,IACN,4BAAC,kBAAD;AAAQ,IAAA,MAAM,EAAC,OAAf;AAAuB,IAAA,QAAQ,EAAG,MAAMgB,cAAc;AAAtD,KACGhB,KADH,CANF,EAUC;AACC,IAAA,OAAO,EAAGJ,OADX;AAEC,IAAA,SAAS,EAAC;AAFX,KAIG,cAAI,MAAJ,CAJH,CAVD,EAgBC;AAAO,IAAA,EAAE,EAAGA,OAAZ;AAAsB,IAAA,IAAI,EAAC,MAA3B;AAAkC,IAAA,QAAQ,EAAGQ;AAA7C,IAhBD,EAiBC,4BAAC,kBAAD;AACC,IAAA,IAAI,EAAC,QADN;AAEC,IAAA,MAAM,EAAGN,SAFV;AAGC,IAAA,QAAQ,EAAG,CAAEI,IAAF,IAAUJ,SAHtB;AAIC,IAAA,OAAO,EAAC,WAJT;AAKC,IAAA,SAAS,EAAC;AALX,KAOG,cAAI,QAAJ,EAAc,cAAd,CAPH,CAjBD,CADD;AA6BA;;eAEc,6BAAgBL,UAAhB,C","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useState, useRef } from '@wordpress/element';\nimport { withInstanceId } from '@wordpress/compose';\nimport { __, _x } from '@wordpress/i18n';\nimport { Button, Notice } from '@wordpress/components';\n\n/**\n * Internal dependencies\n */\nimport importReusableBlock from '../../utils/import';\n\nfunction ImportForm( { instanceId, onUpload } ) {\n\tconst inputId = 'list-reusable-blocks-import-form-' + instanceId;\n\n\tconst formRef = useRef();\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\tconst [ error, setError ] = useState( null );\n\tconst [ file, setFile ] = useState( null );\n\n\tconst onChangeFile = ( event ) => {\n\t\tsetFile( event.target.files[ 0 ] );\n\t\tsetError( null );\n\t};\n\n\tconst onSubmit = ( event ) => {\n\t\tevent.preventDefault();\n\t\tif ( ! file ) {\n\t\t\treturn;\n\t\t}\n\t\tsetIsLoading( { isLoading: true } );\n\t\timportReusableBlock( file )\n\t\t\t.then( ( reusableBlock ) => {\n\t\t\t\tif ( ! formRef ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tsetIsLoading( false );\n\t\t\t\tonUpload( reusableBlock );\n\t\t\t} )\n\t\t\t.catch( ( errors ) => {\n\t\t\t\tif ( ! formRef ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tlet uiMessage;\n\t\t\t\tswitch ( errors.message ) {\n\t\t\t\t\tcase 'Invalid JSON file':\n\t\t\t\t\t\tuiMessage = __( 'Invalid JSON file' );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'Invalid Reusable block JSON file':\n\t\t\t\t\t\tuiMessage = __( 'Invalid Reusable block JSON file' );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tuiMessage = __( 'Unknown error' );\n\t\t\t\t}\n\n\t\t\t\tsetIsLoading( false );\n\t\t\t\tsetError( uiMessage );\n\t\t\t} );\n\t};\n\n\tconst onDismissError = () => {\n\t\tsetError( null );\n\t};\n\n\treturn (\n\t\t<form\n\t\t\tclassName=\"list-reusable-blocks-import-form\"\n\t\t\tonSubmit={ onSubmit }\n\t\t\tref={ formRef }\n\t\t>\n\t\t\t{ error && (\n\t\t\t\t<Notice status=\"error\" onRemove={ () => onDismissError() }>\n\t\t\t\t\t{ error }\n\t\t\t\t</Notice>\n\t\t\t) }\n\t\t\t<label\n\t\t\t\thtmlFor={ inputId }\n\t\t\t\tclassName=\"list-reusable-blocks-import-form__label\"\n\t\t\t>\n\t\t\t\t{ __( 'File' ) }\n\t\t\t</label>\n\t\t\t<input id={ inputId } type=\"file\" onChange={ onChangeFile } />\n\t\t\t<Button\n\t\t\t\ttype=\"submit\"\n\t\t\t\tisBusy={ isLoading }\n\t\t\t\tdisabled={ ! file || isLoading }\n\t\t\t\tvariant=\"secondary\"\n\t\t\t\tclassName=\"list-reusable-blocks-import-form__button\"\n\t\t\t>\n\t\t\t\t{ _x( 'Import', 'button label' ) }\n\t\t\t</Button>\n\t\t</form>\n\t);\n}\n\nexport default withInstanceId( ImportForm );\n"]}
@@ -3,7 +3,7 @@ import { createElement } from "@wordpress/element";
3
3
  /**
4
4
  * WordPress dependencies
5
5
  */
6
- import { Component } from '@wordpress/element';
6
+ import { useState, useRef } from '@wordpress/element';
7
7
  import { withInstanceId } from '@wordpress/compose';
8
8
  import { __, _x } from '@wordpress/i18n';
9
9
  import { Button, Notice } from '@wordpress/components';
@@ -13,63 +13,47 @@ import { Button, Notice } from '@wordpress/components';
13
13
 
14
14
  import importReusableBlock from '../../utils/import';
15
15
 
16
- class ImportForm extends Component {
17
- constructor() {
18
- super(...arguments);
19
- this.state = {
20
- isLoading: false,
21
- error: null,
22
- file: null
23
- };
24
- this.isStillMounted = true;
25
- this.onChangeFile = this.onChangeFile.bind(this);
26
- this.onSubmit = this.onSubmit.bind(this);
27
- }
28
-
29
- componentWillUnmount() {
30
- this.isStillMounted = false;
31
- }
32
-
33
- onChangeFile(event) {
34
- this.setState({
35
- file: event.target.files[0],
36
- error: null
37
- });
38
- }
39
-
40
- onSubmit(event) {
16
+ function ImportForm(_ref) {
17
+ let {
18
+ instanceId,
19
+ onUpload
20
+ } = _ref;
21
+ const inputId = 'list-reusable-blocks-import-form-' + instanceId;
22
+ const formRef = useRef();
23
+ const [isLoading, setIsLoading] = useState(false);
24
+ const [error, setError] = useState(null);
25
+ const [file, setFile] = useState(null);
26
+
27
+ const onChangeFile = event => {
28
+ setFile(event.target.files[0]);
29
+ setError(null);
30
+ };
31
+
32
+ const onSubmit = event => {
41
33
  event.preventDefault();
42
- const {
43
- file
44
- } = this.state;
45
- const {
46
- onUpload
47
- } = this.props;
48
34
 
49
35
  if (!file) {
50
36
  return;
51
37
  }
52
38
 
53
- this.setState({
39
+ setIsLoading({
54
40
  isLoading: true
55
41
  });
56
42
  importReusableBlock(file).then(reusableBlock => {
57
- if (!this.isStillMounted) {
43
+ if (!formRef) {
58
44
  return;
59
45
  }
60
46
 
61
- this.setState({
62
- isLoading: false
63
- });
47
+ setIsLoading(false);
64
48
  onUpload(reusableBlock);
65
- }).catch(error => {
66
- if (!this.isStillMounted) {
49
+ }).catch(errors => {
50
+ if (!formRef) {
67
51
  return;
68
52
  }
69
53
 
70
54
  let uiMessage;
71
55
 
72
- switch (error.message) {
56
+ switch (errors.message) {
73
57
  case 'Invalid JSON file':
74
58
  uiMessage = __('Invalid JSON file');
75
59
  break;
@@ -82,51 +66,36 @@ class ImportForm extends Component {
82
66
  uiMessage = __('Unknown error');
83
67
  }
84
68
 
85
- this.setState({
86
- isLoading: false,
87
- error: uiMessage
88
- });
69
+ setIsLoading(false);
70
+ setError(uiMessage);
89
71
  });
90
- }
91
-
92
- onDismissError() {
93
- this.setState({
94
- error: null
95
- });
96
- }
97
-
98
- render() {
99
- const {
100
- instanceId
101
- } = this.props;
102
- const {
103
- file,
104
- isLoading,
105
- error
106
- } = this.state;
107
- const inputId = 'list-reusable-blocks-import-form-' + instanceId;
108
- return createElement("form", {
109
- className: "list-reusable-blocks-import-form",
110
- onSubmit: this.onSubmit
111
- }, error && createElement(Notice, {
112
- status: "error",
113
- onRemove: () => this.onDismissError()
114
- }, error), createElement("label", {
115
- htmlFor: inputId,
116
- className: "list-reusable-blocks-import-form__label"
117
- }, __('File')), createElement("input", {
118
- id: inputId,
119
- type: "file",
120
- onChange: this.onChangeFile
121
- }), createElement(Button, {
122
- type: "submit",
123
- isBusy: isLoading,
124
- disabled: !file || isLoading,
125
- variant: "secondary",
126
- className: "list-reusable-blocks-import-form__button"
127
- }, _x('Import', 'button label')));
128
- }
129
-
72
+ };
73
+
74
+ const onDismissError = () => {
75
+ setError(null);
76
+ };
77
+
78
+ return createElement("form", {
79
+ className: "list-reusable-blocks-import-form",
80
+ onSubmit: onSubmit,
81
+ ref: formRef
82
+ }, error && createElement(Notice, {
83
+ status: "error",
84
+ onRemove: () => onDismissError()
85
+ }, error), createElement("label", {
86
+ htmlFor: inputId,
87
+ className: "list-reusable-blocks-import-form__label"
88
+ }, __('File')), createElement("input", {
89
+ id: inputId,
90
+ type: "file",
91
+ onChange: onChangeFile
92
+ }), createElement(Button, {
93
+ type: "submit",
94
+ isBusy: isLoading,
95
+ disabled: !file || isLoading,
96
+ variant: "secondary",
97
+ className: "list-reusable-blocks-import-form__button"
98
+ }, _x('Import', 'button label')));
130
99
  }
131
100
 
132
101
  export default withInstanceId(ImportForm);
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/list-reusable-blocks/src/components/import-form/index.js"],"names":["Component","withInstanceId","__","_x","Button","Notice","importReusableBlock","ImportForm","constructor","arguments","state","isLoading","error","file","isStillMounted","onChangeFile","bind","onSubmit","componentWillUnmount","event","setState","target","files","preventDefault","onUpload","props","then","reusableBlock","catch","uiMessage","message","onDismissError","render","instanceId","inputId"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,SAAT,QAA0B,oBAA1B;AACA,SAASC,cAAT,QAA+B,oBAA/B;AACA,SAASC,EAAT,EAAaC,EAAb,QAAuB,iBAAvB;AACA,SAASC,MAAT,EAAiBC,MAAjB,QAA+B,uBAA/B;AAEA;AACA;AACA;;AACA,OAAOC,mBAAP,MAAgC,oBAAhC;;AAEA,MAAMC,UAAN,SAAyBP,SAAzB,CAAmC;AAClCQ,EAAAA,WAAW,GAAG;AACb,UAAO,GAAGC,SAAV;AACA,SAAKC,KAAL,GAAa;AACZC,MAAAA,SAAS,EAAE,KADC;AAEZC,MAAAA,KAAK,EAAE,IAFK;AAGZC,MAAAA,IAAI,EAAE;AAHM,KAAb;AAMA,SAAKC,cAAL,GAAsB,IAAtB;AACA,SAAKC,YAAL,GAAoB,KAAKA,YAAL,CAAkBC,IAAlB,CAAwB,IAAxB,CA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L,CAAe;AAAET,QAAAA,SAAS,EAAE,KAAb;AAAoBC,QAAAA,KAAK,EAAEiB;AAA3B,OAAf;AACA,KA3BF;AA4BA;;AAEDE,EAAAA,cAAc,GAAG;AAChB,SAAKX,QAAL,CAAe;AAAER,MAAAA,KAAK,EAAE;AAAT,KAAf;AACA;;AAEDoB,EAAAA,MAAM,GAAG;AACR,UAAM;AAAEC,MAAAA;AAAF,QAAiB,KAAKR,KAA5B;AACA,UAAM;AAAEZ,MAAAA,IAAF;AAAQF,MAAAA,SAAR;AAAmBC,MAAAA;AAAnB,QAA6B,KAAKF,KAAxC;AACA,UAAMwB,OAAO,GAAG,sCAAsCD,UAAtD;AACA,WACC;AACC,MAAA,SAAS,EAAC,kCADX;AAEC,MAAA,QAAQ,EAAG,KAAKhB;AAFjB,OAIGL,KAAK,IACN,cAAC,MAAD;AACC,MAAA,MAAM,EAAC,OADR;AAEC,MAAA,QAAQ,EAAG,MAAM,KAAKmB,cAAL;AAFlB,OAIGnB,KAJH,CALF,EAYC;AACC,MAAA,OAAO,EAAGsB,OADX;AAEC,MAAA,SAAS,EAAC;AAFX,OAIGhC,EAAE,CAAE,MAAF,CAJL,CAZD,EAkBC;AACC,MAAA,EAAE,EAAGgC,OADN;AAEC,MAAA,IAAI,EAAC,MAFN;AAGC,MAAA,QAAQ,EAAG,KAAKnB;AAHjB,MAlBD,EAuBC,cAAC,MAAD;AACC,MAAA,IAAI,EAAC,QADN;AAEC,MAAA,MAAM,EAAGJ,SAFV;AAGC,MAAA,QAAQ,EAAG,CAAEE,IAAF,IAAUF,SAHtB;AAIC,MAAA,OAAO,EAAC,WAJT;AAKC,MAAA,SAAS,EAAC;AALX,OAOGR,EAAE,CAAE,QAAF,EAAY,cAAZ,CAPL,CAvBD,CADD;AAmCA;;AAvGiC;;AA0GnC,eAAeF,cAAc,CAAEM,UAAF,CAA7B","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { Component } from '@wordpress/element';\nimport { withInstanceId } from '@wordpress/compose';\nimport { __, _x } from '@wordpress/i18n';\nimport { Button, Notice } from '@wordpress/components';\n\n/**\n * Internal dependencies\n */\nimport importReusableBlock from '../../utils/import';\n\nclass ImportForm extends Component {\n\tconstructor() {\n\t\tsuper( ...arguments );\n\t\tthis.state = {\n\t\t\tisLoading: false,\n\t\t\terror: null,\n\t\t\tfile: null,\n\t\t};\n\n\t\tthis.isStillMounted = true;\n\t\tthis.onChangeFile = this.onChangeFile.bind( this );\n\t\tthis.onSubmit = this.onSubmit.bind( this );\n\t}\n\n\tcomponentWillUnmount() {\n\t\tthis.isStillMounted = false;\n\t}\n\n\tonChangeFile( event ) {\n\t\tthis.setState( { file: event.target.files[ 0 ], error: null } );\n\t}\n\n\tonSubmit( event ) {\n\t\tevent.preventDefault();\n\t\tconst { file } = this.state;\n\t\tconst { onUpload } = this.props;\n\t\tif ( ! file ) {\n\t\t\treturn;\n\t\t}\n\t\tthis.setState( { isLoading: true } );\n\t\timportReusableBlock( file )\n\t\t\t.then( ( reusableBlock ) => {\n\t\t\t\tif ( ! this.isStillMounted ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tthis.setState( { isLoading: false } );\n\t\t\t\tonUpload( reusableBlock );\n\t\t\t} )\n\t\t\t.catch( ( error ) => {\n\t\t\t\tif ( ! this.isStillMounted ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tlet uiMessage;\n\t\t\t\tswitch ( error.message ) {\n\t\t\t\t\tcase 'Invalid JSON file':\n\t\t\t\t\t\tuiMessage = __( 'Invalid JSON file' );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'Invalid Reusable block JSON file':\n\t\t\t\t\t\tuiMessage = __( 'Invalid Reusable block JSON file' );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tuiMessage = __( 'Unknown error' );\n\t\t\t\t}\n\n\t\t\t\tthis.setState( { isLoading: false, error: uiMessage } );\n\t\t\t} );\n\t}\n\n\tonDismissError() {\n\t\tthis.setState( { error: null } );\n\t}\n\n\trender() {\n\t\tconst { instanceId } = this.props;\n\t\tconst { file, isLoading, error } = this.state;\n\t\tconst inputId = 'list-reusable-blocks-import-form-' + instanceId;\n\t\treturn (\n\t\t\t<form\n\t\t\t\tclassName=\"list-reusable-blocks-import-form\"\n\t\t\t\tonSubmit={ this.onSubmit }\n\t\t\t>\n\t\t\t\t{ error && (\n\t\t\t\t\t<Notice\n\t\t\t\t\t\tstatus=\"error\"\n\t\t\t\t\t\tonRemove={ () => this.onDismissError() }\n\t\t\t\t\t>\n\t\t\t\t\t\t{ error }\n\t\t\t\t\t</Notice>\n\t\t\t\t) }\n\t\t\t\t<label\n\t\t\t\t\thtmlFor={ inputId }\n\t\t\t\t\tclassName=\"list-reusable-blocks-import-form__label\"\n\t\t\t\t>\n\t\t\t\t\t{ __( 'File' ) }\n\t\t\t\t</label>\n\t\t\t\t<input\n\t\t\t\t\tid={ inputId }\n\t\t\t\t\ttype=\"file\"\n\t\t\t\t\tonChange={ this.onChangeFile }\n\t\t\t\t/>\n\t\t\t\t<Button\n\t\t\t\t\ttype=\"submit\"\n\t\t\t\t\tisBusy={ isLoading }\n\t\t\t\t\tdisabled={ ! file || isLoading }\n\t\t\t\t\tvariant=\"secondary\"\n\t\t\t\t\tclassName=\"list-reusable-blocks-import-form__button\"\n\t\t\t\t>\n\t\t\t\t\t{ _x( 'Import', 'button label' ) }\n\t\t\t\t</Button>\n\t\t\t</form>\n\t\t);\n\t}\n}\n\nexport default withInstanceId( ImportForm );\n"]}
1
+ {"version":3,"sources":["@wordpress/list-reusable-blocks/src/components/import-form/index.js"],"names":["useState","useRef","withInstanceId","__","_x","Button","Notice","importReusableBlock","ImportForm","instanceId","onUpload","inputId","formRef","isLoading","setIsLoading","error","setError","file","setFile","onChangeFile","event","target","files","onSubmit","preventDefault","then","reusableBlock","catch","errors","uiMessage","message","onDismissError"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,QAAT,EAAmBC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ordPress dependencies\n */\nimport { useState, useRef } from '@wordpress/element';\nimport { withInstanceId } from '@wordpress/compose';\nimport { __, _x } from '@wordpress/i18n';\nimport { Button, Notice } from '@wordpress/components';\n\n/**\n * Internal dependencies\n */\nimport importReusableBlock from '../../utils/import';\n\nfunction ImportForm( { instanceId, onUpload } ) {\n\tconst inputId = 'list-reusable-blocks-import-form-' + instanceId;\n\n\tconst formRef = useRef();\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\tconst [ error, setError ] = useState( null );\n\tconst [ file, setFile ] = useState( null );\n\n\tconst onChangeFile = ( event ) => {\n\t\tsetFile( event.target.files[ 0 ] );\n\t\tsetError( null );\n\t};\n\n\tconst onSubmit = ( event ) => {\n\t\tevent.preventDefault();\n\t\tif ( ! file ) {\n\t\t\treturn;\n\t\t}\n\t\tsetIsLoading( { isLoading: true } );\n\t\timportReusableBlock( file )\n\t\t\t.then( ( reusableBlock ) => {\n\t\t\t\tif ( ! formRef ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tsetIsLoading( false );\n\t\t\t\tonUpload( reusableBlock );\n\t\t\t} )\n\t\t\t.catch( ( errors ) => {\n\t\t\t\tif ( ! formRef ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tlet uiMessage;\n\t\t\t\tswitch ( errors.message ) {\n\t\t\t\t\tcase 'Invalid JSON file':\n\t\t\t\t\t\tuiMessage = __( 'Invalid JSON file' );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'Invalid Reusable block JSON file':\n\t\t\t\t\t\tuiMessage = __( 'Invalid Reusable block JSON file' );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tuiMessage = __( 'Unknown error' );\n\t\t\t\t}\n\n\t\t\t\tsetIsLoading( false );\n\t\t\t\tsetError( uiMessage );\n\t\t\t} );\n\t};\n\n\tconst onDismissError = () => {\n\t\tsetError( null );\n\t};\n\n\treturn (\n\t\t<form\n\t\t\tclassName=\"list-reusable-blocks-import-form\"\n\t\t\tonSubmit={ onSubmit }\n\t\t\tref={ formRef }\n\t\t>\n\t\t\t{ error && (\n\t\t\t\t<Notice status=\"error\" onRemove={ () => onDismissError() }>\n\t\t\t\t\t{ error }\n\t\t\t\t</Notice>\n\t\t\t) }\n\t\t\t<label\n\t\t\t\thtmlFor={ inputId }\n\t\t\t\tclassName=\"list-reusable-blocks-import-form__label\"\n\t\t\t>\n\t\t\t\t{ __( 'File' ) }\n\t\t\t</label>\n\t\t\t<input id={ inputId } type=\"file\" onChange={ onChangeFile } />\n\t\t\t<Button\n\t\t\t\ttype=\"submit\"\n\t\t\t\tisBusy={ isLoading }\n\t\t\t\tdisabled={ ! file || isLoading }\n\t\t\t\tvariant=\"secondary\"\n\t\t\t\tclassName=\"list-reusable-blocks-import-form__button\"\n\t\t\t>\n\t\t\t\t{ _x( 'Import', 'button label' ) }\n\t\t\t</Button>\n\t\t</form>\n\t);\n}\n\nexport default withInstanceId( ImportForm );\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/list-reusable-blocks",
3
- "version": "3.0.16",
3
+ "version": "3.1.0",
4
4
  "description": "Adding Export/Import support to the reusable blocks listing.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -26,15 +26,15 @@
26
26
  "module": "build-module/index.js",
27
27
  "dependencies": {
28
28
  "@babel/runtime": "^7.16.0",
29
- "@wordpress/api-fetch": "^5.2.6",
30
- "@wordpress/components": "^19.1.6",
31
- "@wordpress/compose": "^5.0.7",
32
- "@wordpress/element": "^4.0.4",
33
- "@wordpress/i18n": "^4.2.4",
29
+ "@wordpress/api-fetch": "^6.0.0",
30
+ "@wordpress/components": "^19.3.0",
31
+ "@wordpress/compose": "^5.1.0",
32
+ "@wordpress/element": "^4.1.0",
33
+ "@wordpress/i18n": "^4.3.0",
34
34
  "lodash": "^4.17.21"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"
38
38
  },
39
- "gitHead": "1081a28b2368fcebd9a14554fbd0e25eb33fbcb6"
39
+ "gitHead": "d95ccb9366e249133cdb1d7b25c382446b9ee502"
40
40
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { Component } from '@wordpress/element';
4
+ import { useState, useRef } from '@wordpress/element';
5
5
  import { withInstanceId } from '@wordpress/compose';
6
6
  import { __, _x } from '@wordpress/i18n';
7
7
  import { Button, Notice } from '@wordpress/components';
@@ -11,52 +11,41 @@ import { Button, Notice } from '@wordpress/components';
11
11
  */
12
12
  import importReusableBlock from '../../utils/import';
13
13
 
14
- class ImportForm extends Component {
15
- constructor() {
16
- super( ...arguments );
17
- this.state = {
18
- isLoading: false,
19
- error: null,
20
- file: null,
21
- };
14
+ function ImportForm( { instanceId, onUpload } ) {
15
+ const inputId = 'list-reusable-blocks-import-form-' + instanceId;
22
16
 
23
- this.isStillMounted = true;
24
- this.onChangeFile = this.onChangeFile.bind( this );
25
- this.onSubmit = this.onSubmit.bind( this );
26
- }
17
+ const formRef = useRef();
18
+ const [ isLoading, setIsLoading ] = useState( false );
19
+ const [ error, setError ] = useState( null );
20
+ const [ file, setFile ] = useState( null );
27
21
 
28
- componentWillUnmount() {
29
- this.isStillMounted = false;
30
- }
22
+ const onChangeFile = ( event ) => {
23
+ setFile( event.target.files[ 0 ] );
24
+ setError( null );
25
+ };
31
26
 
32
- onChangeFile( event ) {
33
- this.setState( { file: event.target.files[ 0 ], error: null } );
34
- }
35
-
36
- onSubmit( event ) {
27
+ const onSubmit = ( event ) => {
37
28
  event.preventDefault();
38
- const { file } = this.state;
39
- const { onUpload } = this.props;
40
29
  if ( ! file ) {
41
30
  return;
42
31
  }
43
- this.setState( { isLoading: true } );
32
+ setIsLoading( { isLoading: true } );
44
33
  importReusableBlock( file )
45
34
  .then( ( reusableBlock ) => {
46
- if ( ! this.isStillMounted ) {
35
+ if ( ! formRef ) {
47
36
  return;
48
37
  }
49
38
 
50
- this.setState( { isLoading: false } );
39
+ setIsLoading( false );
51
40
  onUpload( reusableBlock );
52
41
  } )
53
- .catch( ( error ) => {
54
- if ( ! this.isStillMounted ) {
42
+ .catch( ( errors ) => {
43
+ if ( ! formRef ) {
55
44
  return;
56
45
  }
57
46
 
58
47
  let uiMessage;
59
- switch ( error.message ) {
48
+ switch ( errors.message ) {
60
49
  case 'Invalid JSON file':
61
50
  uiMessage = __( 'Invalid JSON file' );
62
51
  break;
@@ -67,54 +56,44 @@ class ImportForm extends Component {
67
56
  uiMessage = __( 'Unknown error' );
68
57
  }
69
58
 
70
- this.setState( { isLoading: false, error: uiMessage } );
59
+ setIsLoading( false );
60
+ setError( uiMessage );
71
61
  } );
72
- }
62
+ };
73
63
 
74
- onDismissError() {
75
- this.setState( { error: null } );
76
- }
64
+ const onDismissError = () => {
65
+ setError( null );
66
+ };
77
67
 
78
- render() {
79
- const { instanceId } = this.props;
80
- const { file, isLoading, error } = this.state;
81
- const inputId = 'list-reusable-blocks-import-form-' + instanceId;
82
- return (
83
- <form
84
- className="list-reusable-blocks-import-form"
85
- onSubmit={ this.onSubmit }
68
+ return (
69
+ <form
70
+ className="list-reusable-blocks-import-form"
71
+ onSubmit={ onSubmit }
72
+ ref={ formRef }
73
+ >
74
+ { error && (
75
+ <Notice status="error" onRemove={ () => onDismissError() }>
76
+ { error }
77
+ </Notice>
78
+ ) }
79
+ <label
80
+ htmlFor={ inputId }
81
+ className="list-reusable-blocks-import-form__label"
82
+ >
83
+ { __( 'File' ) }
84
+ </label>
85
+ <input id={ inputId } type="file" onChange={ onChangeFile } />
86
+ <Button
87
+ type="submit"
88
+ isBusy={ isLoading }
89
+ disabled={ ! file || isLoading }
90
+ variant="secondary"
91
+ className="list-reusable-blocks-import-form__button"
86
92
  >
87
- { error && (
88
- <Notice
89
- status="error"
90
- onRemove={ () => this.onDismissError() }
91
- >
92
- { error }
93
- </Notice>
94
- ) }
95
- <label
96
- htmlFor={ inputId }
97
- className="list-reusable-blocks-import-form__label"
98
- >
99
- { __( 'File' ) }
100
- </label>
101
- <input
102
- id={ inputId }
103
- type="file"
104
- onChange={ this.onChangeFile }
105
- />
106
- <Button
107
- type="submit"
108
- isBusy={ isLoading }
109
- disabled={ ! file || isLoading }
110
- variant="secondary"
111
- className="list-reusable-blocks-import-form__button"
112
- >
113
- { _x( 'Import', 'button label' ) }
114
- </Button>
115
- </form>
116
- );
117
- }
93
+ { _x( 'Import', 'button label' ) }
94
+ </Button>
95
+ </form>
96
+ );
118
97
  }
119
98
 
120
99
  export default withInstanceId( ImportForm );