@lowdefy/plugin-aws 4.0.0-alpha.8 → 4.0.0-rc.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.
@@ -0,0 +1,281 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`Render default - value[0] 1`] = `
4
+ <span
5
+ class=""
6
+ >
7
+ <div
8
+ class="ant-upload ant-upload-select ant-upload-select-text"
9
+ >
10
+ <span
11
+ class="ant-upload"
12
+ role="button"
13
+ tabindex="0"
14
+ >
15
+ <input
16
+ accept=""
17
+ id="default"
18
+ multiple=""
19
+ style="display: none;"
20
+ type="file"
21
+ />
22
+ <button
23
+ class="ant-btn emotion-0"
24
+ id="default_button"
25
+ type="button"
26
+ >
27
+ <svg
28
+ class="emotion-1"
29
+ data-testid="AiOutlineExclamationCircle"
30
+ id="default_button_icon"
31
+ />
32
+ <span
33
+ class="emotion-1"
34
+ >
35
+ Upload
36
+ </span>
37
+ </button>
38
+ </span>
39
+ </div>
40
+ <div
41
+ class="ant-upload-list ant-upload-list-text"
42
+ />
43
+ </span>
44
+ `;
45
+
46
+ exports[`Render properties.accept - value[0] 1`] = `
47
+ <span
48
+ class=""
49
+ >
50
+ <div
51
+ class="ant-upload ant-upload-select ant-upload-select-text"
52
+ >
53
+ <span
54
+ class="ant-upload"
55
+ role="button"
56
+ tabindex="0"
57
+ >
58
+ <input
59
+ accept=".png,.jpg"
60
+ id="properties.accept"
61
+ multiple=""
62
+ style="display: none;"
63
+ type="file"
64
+ />
65
+ <button
66
+ class="ant-btn emotion-0"
67
+ id="properties.accept_button"
68
+ type="button"
69
+ >
70
+ <svg
71
+ class="emotion-1"
72
+ data-testid="AiOutlineExclamationCircle"
73
+ id="properties.accept_button_icon"
74
+ />
75
+ <span
76
+ class="emotion-1"
77
+ >
78
+ Upload
79
+ </span>
80
+ </button>
81
+ </span>
82
+ </div>
83
+ <div
84
+ class="ant-upload-list ant-upload-list-text"
85
+ />
86
+ </span>
87
+ `;
88
+
89
+ exports[`Render properties.button - value[0] 1`] = `
90
+ <span
91
+ class=""
92
+ >
93
+ <div
94
+ class="ant-upload ant-upload-select ant-upload-select-text"
95
+ >
96
+ <span
97
+ class="ant-upload"
98
+ role="button"
99
+ tabindex="0"
100
+ >
101
+ <input
102
+ accept=""
103
+ id="properties.button"
104
+ multiple=""
105
+ style="display: none;"
106
+ type="file"
107
+ />
108
+ <button
109
+ class="ant-btn ant-btn-primary emotion-0"
110
+ id="properties.button_button"
111
+ type="button"
112
+ >
113
+ <svg
114
+ class="emotion-1"
115
+ data-testid="AiOutlineExclamationCircle"
116
+ id="properties.button_button_icon"
117
+ />
118
+ <span
119
+ class="emotion-1"
120
+ >
121
+ Upload
122
+ </span>
123
+ </button>
124
+ </span>
125
+ </div>
126
+ <div
127
+ class="ant-upload-list ant-upload-list-text"
128
+ />
129
+ </span>
130
+ `;
131
+
132
+ exports[`Render properties.disabled - value[0] 1`] = `
133
+ <span
134
+ class=""
135
+ >
136
+ <div
137
+ class="ant-upload ant-upload-select ant-upload-select-text ant-upload-disabled"
138
+ >
139
+ <span
140
+ class="ant-upload ant-upload-disabled"
141
+ role="button"
142
+ >
143
+ <input
144
+ accept=""
145
+ multiple=""
146
+ style="display: none;"
147
+ type="file"
148
+ />
149
+ <button
150
+ class="ant-btn emotion-0"
151
+ disabled=""
152
+ id="properties.disabled_button"
153
+ type="button"
154
+ >
155
+ <svg
156
+ class="emotion-1"
157
+ data-testid="AiOutlineExclamationCircle"
158
+ id="properties.disabled_button_icon"
159
+ />
160
+ <span
161
+ class="emotion-1"
162
+ >
163
+ Upload
164
+ </span>
165
+ </button>
166
+ </span>
167
+ </div>
168
+ <div
169
+ class="ant-upload-list ant-upload-list-text"
170
+ />
171
+ </span>
172
+ `;
173
+
174
+ exports[`Render properties.showUploadList - value[0] 1`] = `
175
+ <span
176
+ class=""
177
+ >
178
+ <div
179
+ class="ant-upload ant-upload-select ant-upload-select-text"
180
+ >
181
+ <span
182
+ class="ant-upload"
183
+ role="button"
184
+ tabindex="0"
185
+ >
186
+ <input
187
+ accept=""
188
+ id="properties.showUploadList"
189
+ multiple=""
190
+ style="display: none;"
191
+ type="file"
192
+ />
193
+ <button
194
+ class="ant-btn emotion-0"
195
+ id="properties.showUploadList_button"
196
+ type="button"
197
+ >
198
+ <svg
199
+ class="emotion-1"
200
+ data-testid="AiOutlineExclamationCircle"
201
+ id="properties.showUploadList_button_icon"
202
+ />
203
+ <span
204
+ class="emotion-1"
205
+ >
206
+ Upload
207
+ </span>
208
+ </button>
209
+ </span>
210
+ </div>
211
+ <div
212
+ class="ant-upload-list ant-upload-list-text"
213
+ />
214
+ </span>
215
+ `;
216
+
217
+ exports[`Render properties.singleFile - value[0] 1`] = `
218
+ <span
219
+ class=""
220
+ >
221
+ <div
222
+ class="ant-upload ant-upload-select ant-upload-select-text"
223
+ >
224
+ <span
225
+ class="ant-upload"
226
+ role="button"
227
+ tabindex="0"
228
+ >
229
+ <input
230
+ accept=""
231
+ id="properties.singleFile"
232
+ style="display: none;"
233
+ type="file"
234
+ />
235
+ <button
236
+ class="ant-btn emotion-0"
237
+ id="properties.singleFile_button"
238
+ type="button"
239
+ >
240
+ <svg
241
+ class="emotion-1"
242
+ data-testid="AiOutlineExclamationCircle"
243
+ id="properties.singleFile_button_icon"
244
+ />
245
+ <span
246
+ class="emotion-1"
247
+ >
248
+ Upload
249
+ </span>
250
+ </button>
251
+ </span>
252
+ </div>
253
+ <div
254
+ class="ant-upload-list ant-upload-list-text"
255
+ />
256
+ </span>
257
+ `;
258
+
259
+ exports[`Test Schema default 1`] = `true`;
260
+
261
+ exports[`Test Schema default 2`] = `null`;
262
+
263
+ exports[`Test Schema properties.accept 1`] = `true`;
264
+
265
+ exports[`Test Schema properties.accept 2`] = `null`;
266
+
267
+ exports[`Test Schema properties.button 1`] = `true`;
268
+
269
+ exports[`Test Schema properties.button 2`] = `null`;
270
+
271
+ exports[`Test Schema properties.disabled 1`] = `true`;
272
+
273
+ exports[`Test Schema properties.disabled 2`] = `null`;
274
+
275
+ exports[`Test Schema properties.showUploadList 1`] = `true`;
276
+
277
+ exports[`Test Schema properties.showUploadList 2`] = `null`;
278
+
279
+ exports[`Test Schema properties.singleFile 1`] = `true`;
280
+
281
+ exports[`Test Schema properties.singleFile 2`] = `null`;
@@ -0,0 +1,44 @@
1
+ # Copyright 2020-2022 Lowdefy, Inc
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ - id: default
16
+ type: S3UploadButton
17
+ properties:
18
+ s3PostPolicyRequestId: s3PostPolicyRequestId
19
+ - id: properties.accept
20
+ type: S3UploadButton
21
+ properties:
22
+ accept: '.png,.jpg'
23
+ s3PostPolicyRequestId: s3PostPolicyRequestId
24
+ - id: properties.button
25
+ type: S3UploadButton
26
+ properties:
27
+ button:
28
+ type: primary
29
+ s3PostPolicyRequestId: s3PostPolicyRequestId
30
+ - id: properties.disabled
31
+ type: S3UploadButton
32
+ properties:
33
+ disabled: true
34
+ s3PostPolicyRequestId: s3PostPolicyRequestId
35
+ - id: properties.showUploadList
36
+ type: S3UploadButton
37
+ properties:
38
+ s3PostPolicyRequestId: s3PostPolicyRequestId
39
+ showUploadList: true
40
+ - id: properties.singleFile
41
+ type: S3UploadButton
42
+ properties:
43
+ s3PostPolicyRequestId: s3PostPolicyRequestId
44
+ singleFile: true
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -15,4 +15,4 @@
15
15
  */
