medusa-contact-logic-plugin 1.1.1
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,11 @@
|
|
|
1
|
+
export interface UseContactFormProps {
|
|
2
|
+
baseUrl?: string;
|
|
3
|
+
publishableKey?: string;
|
|
4
|
+
onComplete?: (data: any) => void;
|
|
5
|
+
}
|
|
6
|
+
export declare const useContactForm: ({ baseUrl: propsBaseUrl, publishableKey: propsPublishableKey, onComplete }?: UseContactFormProps) => {
|
|
7
|
+
status: "idle" | "success" | "error" | "pending";
|
|
8
|
+
message: string;
|
|
9
|
+
handleSubmit: (e: React.FormEvent<HTMLFormElement>) => Promise<void>;
|
|
10
|
+
setStatus: import('react').Dispatch<import('react').SetStateAction<"idle" | "success" | "error" | "pending">>;
|
|
11
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface ContactSubmissionData {
|
|
2
|
+
backendUrl: string;
|
|
3
|
+
publishableKey?: string;
|
|
4
|
+
data: Record<string, any>;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Server-side compatible function to send contact request
|
|
8
|
+
*/
|
|
9
|
+
export declare function sendContactRequest({ backendUrl, publishableKey, data }: ContactSubmissionData): Promise<any>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useState as d } from "react";
|
|
2
|
+
async function g({ backendUrl: i, publishableKey: n, data: r }) {
|
|
3
|
+
const t = `${i.replace(/\/$/, "")}/store/contact-requests`, o = {
|
|
4
|
+
"Content-Type": "application/json"
|
|
5
|
+
};
|
|
6
|
+
n && (o["x-publishable-api-key"] = n);
|
|
7
|
+
const e = await fetch(t, {
|
|
8
|
+
method: "POST",
|
|
9
|
+
headers: o,
|
|
10
|
+
body: JSON.stringify(r)
|
|
11
|
+
});
|
|
12
|
+
if (!e.ok) {
|
|
13
|
+
const c = await e.json().catch(() => ({}));
|
|
14
|
+
throw new Error(c.message || "Failed to send message");
|
|
15
|
+
}
|
|
16
|
+
return await e.json();
|
|
17
|
+
}
|
|
18
|
+
const u = { BASE_URL: "/", DEV: !1, MODE: "production", PROD: !0, SSR: !1 }, f = ({
|
|
19
|
+
baseUrl: i,
|
|
20
|
+
publishableKey: n,
|
|
21
|
+
onComplete: r
|
|
22
|
+
} = {}) => {
|
|
23
|
+
const [l, t] = d("idle"), [o, e] = d(""), c = (a) => {
|
|
24
|
+
var s;
|
|
25
|
+
try {
|
|
26
|
+
return ((s = process.env) == null ? void 0 : s[a]) || (u == null ? void 0 : u[a]);
|
|
27
|
+
} catch {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
}, h = i || c("NEXT_PUBLIC_MEDUSA_BACKEND_URL"), S = n || c("NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY");
|
|
31
|
+
return {
|
|
32
|
+
status: l,
|
|
33
|
+
message: o,
|
|
34
|
+
handleSubmit: async (a) => {
|
|
35
|
+
a.preventDefault(), t("pending"), e("");
|
|
36
|
+
try {
|
|
37
|
+
const s = new FormData(a.currentTarget), b = Object.fromEntries(s);
|
|
38
|
+
await g({ backendUrl: h, publishableKey: S, data: b }), t("success"), e("Thank you! Your message has been sent."), r == null || r(b);
|
|
39
|
+
} catch (s) {
|
|
40
|
+
console.error("Submission error:", s), t("error"), e(s.message || "Something went wrong. Please try again.");
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
setStatus: t
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
export {
|
|
47
|
+
g as sendContactRequest,
|
|
48
|
+
f as useContactForm
|
|
49
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],n):(e=typeof globalThis<"u"?globalThis:e||self,n(e.MedusaUILibrary={},e.React))})(this,function(e,n){"use strict";async function l({backendUrl:f,publishableKey:c,data:a}){const r=`${f.replace(/\/$/,"")}/store/contact-requests`,i={"Content-Type":"application/json"};c&&(i["x-publishable-api-key"]=c);const t=await fetch(r,{method:"POST",headers:i,body:JSON.stringify(a)});if(!t.ok){const u=await t.json().catch(()=>({}));throw new Error(u.message||"Failed to send message")}return await t.json()}const d={BASE_URL:"/",DEV:!1,MODE:"production",PROD:!0,SSR:!1},S=({baseUrl:f,publishableKey:c,onComplete:a}={})=>{const[b,r]=n.useState("idle"),[i,t]=n.useState(""),u=o=>{var s;try{return((s=process.env)==null?void 0:s[o])||(d==null?void 0:d[o])}catch{return}},y=f||u("NEXT_PUBLIC_MEDUSA_BACKEND_URL"),g=c||u("NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY");return{status:b,message:i,handleSubmit:async o=>{o.preventDefault(),r("pending"),t("");try{const s=new FormData(o.currentTarget),h=Object.fromEntries(s);await l({backendUrl:y,publishableKey:g,data:h}),r("success"),t("Thank you! Your message has been sent."),a==null||a(h)}catch(s){console.error("Submission error:",s),r("error"),t(s.message||"Something went wrong. Please try again.")}},setStatus:r}};e.sendContactRequest=l,e.useContactForm=S,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "medusa-contact-logic-plugin",
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/ui-library.umd.cjs",
|
|
6
|
+
"module": "./dist/ui-library.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/ui-library.js",
|
|
11
|
+
"require": "./dist/ui-library.umd.cjs",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"dev": "vite",
|
|
20
|
+
"build": "tsc && vite build",
|
|
21
|
+
"preview": "vite preview"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "^18.0.0",
|
|
25
|
+
"react-dom": "^18.0.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^25.1.0",
|
|
29
|
+
"@types/react": "^18.2.0",
|
|
30
|
+
"@types/react-dom": "^18.2.0",
|
|
31
|
+
"@vitejs/plugin-react": "^5.1.2",
|
|
32
|
+
"react": "^18.2.0",
|
|
33
|
+
"react-dom": "^18.2.0",
|
|
34
|
+
"typescript": "^5.2.2",
|
|
35
|
+
"vite": "^5.1.4",
|
|
36
|
+
"vite-plugin-dts": "^3.9.1"
|
|
37
|
+
}
|
|
38
|
+
}
|