tee3apps-cms-sdk-react 0.0.10 → 0.0.11
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/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/PageFormComponents/BoxRenderer.tsx +1 -1
- package/src/PageFormComponents/Button.tsx +6 -2
- package/src/PageFormComponents/PageForm.tsx +32 -6
package/package.json
CHANGED
|
@@ -218,7 +218,7 @@ const BoxRenderer: React.FC<BoxRendererProps> = ({
|
|
|
218
218
|
);
|
|
219
219
|
} else if (component.name === 'TextComponent') {
|
|
220
220
|
return <TextComponent key={index} props={component.props} />;
|
|
221
|
-
} else if (component.name === 'TermsAndCondition'
|
|
221
|
+
} else if (component.name === 'TermsAndCondition' ) {
|
|
222
222
|
const code = getFieldCode(component);
|
|
223
223
|
const hasError = validationErrors[code];
|
|
224
224
|
return (
|
|
@@ -85,7 +85,11 @@ const Button: React.FC<ButtonProps> = ({
|
|
|
85
85
|
};
|
|
86
86
|
|
|
87
87
|
// Handle button click based on button type
|
|
88
|
-
const handleClick = () => {
|
|
88
|
+
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
89
|
+
// Prevent default form submission behavior
|
|
90
|
+
e.preventDefault();
|
|
91
|
+
e.stopPropagation();
|
|
92
|
+
|
|
89
93
|
const buttonType = props.buttonType || 'submit';
|
|
90
94
|
|
|
91
95
|
// Handle different button types
|
|
@@ -145,7 +149,7 @@ const Button: React.FC<ButtonProps> = ({
|
|
|
145
149
|
<div className="mb-6">
|
|
146
150
|
<button
|
|
147
151
|
onClick={handleClick}
|
|
148
|
-
type=
|
|
152
|
+
type="button"
|
|
149
153
|
disabled={props.disabled || (isSubmitting && props.buttonType === 'submit')}
|
|
150
154
|
style={{
|
|
151
155
|
backgroundColor: currentMode.bgColor || '#3498db',
|
|
@@ -209,7 +209,8 @@ const PageForm: React.FC<PageFormProps> = ({ jsonData, onSubmit, isUrl = false,
|
|
|
209
209
|
const transformedData = transformFormValuesToNames();
|
|
210
210
|
|
|
211
211
|
// Check if we should send to URL or use onSubmit callback
|
|
212
|
-
|
|
212
|
+
// If URL is provided, automatically use it for API submission
|
|
213
|
+
if (url && url.trim() !== '') {
|
|
213
214
|
// Send data to API URL
|
|
214
215
|
setIsSubmitting(true);
|
|
215
216
|
try {
|
|
@@ -223,13 +224,17 @@ const PageForm: React.FC<PageFormProps> = ({ jsonData, onSubmit, isUrl = false,
|
|
|
223
224
|
|
|
224
225
|
// For GET requests, append data as query parameters
|
|
225
226
|
// For POST requests, send data in the body
|
|
226
|
-
let requestUrl = url;
|
|
227
|
+
let requestUrl = url.trim();
|
|
227
228
|
if (httpMethod === 'GET') {
|
|
228
229
|
const queryParams = new URLSearchParams();
|
|
229
230
|
Object.keys(transformedData).forEach((key) => {
|
|
230
|
-
|
|
231
|
+
const value = transformedData[key];
|
|
232
|
+
if (value !== null && value !== undefined && value !== '') {
|
|
233
|
+
queryParams.append(key, String(value));
|
|
234
|
+
}
|
|
231
235
|
});
|
|
232
|
-
|
|
236
|
+
const queryString = queryParams.toString();
|
|
237
|
+
requestUrl = queryString ? `${requestUrl}?${queryString}` : requestUrl;
|
|
233
238
|
} else {
|
|
234
239
|
// POST, PUT, PATCH, etc.
|
|
235
240
|
requestOptions.body = JSON.stringify(transformedData);
|
|
@@ -237,11 +242,28 @@ const PageForm: React.FC<PageFormProps> = ({ jsonData, onSubmit, isUrl = false,
|
|
|
237
242
|
|
|
238
243
|
const response = await fetch(requestUrl, requestOptions);
|
|
239
244
|
|
|
245
|
+
// Read response as text first (can only read body once)
|
|
246
|
+
const responseText = await response.text().catch(() => '');
|
|
247
|
+
|
|
240
248
|
if (!response.ok) {
|
|
241
|
-
|
|
249
|
+
const errorText = responseText || response.statusText || `HTTP ${response.status}`;
|
|
250
|
+
throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);
|
|
242
251
|
}
|
|
243
252
|
|
|
244
|
-
|
|
253
|
+
// Try to parse as JSON, fallback to text if not JSON
|
|
254
|
+
let responseData;
|
|
255
|
+
const contentType = response.headers.get('content-type');
|
|
256
|
+
if (contentType && contentType.includes('application/json') && responseText) {
|
|
257
|
+
try {
|
|
258
|
+
responseData = JSON.parse(responseText);
|
|
259
|
+
} catch {
|
|
260
|
+
// If JSON parsing fails, use text as message
|
|
261
|
+
responseData = { message: responseText };
|
|
262
|
+
}
|
|
263
|
+
} else {
|
|
264
|
+
// Not JSON or empty response, use text as message
|
|
265
|
+
responseData = responseText ? { message: responseText } : { message: 'Success' };
|
|
266
|
+
}
|
|
245
267
|
|
|
246
268
|
setIsSubmitting(false);
|
|
247
269
|
showToast('Form submitted successfully!', 'success');
|
|
@@ -253,6 +275,7 @@ const PageForm: React.FC<PageFormProps> = ({ jsonData, onSubmit, isUrl = false,
|
|
|
253
275
|
} catch (error) {
|
|
254
276
|
setIsSubmitting(false);
|
|
255
277
|
const errorMessage = error instanceof Error ? error.message : 'Failed to submit form';
|
|
278
|
+
console.error('Form submission error:', error);
|
|
256
279
|
showToast(`Error submitting form: ${errorMessage}`, 'error');
|
|
257
280
|
|
|
258
281
|
// Still call onSubmit with error data if provided
|
|
@@ -264,6 +287,9 @@ const PageForm: React.FC<PageFormProps> = ({ jsonData, onSubmit, isUrl = false,
|
|
|
264
287
|
// Send transformed data back to parent component via onSubmit callback
|
|
265
288
|
if (onSubmit) {
|
|
266
289
|
onSubmit(transformedData);
|
|
290
|
+
} else {
|
|
291
|
+
// If neither URL nor onSubmit is provided, show a warning
|
|
292
|
+
showToast('No submission handler configured. Please provide either a URL or onSubmit callback.', 'warning');
|
|
267
293
|
}
|
|
268
294
|
}
|
|
269
295
|
}, [formValues, validateForm, getRequiredFields, transformFormValuesToNames, onSubmit, showToast, isUrl, method, url]);
|