16
16
 
17
17
  @import 'antd/lib/upload/style/index.less';
18
- @import '../Button/style.less';
18
+ @import 'antd/lib/button/style/index.less';
@@ -0,0 +1,220 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import React, { useEffect, useState } from 'react';
16
+ import { blockDefaultProps } from '@lowdefy/block-utils';
17
+ import { get } from '@lowdefy/helpers';
18
+ import { message, Upload } from 'antd';
19
+ const makeFileValue = (file, s3Parameters)=>{
20
+ const { lastModified , name , percent , size , status , type , uid } = file;
21
+ const { bucket , key } = get(s3Parameters, uid, {
22
+ default: {}
23
+ });
24
+ return {
25
+ bucket,
26
+ key,
27
+ lastModified,
28
+ name,
29
+ percent,
30
+ size,
31
+ status,
32
+ type,
33
+ uid
34
+ };
35
+ };
36
+ const makeOnChangeValue = (s3Parameters, changeEvent)=>{
37
+ const { file , fileList } = changeEvent;
38
+ return {
39
+ file: makeFileValue(file, s3Parameters),
40
+ fileList: fileList.map((fl)=>makeFileValue(fl, s3Parameters))
41
+ };
42
+ };
43
+ const getCustomRequest = ({ methods , setS3Parameters , setLoading })=>async ({ file , onError , onProgress , onSuccess })=>{
44
+ try {
45
+ setLoading(true);
46
+ const { name , size , type , uid } = file;
47
+ if (size > 1024 * 1024 * 10) throw new Error('File cannot exceed 10mb.');
48
+ const s3PostPolicyResponse = await methods.triggerEvent({
49
+ name: '__getS3PostPolicy',
50
+ event: {
51
+ filename: name,
52
+ size,
53
+ type,
54
+ uid
55
+ }
56
+ });
57
+ if (s3PostPolicyResponse.success !== true) {
58
+ throw new Error('S3 post policy request error.');
59
+ }
60
+ const { url , fields } = s3PostPolicyResponse.responses.__getS3PostPolicy.response[0];
61
+ const { bucket , key } = fields;
62
+ const meta = {
63
+ bucket,
64
+ key,
65
+ filename: name,
66
+ size,
67
+ type,
68
+ uid
69
+ };
70
+ setS3Parameters((prevState)=>{
71
+ const ret = {
72
+ ...prevState
73
+ };
74
+ ret[uid] = {
75
+ bucket,
76
+ key
77
+ };
78
+ return ret;
79
+ });
80
+ // Set 20 % progress on policy is acquired else user waits to long before progress is reported
81
+ onProgress({
82
+ percent: 20
83
+ });
84
+ // Create FormData with all required fields in S3 policy
85
+ const formData = new FormData();
86
+ Object.keys(fields).forEach((field)=>{
87
+ formData.append(field, fields[field]);
88
+ });
89
+ // file needs to be the last field in the form
90
+ formData.append('file', file);
91
+ const xhr = new XMLHttpRequest();
92
+ xhr.upload.onprogress = (event)=>{
93
+ if (event.lengthComputable) {
94
+ onProgress({
95
+ percent: event.loaded / event.total * 80 + 20
96
+ });
97
+ }
98
+ };
99
+ xhr.addEventListener('error', async (event)=>{
100
+ await methods.triggerEvent({
101
+ name: 'onError',
102
+ event: {
103
+ meta,
104
+ event
105
+ }
106
+ });
107
+ onError(event);
108
+ });
109
+ xhr.addEventListener('load', async (event)=>{
110
+ await methods.triggerEvent({
111
+ name: 'onSuccess',
112
+ event: {
113
+ meta,
114
+ event
115
+ }
116
+ });
117
+ onSuccess(event);
118
+ });
119
+ xhr.addEventListener('loadend', async (event)=>{
120
+ await methods.triggerEvent({
121
+ name: 'onDone',
122
+ event: {
123
+ meta,
124
+ event
125
+ }
126
+ });
127
+ setLoading(false);
128
+ });
129
+ xhr.open('post', url);
130
+ xhr.send(formData);
131
+ } catch (error) {
132
+ console.error(error);
133
+ await methods.triggerEvent({
134
+ name: 'onError',
135
+ event: {
136
+ meta,
137
+ event
138
+ }
139
+ });
140
+ onError(error);
141
+ }
142
+ };
143
+ const S3UploadPhoto = ({ blockId , components: { Icon } , events , methods , properties , value })=>{
144
+ // Use state here because we need to set s3 bucket and key as block value
145
+ // The customRequest function does not have access to the updated block value,
146
+ // so it cannot set the value directly. customRequest sets the parameters to s3Parameters state,
147
+ // and then onChange updates the block value.
148
+ const [s3Parameters, setS3Parameters] = useState(value);
149
+ const [loading, setLoading] = useState(false);
150
+ const customRequest = getCustomRequest({
151
+ methods,
152
+ setS3Parameters,
153
+ setLoading
154
+ });
155
+ useEffect(()=>{
156
+ methods.setValue({
157
+ file: null,
158
+ fileList: []
159
+ });
160
+ methods.registerEvent({
161
+ name: '__getS3PostPolicy',
162
+ actions: [
163
+ {
164
+ id: '__getS3PostPolicy',
165
+ type: 'Request',
166
+ params: [
167
+ properties.s3PostPolicyRequestId
168
+ ]
169
+ },
170
+ ]
171
+ });
172
+ }, []);
173
+ return /*#__PURE__*/ React.createElement(Upload, {
174
+ listType: "picture-card",
175
+ className: "avatar-uploader",
176
+ accept: "image/*",
177
+ customRequest: customRequest,
178
+ maxCount: 1,
179
+ id: blockId,
180
+ multiple: !properties.singleFile,
181
+ showUploadList: properties.showUploadList,
182
+ onChange: (event)=>{
183
+ methods.setValue(makeOnChangeValue(s3Parameters, event));
184
+ methods.triggerEvent({
185
+ name: 'onChange'
186
+ });
187
+ }
188
+ }, /*#__PURE__*/ React.createElement("div", null, loading ? /*#__PURE__*/ React.createElement(Icon, {
189
+ blockId: `${blockId}_icon`,
190
+ events: events,
191
+ properties: {
192
+ name: 'AiOutlineLoading',
193
+ size: 24
194
+ }
195
+ }) : /*#__PURE__*/ React.createElement(Icon, {
196
+ blockId: `${blockId}_icon`,
197
+ events: events,
198
+ properties: {
199
+ name: 'AiOutlineCamera',
200
+ size: 24
201
+ }
202
+ }), /*#__PURE__*/ React.createElement("div", {
203
+ style: {
204
+ marginTop: 8
205
+ }
206
+ }, "Upload a photo")));
207
+ };
208
+ S3UploadPhoto.defaultProps = blockDefaultProps;
209
+ S3UploadPhoto.meta = {
210
+ valueType: 'object',
211
+ category: 'input',
212
+ icons: [
213
+ 'AiOutlineLoading',
214
+ 'AiOutlineCamera'
215
+ ],
216
+ styles: [
217
+ 'blocks/S3UploadPhoto/style.less'
218
+ ]
219
+ };
220
+ export default S3UploadPhoto;
@@ -0,0 +1,83 @@
1
+ {
2
+ "type": "object",
3
+ "properties": {
4
+ "type": "object",
5
+ "required": ["s3PostPolicyRequestId"],
6
+ "properties": {
7
+ "accept": {
8
+ "type": "string",
9
+ "description": "File types accepted by the input. See html file type input accept property at https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept."
10
+ },
11
+ "button": {
12
+ "type": "object",
13
+ "description": "Button block properties.",
14
+ "default": {
15
+ "icon": "UploadOutlined",
16
+ "title": "Upload",
17
+ "type": "default"
18
+ },
19
+ "docs": {
20
+ "displayType": "button"
21
+ }
22
+ },
23
+ "disabled": {
24
+ "type": "boolean",
25
+ "description": "Disable the file input."
26
+ },
27
+ "s3PostPolicyRequestId": {
28
+ "type": "string",
29
+ "description": "Id of a request of type AwsS3PresignedPostPolicy that defines to which S3 bucket and how the file should be uploaded.",
30
+ "docs": {
31
+ "displayType": "manual",
32
+ "block": {
33
+ "id": "block_properties_s3PostPolicyRequestId",
34
+ "layout": { "_global": "settings_input_layout" },
35
+ "type": "Label",
36
+ "required": true,
37
+ "properties": {
38
+ "title": "s3PostPolicyRequestId",
39
+ "span": 8,
40
+ "align": "right"
41
+ },
42
+ "blocks": [
43
+ {
44
+ "id": "block_properties_s3PostPolicyRequestId_text",
45
+ "type": "Markdown",
46
+ "style": {
47
+ "color": "#8c8c8c"
48
+ },
49
+ "properties": {
50
+ "content": "Id of a request of type AwsS3PresignedPostPolicy that defines to which S3 bucket and how the file should be uploaded."
51
+ }
52
+ }
53
+ ]
54
+ },
55
+ "getter": {}
56
+ }
57
+ },
58
+ "showUploadList": {
59
+ "type": "boolean",
60
+ "default": true,
61
+ "description": "Whether to show default upload list."
62
+ },
63
+ "singleFile": {
64
+ "type": "boolean",
65
+ "default": false,
66
+ "description": "Only allow a single file to be uploaded. Only one file can be selected in the prompt and the upload button is disabled after a file is uploaded."
67
+ }
68
+ }
69
+ },
70
+ "events": {
71
+ "type": "object",
72
+ "properties": {
73
+ "onChange": {
74
+ "type": "array",
75
+ "description": "Triggered when the upload state is changing."
76
+ },
77
+ "onClick": {
78
+ "type": "array",
79
+ "description": "Triggered when the upload button is clicked."
80
+ }
81
+ }
82
+ }
83
+ }
@@ -0,0 +1,17 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ @import 'antd/lib/upload/style/index.less';
package/dist/blocks.js ADDED
@@ -0,0 +1,16 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ export { default as S3UploadButton } from './blocks/S3UploadButton/S3UploadButton.js';
16
+ export { default as S3UploadPhoto } from './blocks/S3UploadPhoto/S3UploadPhoto.js';
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2021 Lowdefy, Inc
2
+ Copyright 2020-2022 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.