@marvalt/madapter 2.3.2 → 2.3.4

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.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useRef, useEffect, useState, createContext, useMemo, useContext } from 'react';
1
+ import React, { useRef, useEffect, useState, useMemo, useCallback, createContext, useContext } from 'react';
2
2
  import { useQueryClient, useMutation, useQuery } from '@tanstack/react-query';
3
3
 
4
4
  /**
@@ -102,11 +102,14 @@ class MauticClient {
102
102
  if (submission.formName) {
103
103
  formParams.append('mauticform[formName]', submission.formName);
104
104
  }
105
+ // Merge custom headers (e.g., Turnstile token) with default headers
106
+ const headers = {
107
+ 'Content-Type': 'application/x-www-form-urlencoded',
108
+ ...(submission.headers || {}),
109
+ };
105
110
  return this.makeRequest(endpoint, {
106
111
  method: 'POST',
107
- headers: {
108
- 'Content-Type': 'application/x-www-form-urlencoded'
109
- },
112
+ headers,
110
113
  body: formParams.toString(),
111
114
  });
112
115
  }
@@ -2168,8 +2171,8 @@ const MauticForm = ({ formId, title, description, className = '', form, onSubmit
2168
2171
  const [showSuccessMessage, setShowSuccessMessage] = useState(false);
2169
2172
  const [turnstileToken, setTurnstileToken] = useState(null);
2170
2173
  const submitMutation = useMauticFormSubmission();
2171
- // Get Turnstile site key from environment
2172
- const getTurnstilesiteKey = () => {
2174
+ // Get Turnstile site key from environment - memoized to prevent re-renders
2175
+ const turnstilesiteKey = useMemo(() => {
2173
2176
  try {
2174
2177
  // @ts-ignore - import.meta is a Vite-specific global
2175
2178
  if (typeof import.meta !== 'undefined' && import.meta.env) {
@@ -2183,8 +2186,27 @@ const MauticForm = ({ formId, title, description, className = '', form, onSubmit
2183
2186
  // import.meta not available
2184
2187
  }
2185
2188
  return undefined;
2186
- };
2187
- const turnstilesiteKey = getTurnstilesiteKey();
2189
+ }, []); // Empty deps - site key doesn't change during runtime
2190
+ // Memoize Turnstile callbacks to prevent widget re-renders
2191
+ const handleTurnstileSuccess = useCallback((token) => {
2192
+ setTurnstileToken(token);
2193
+ // Clear any turnstile error
2194
+ setErrors(prev => {
2195
+ if (!prev._turnstile)
2196
+ return prev;
2197
+ const next = { ...prev };
2198
+ delete next._turnstile;
2199
+ return next;
2200
+ });
2201
+ }, []);
2202
+ const handleTurnstileError = useCallback(() => {
2203
+ setTurnstileToken(null);
2204
+ setErrors(prev => ({ ...prev, _turnstile: 'Verification failed. Please try again.' }));
2205
+ }, []);
2206
+ const handleTurnstileExpire = useCallback(() => {
2207
+ setTurnstileToken(null);
2208
+ setErrors(prev => ({ ...prev, _turnstile: 'Verification expired. Please verify again.' }));
2209
+ }, []);
2188
2210
  useEffect(() => {
2189
2211
  if (!form) {
2190
2212
  console.warn(`Form ${formId} not found`);
@@ -2357,23 +2379,7 @@ const MauticForm = ({ formId, title, description, className = '', form, onSubmit
2357
2379
  return (jsxRuntimeExports.jsxs("form", { className: `mautic-form ${className}`, onSubmit: handleSubmit, children: [(title || form.name) && (jsxRuntimeExports.jsxs("div", { className: "form-header", children: [jsxRuntimeExports.jsx("h3", { children: title || form.name }), (description || form.description) && (jsxRuntimeExports.jsx("p", { className: "form-description", children: description || form.description }))] })), jsxRuntimeExports.jsx("div", { className: "form-fields", children: form.fields
2358
2380
  // Filter out button/submit from field group; render them in actions
2359
2381
  .filter((field) => field.type !== 'button' && field.alias !== 'submit')
2360
- .map((field) => (jsxRuntimeExports.jsxs("div", { className: `form-field form-field-${field.type}`, children: [field.showLabel !== false && (jsxRuntimeExports.jsxs("label", { htmlFor: field.alias, className: "field-label", children: [field.label, field.isRequired && jsxRuntimeExports.jsx("span", { className: "required", children: "*" })] })), renderField(field), field.properties?.helpText && (jsxRuntimeExports.jsx("p", { className: "field-help", children: field.properties.helpText })), errors[field.alias] && (jsxRuntimeExports.jsx("p", { className: "field-error", children: errors[field.alias] }))] }, field.id))) }), turnstilesiteKey && (jsxRuntimeExports.jsxs("div", { className: "form-turnstile", children: [jsxRuntimeExports.jsx(TurnstileWidget, { siteKey: turnstilesiteKey, onSuccess: (token) => {
2361
- setTurnstileToken(token);
2362
- // Clear any turnstile error
2363
- if (errors._turnstile) {
2364
- setErrors(prev => {
2365
- const next = { ...prev };
2366
- delete next._turnstile;
2367
- return next;
2368
- });
2369
- }
2370
- }, onError: () => {
2371
- setTurnstileToken(null);
2372
- setErrors(prev => ({ ...prev, _turnstile: 'Verification failed. Please try again.' }));
2373
- }, onExpire: () => {
2374
- setTurnstileToken(null);
2375
- setErrors(prev => ({ ...prev, _turnstile: 'Verification expired. Please verify again.' }));
2376
- }, theme: "auto", size: "normal" }), errors._turnstile && (jsxRuntimeExports.jsx("p", { className: "field-error turnstile-error", children: errors._turnstile }))] })), jsxRuntimeExports.jsx("div", { className: "form-actions", children: jsxRuntimeExports.jsx("button", { type: "submit", disabled: isSubmitting || (!!turnstilesiteKey && !turnstileToken), className: "submit-button", children: isSubmitting ? 'Submitting...' : 'Submit' }) }), submitMutation.error && (jsxRuntimeExports.jsx("div", { className: "form-error", children: jsxRuntimeExports.jsx("p", { children: "There was an error submitting the form. Please try again." }) }))] }));
2382
+ .map((field) => (jsxRuntimeExports.jsxs("div", { className: `form-field form-field-${field.type}`, children: [field.showLabel !== false && (jsxRuntimeExports.jsxs("label", { htmlFor: field.alias, className: "field-label", children: [field.label, field.isRequired && jsxRuntimeExports.jsx("span", { className: "required", children: "*" })] })), renderField(field), field.properties?.helpText && (jsxRuntimeExports.jsx("p", { className: "field-help", children: field.properties.helpText })), errors[field.alias] && (jsxRuntimeExports.jsx("p", { className: "field-error", children: errors[field.alias] }))] }, field.id))) }), turnstilesiteKey && (jsxRuntimeExports.jsxs("div", { className: "form-turnstile", children: [jsxRuntimeExports.jsx(TurnstileWidget, { siteKey: turnstilesiteKey, onSuccess: handleTurnstileSuccess, onError: handleTurnstileError, onExpire: handleTurnstileExpire, theme: "auto", size: "normal" }), errors._turnstile && (jsxRuntimeExports.jsx("p", { className: "field-error turnstile-error", children: errors._turnstile }))] })), jsxRuntimeExports.jsx("div", { className: "form-actions", children: jsxRuntimeExports.jsx("button", { type: "submit", disabled: isSubmitting || (!!turnstilesiteKey && !turnstileToken), className: "submit-button", children: isSubmitting ? 'Submitting...' : 'Submit' }) }), submitMutation.error && (jsxRuntimeExports.jsx("div", { className: "form-error", children: jsxRuntimeExports.jsx("p", { children: "There was an error submitting the form. Please try again." }) }))] }));
2377
2383
  };
2378
2384
 
2379
2385
  const MauticTracking = ({ enabled, mauticUrl, proxyUrl, children }) => {