@wordpress/list-reusable-blocks 3.0.16-next.33ec3857e2.0 → 3.0.19

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.
@@ -24,47 +24,63 @@ var _import = _interopRequireDefault(require("../../utils/import"));
24
24
  /**
25
25
  * Internal dependencies
26
26
  */
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 => {
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) {
44
52
  event.preventDefault();
53
+ const {
54
+ file
55
+ } = this.state;
56
+ const {
57
+ onUpload
58
+ } = this.props;
45
59
 
46
60
  if (!file) {
47
61
  return;
48
62
  }
49
63
 
50
- setIsLoading({
64
+ this.setState({
51
65
  isLoading: true
52
66
  });
53
67
  (0, _import.default)(file).then(reusableBlock => {
54
- if (!formRef) {
68
+ if (!this.isStillMounted) {
55
69
  return;
56
70
  }
57
71
 
58
- setIsLoading(false);
72
+ this.setState({
73
+ isLoading: false
74
+ });
59
75
  onUpload(reusableBlock);
60
- }).catch(errors => {
61
- if (!formRef) {
76
+ }).catch(error => {
77
+ if (!this.isStillMounted) {
62
78
  return;
63
79
  }
64
80
 
65
81
  let uiMessage;
66
82
 
67
- switch (errors.message) {
83
+ switch (error.message) {
68
84
  case 'Invalid JSON file':
69
85
  uiMessage = (0, _i18n.__)('Invalid JSON file');
70
86
  break;
@@ -77,36 +93,51 @@ function ImportForm(_ref) {
77
93
  uiMessage = (0, _i18n.__)('Unknown error');
78
94
  }
79
95
 
80
- setIsLoading(false);
81
- setError(uiMessage);
96
+ this.setState({
97
+ isLoading: false,
98
+ error: uiMessage
99
+ });
82
100
  });
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')));
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
+
110
141
  }
111
142
 
112
143
  var _default = (0, _compose.withInstanceId)(ImportForm);
@@ -1 +1 @@
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,GAA2B;AAC/C,QAAMC,OAAO,GAAG,sCAAsCF,UAAtD;AAEA,QAAMG,OAAO,GAAG,sBAAhB;AACA,QAAM,CAAEC,SAAF,EAAaC,YAAb,IAA8B,uBAAU,KAAV,CAApC;AACA,QAAM,CAAEC,KAAF,EAASC,QAAT,IAAsB,uBAAU,IAAV,CAA5B;AACA,QAAM,CAAEC,IAAF,EAAQC,OAAR,IAAoB,uBAAU,IAAV,CAA1B;;AAEA,QAAMC,YAAY,GAAKC,KAAF,IAAa;AACjCF,IAAAA,OAAO,CAAEE,KAAK,CAACC,MAAN,CAAaC,KAAb,CAAoB,CAApB,CAAF,CAAP;AACAN,IAAAA,QAAQ,CAAE,IAAF,CAAR;AACA,GAHD;;AAKA,QAAMO,QAAQ,GAAKH,KAAF,IAAa;AAC7BA,IAAAA,KAAK,CAACI,cAAN;;AACA,QA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,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"]}
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,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AAF,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"]}
@@ -3,7 +3,7 @@ import { createElement } from "@wordpress/element";
3
3
  /**
4
4
  * WordPress dependencies
5
5
  */
6
- import { useState, useRef } from '@wordpress/element';
6
+ import { Component } 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,47 +13,63 @@ import { Button, Notice } from '@wordpress/components';
13
13
 
14
14
  import importReusableBlock from '../../utils/import';
15
15
 
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 => {
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) {
33
41
  event.preventDefault();
42
+ const {
43
+ file
44
+ } = this.state;
45
+ const {
46
+ onUpload
47
+ } = this.props;
34
48
 
35
49
  if (!file) {
36
50
  return;
37
51
  }
38
52
 
39
- setIsLoading({
53
+ this.setState({
40
54
  isLoading: true
41
55
  });
42
56
  importReusableBlock(file).then(reusableBlock => {
43
- if (!formRef) {
57
+ if (!this.isStillMounted) {
44
58
  return;
45
59
  }
46
60
 
47
- setIsLoading(false);
61
+ this.setState({
62
+ isLoading: false
63
+ });
48
64
  onUpload(reusableBlock);
49
- }).catch(errors => {
50
- if (!formRef) {
65
+ }).catch(error => {
66
+ if (!this.isStillMounted) {
51
67
  return;
52
68
  }
53
69
 
54
70
  let uiMessage;
55
71
 
56
- switch (errors.message) {
72
+ switch (error.message) {
57
73
  case 'Invalid JSON file':
58
74
  uiMessage = __('Invalid JSON file');
59
75
  break;
@@ -66,36 +82,51 @@ function ImportForm(_ref) {
66
82
  uiMessage = __('Unknown error');
67
83
  }
68
84
 
69
- setIsLoading(false);
70
- setError(uiMessage);
85
+ this.setState({
86
+ isLoading: false,
87
+ error: uiMessage
88
+ });
71
89
  });
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')));
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
+
99
130
  }
100
131
 
101
132
  export default withInstanceId(ImportForm);
@@ -1 +1 @@
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,MAAnB,QAAiC,oBAAjC;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,SAASC,UAAT,OAAgD;AAAA,MAA3B;AAAEC,IAAAA,UAAF;AAAcC,IAAAA;AAAd,GAA2B;AAC/C,QAAMC,OAAO,GAAG,sCAAsCF,UAAtD;AAEA,QAAMG,OAAO,GAAGX,MAAM,EAAtB;AACA,QAAM,CAAEY,SAAF,EAAaC,YAAb,IAA8Bd,QAAQ,CAAE,KAAF,CAA5C;AACA,QAAM,CAAEe,KAAF,EAASC,QAAT,IAAsBhB,QAAQ,CAAE,IAAF,CAApC;AACA,QAAM,CAAEiB,IAAF,EAAQC,OAAR,IAAoBlB,QAAQ,CAAE,IAAF,CAAlC;;AAEA,QAAMmB,YAAY,GAAKC,KAAF,IAAa;AACjCF,IAAAA,OAAO,CAAEE,KAAK,CAACC,MAAN,CAAaC,KAAb,CAAoB,CAApB,CAAF,CAAP;AACAN,IAAAA,QAAQ,CAAE,IAAF,C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AS,EAAC;AAFX,KAIGR,EAAE,CAAE,MAAF,CAJL,CAVD,EAgBC;AAAO,IAAA,EAAE,EAAGQ,OAAZ;AAAsB,IAAA,IAAI,EAAC,MAA3B;AAAkC,IAAA,QAAQ,EAAGQ;AAA7C,IAhBD,EAiBC,cAAC,MAAD;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,KAOGT,EAAE,CAAE,QAAF,EAAY,cAAZ,CAPL,CAjBD,CADD;AA6BA;;AAED,eAAeF,cAAc,CAAEM,UAAF,CAA7B","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"]}
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,CAApB;AACA,SAAKC,QAAL,GAAgB,KAAKA,QAAL,CAAcD,IAAd,CAAoB,IAApB,CAAhB;AACA;;AAEDE,EAAAA,oBAAoB,GAAG;AACtB,SAAKJ,cAAL,GAAsB,KAAtB;AACA;;AAEDC,EAAAA,YAAY,CAAEI,KAAF,EAAU;AACrB,SAAKC,QAAL,CAAe;AAAEP,MAAAA,IAAI,EAAEM,KAAK,CAACE,MAAN,CAAaC,KAAb,CAAoB,CAApB,CAAR;AAAiCV,MAAAA,KAAK,EAAE;AAAxC,KAAf;AACA;;AAEDK,EAAAA,QAAQ,CAAEE,KAAF,EAAU;AACjBA,IAAAA,KAAK,CAACI,cAAN;AACA,UAAM;AAAEV,MAAAA;AAAF,QAAW,KAAKH,KAAtB;AACA,UAAM;AAAEc,MAAAA;AAAF,QAAe,KAAKC,KAA1B;;AACA,QAAK,CAAEZ,IAAP,EAAc;AACb;AACA;;AAC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AA,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/list-reusable-blocks",
3
- "version": "3.0.16-next.33ec3857e2.0",
3
+ "version": "3.0.19",
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.7-next.33ec3857e2.0",
30
- "@wordpress/components": "^19.2.1-next.33ec3857e2.0",
31
- "@wordpress/compose": "^5.0.7-next.33ec3857e2.0",
32
- "@wordpress/element": "^4.0.6-next.33ec3857e2.0",
33
- "@wordpress/i18n": "^4.2.5-next.33ec3857e2.0",
29
+ "@wordpress/api-fetch": "^5.2.6",
30
+ "@wordpress/components": "^19.2.2",
31
+ "@wordpress/compose": "^5.0.7",
32
+ "@wordpress/element": "^4.0.4",
33
+ "@wordpress/i18n": "^4.2.4",
34
34
  "lodash": "^4.17.21"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"
38
38
  },
39
- "gitHead": "51c7917ea7fac72953702f24d6daac87d99e7617"
39
+ "gitHead": "8c2cd6a5fd8781266da858a2e7d1bdd9a3a279e4"
40
40
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { useState, useRef } from '@wordpress/element';
4
+ import { Component } 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,41 +11,52 @@ import { Button, Notice } from '@wordpress/components';
11
11
  */
12
12
  import importReusableBlock from '../../utils/import';
13
13
 
14
- function ImportForm( { instanceId, onUpload } ) {
15
- const inputId = 'list-reusable-blocks-import-form-' + instanceId;
14
+ class ImportForm extends Component {
15
+ constructor() {
16
+ super( ...arguments );
17
+ this.state = {
18
+ isLoading: false,
19
+ error: null,
20
+ file: null,
21
+ };
16
22
 
17
- const formRef = useRef();
18
- const [ isLoading, setIsLoading ] = useState( false );
19
- const [ error, setError ] = useState( null );
20
- const [ file, setFile ] = useState( null );
23
+ this.isStillMounted = true;
24
+ this.onChangeFile = this.onChangeFile.bind( this );
25
+ this.onSubmit = this.onSubmit.bind( this );
26
+ }
21
27
 
22
- const onChangeFile = ( event ) => {
23
- setFile( event.target.files[ 0 ] );
24
- setError( null );
25
- };
28
+ componentWillUnmount() {
29
+ this.isStillMounted = false;
30
+ }
26
31
 
27
- const onSubmit = ( event ) => {
32
+ onChangeFile( event ) {
33
+ this.setState( { file: event.target.files[ 0 ], error: null } );
34
+ }
35
+
36
+ onSubmit( event ) {
28
37
  event.preventDefault();
38
+ const { file } = this.state;
39
+ const { onUpload } = this.props;
29
40
  if ( ! file ) {
30
41
  return;
31
42
  }
32
- setIsLoading( { isLoading: true } );
43
+ this.setState( { isLoading: true } );
33
44
  importReusableBlock( file )
34
45
  .then( ( reusableBlock ) => {
35
- if ( ! formRef ) {
46
+ if ( ! this.isStillMounted ) {
36
47
  return;
37
48
  }
38
49
 
39
- setIsLoading( false );
50
+ this.setState( { isLoading: false } );
40
51
  onUpload( reusableBlock );
41
52
  } )
42
- .catch( ( errors ) => {
43
- if ( ! formRef ) {
53
+ .catch( ( error ) => {
54
+ if ( ! this.isStillMounted ) {
44
55
  return;
45
56
  }
46
57
 
47
58
  let uiMessage;
48
- switch ( errors.message ) {
59
+ switch ( error.message ) {
49
60
  case 'Invalid JSON file':
50
61
  uiMessage = __( 'Invalid JSON file' );
51
62
  break;
@@ -56,44 +67,54 @@ function ImportForm( { instanceId, onUpload } ) {
56
67
  uiMessage = __( 'Unknown error' );
57
68
  }
58
69
 
59
- setIsLoading( false );
60
- setError( uiMessage );
70
+ this.setState( { isLoading: false, error: uiMessage } );
61
71
  } );
62
- };
72
+ }
63
73
 
64
- const onDismissError = () => {
65
- setError( null );
66
- };
74
+ onDismissError() {
75
+ this.setState( { error: null } );
76
+ }
67
77
 
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"
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 }
92
86
  >
93
- { _x( 'Import', 'button label' ) }
94
- </Button>
95
- </form>
96
- );
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
+ }
97
118
  }
98
119
 
99
120
  export default withInstanceId( ImportForm );