@szymonpiatek/nextwordpress 0.0.13 → 0.0.14

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.
@@ -3,10 +3,12 @@
3
3
  var react = require('react');
4
4
  var jsxRuntime = require('react/jsx-runtime');
5
5
  var useSWR2 = require('swr');
6
+ var useSWRMutation = require('swr/mutation');
6
7
 
7
8
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
9
 
9
10
  var useSWR2__default = /*#__PURE__*/_interopDefault(useSWR2);
11
+ var useSWRMutation__default = /*#__PURE__*/_interopDefault(useSWRMutation);
10
12
 
11
13
  var __defProp = Object.defineProperty;
12
14
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -45,7 +47,13 @@ var ErrorCode = {
45
47
  // Auth
46
48
  AUTH_JWT_FAILED: "AUTH_JWT_FAILED",
47
49
  AUTH_CREDENTIALS_INVALID: "AUTH_CREDENTIALS_INVALID",
48
- AUTH_TOKEN_INVALID: "AUTH_TOKEN_INVALID"
50
+ AUTH_TOKEN_INVALID: "AUTH_TOKEN_INVALID",
51
+ // Contact Form 7
52
+ CF7_MAIL_SENT: "CF7_MAIL_SENT",
53
+ CF7_MAIL_FAILED: "CF7_MAIL_FAILED",
54
+ CF7_VALIDATION_FAILED: "CF7_VALIDATION_FAILED",
55
+ CF7_SPAM: "CF7_SPAM",
56
+ CF7_ABORTED: "CF7_ABORTED"
49
57
  };
50
58
  var defaultMessages = {
51
59
  WP_REQUEST_FAILED: "WordPress API request failed",
@@ -70,7 +78,12 @@ var defaultMessages = {
70
78
  WOO_SHIPPING_ZONE_NOT_FOUND: "Shipping zone not found",
71
79
  AUTH_JWT_FAILED: "JWT authentication failed",
72
80
  AUTH_CREDENTIALS_INVALID: "Invalid credentials",
73
- AUTH_TOKEN_INVALID: "JWT token is invalid or expired"
81
+ AUTH_TOKEN_INVALID: "JWT token is invalid or expired",
82
+ CF7_MAIL_SENT: "Thank you for your message.",
83
+ CF7_MAIL_FAILED: "There was an error trying to send your message.",
84
+ CF7_VALIDATION_FAILED: "One or more fields have an error. Please check and try again.",
85
+ CF7_SPAM: "There was an error trying to send your message. Please try later.",
86
+ CF7_ABORTED: "Message submission has been aborted."
74
87
  };
75
88
  function resolveMessage(code, overrides) {
76
89
  return overrides?.[code] ?? defaultMessages[code];
@@ -1457,11 +1470,45 @@ function useWPULike(config, params, swrOptions) {
1457
1470
  return { data, error, isLoading, vote };
1458
1471
  }
1459
1472
 
1473
+ // src/integrations/restApi/contactForm7/queries.ts
1474
+ function createCF7Queries(config) {
1475
+ async function submitForm(formId, data) {
1476
+ const url = buildUrl(config, `/wp-json/contact-form-7/v1/contact-forms/${formId}/feedback`);
1477
+ const body = new FormData();
1478
+ for (const [key, value] of Object.entries(data)) {
1479
+ body.append(key, value);
1480
+ }
1481
+ const res = await fetch(url, { method: "POST", body, cache: "no-store" });
1482
+ return res.json();
1483
+ }
1484
+ return { submitForm };
1485
+ }
1486
+
1487
+ // src/hooks/contactForm7/useCF7Submit.ts
1488
+ var statusToCode = {
1489
+ mail_sent: ErrorCode.CF7_MAIL_SENT,
1490
+ mail_failed: ErrorCode.CF7_MAIL_FAILED,
1491
+ validation_failed: ErrorCode.CF7_VALIDATION_FAILED,
1492
+ spam: ErrorCode.CF7_SPAM,
1493
+ aborted: ErrorCode.CF7_ABORTED
1494
+ };
1495
+ function useCF7Submit(config) {
1496
+ return useSWRMutation__default.default(
1497
+ `cf7-submit:${config.serverURL}`,
1498
+ async (_, { arg }) => {
1499
+ const result = await createCF7Queries(config).submitForm(arg.formId, arg.fields);
1500
+ const override = config.errorMessages?.[statusToCode[result.status]];
1501
+ return override ? { ...result, message: override } : result;
1502
+ }
1503
+ );
1504
+ }
1505
+
1460
1506
  exports.AuthProvider = AuthProvider;
1461
1507
  exports.CartProvider = CartProvider;
1462
1508
  exports.WooCommerceCustomerProvider = WooCommerceCustomerProvider;
1463
1509
  exports.cartReducer = cartReducer;
1464
1510
  exports.useAuth = useAuth;
1511
+ exports.useCF7Submit = useCF7Submit;
1465
1512
  exports.useCart = useCart;
1466
1513
  exports.useCookieConsent = useCookieConsent;
1467
1514
  exports.useCustomer = useCustomer;