-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.jsx
More file actions
162 lines (139 loc) · 4.68 KB
/
request.jsx
File metadata and controls
162 lines (139 loc) · 4.68 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { __, data_pointer, getHighestUnitFromBytes, sprintf } from "./helpers.jsx";
const {_token} = window[data_pointer];
const error_message = {
'timeout' : __('The request timed out.'),
'error' : __('Request error'),
'abort' : __('Request aborted.'),
'parsererror' : __('Failed to parse the response.')
};
function calculateFormDataSize(formData) {
const {max_filesize, max_post_size} = window[data_pointer].configs || {};
if ( isNaN( max_filesize ) || isNaN( max_post_size ) ) {
return true;
}
let totalSize = 0;
for (let entry of formData.entries()) {
const [key, value] = entry;
totalSize += key.length;
if ( value instanceof File ) {
totalSize += value.size;
if ( value.size > max_filesize ) {
return sprintf( __( 'File size must be less than %s' ), getHighestUnitFromBytes( max_filesize ) );
}
} else {
// Add size of non-file fields (strings, etc.)
totalSize += new Blob([value]).size;
}
}
return totalSize > max_post_size ? sprintf( __( 'Total size must be less than %s'), getHighestUnitFromBytes(max_post_size) ) : true;
}
/**
* Ajax request wrapper
* @param {string} action Ajax request handler method name
* @param {object?} payload Post data including File object
* @param {function?} callback Response handler
* @param {function?} progressCallback File upload progress callback
*/
export function request(action, payload = {}, callback, progressCallback) {
// Append action and nonce
payload = {
...payload,
action: action.indexOf('wp_ajax_') === 0 ? action.replace('wp_ajax_', '') : ((window[data_pointer]?.app_id || '') + '_' + action),
nonce: window[data_pointer]?.nonce || null,
nonce_action: window[data_pointer]?.nonce_action || null,
browser_timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
};
// Build form data
const formData = new FormData();
formData.append('_token', _token);
// Function to flatten nested JSON into a flat object and append files to FormData.
function flattenObject(obj, formData, parentKey = '') {
// Loop through object
for (let key in obj) {
// Do not enclose for first level key
const _key = parentKey === '' ? key : `${parentKey}[${key}]`;
// Process object even if it is array
if (typeof obj[key] === 'object') {
// Process if it is array
if (Array.isArray(obj[key])) {
// Put empty array manually. Otherwise it doesn't get submitted as forEach method doesn't cover it.
// The post data should go through castRecursive method in the backend to convert this string array[] to real empty array.
if (!obj[key].length) {
formData.append(`${_key}`, '[]');
}
// Loop through array elements
obj[key].forEach((item, index) => {
// If the element is uploaded file
if (item instanceof File) {
// Put file object which is an array element
formData.append(`${_key}[${index}]`, item, item.name);
} else if (!Array.isArray(item) && typeof item !== 'object') {
// If it is singular array item
formData.append(`${_key}[${index}]`, item);
} else {
// Run recurson for singular values
flattenObject(item, formData, `${_key}[${index}]`);
}
});
} else if (obj[key] instanceof File) {
// Process file object
formData.append(`${_key}`, obj[key]);
} else {
// Process singluar elements in the object recursively
flattenObject(obj[key], formData, `${_key}`);
}
} else if( obj[key] !== undefined ) {
// Put non object data directly
formData.append(`${_key}`, obj[key]);
}
}
}
// Flatten the nested JSON and append files to FormData
flattenObject(payload, formData);
const size_err = calculateFormDataSize(formData);
if ( typeof size_err === 'string' ) {
callback({
success: false,
data: {
message: size_err
}
});
return;
}
window.jQuery.ajax({
url: window[data_pointer].permalinks.ajaxurl,
type: 'POST',
data: formData,
contentType: false,
cache: false,
processData: false,
success: function (response) {
if (typeof callback == 'function') {
callback({
...response,
success: response.success || false,
data: response.data || {}
});
}
},
error: function (xhr, status, error) {
callback({
success: false,
data: {message: error_message[status] || __('Something went wrong!')}
});
},
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener(
'progress',
function (evt) {
if (typeof progressCallback == 'function' && evt.lengthComputable) {
progressCallback(Math.round((evt.loaded / evt.total) * 100));
}
},
false
);
return xhr;
}
});
}