@pradip1995/segment-newsletter 0.2.0

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/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@pradip1995/segment-newsletter",
3
+ "version": "0.2.0",
4
+ "license": "MIT",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "sideEffects": [
9
+ "src/segment.css"
10
+ ],
11
+ "files": [
12
+ "src"
13
+ ],
14
+ "exports": {
15
+ ".": "./src/index.ts",
16
+ "./manifest": "./src/manifest.ts"
17
+ },
18
+ "peerDependencies": {
19
+ "@pradip1995/commerce-core": "^4.0.0",
20
+ "@pradip1995/plugin-sdk": "^0.2.0",
21
+ "react": ">=19",
22
+ "react-dom": ">=19",
23
+ "next": ">=15"
24
+ },
25
+ "dependencies": {
26
+ "@pradip1995/segment-primitives": "0.3.0",
27
+ "@pradip1995/segment-tokens": "0.3.2"
28
+ },
29
+ "devDependencies": {
30
+ "@pradip1995/plugin-sdk": "^0.2.0",
31
+ "@types/react": "^19",
32
+ "react": "19.0.3",
33
+ "typescript": "^5.7.2"
34
+ },
35
+ "scripts": {
36
+ "typecheck": "tsc --noEmit",
37
+ "lint": "tsc --noEmit"
38
+ }
39
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { default } from "./segment"
2
+ export { default as manifest } from "./manifest"
@@ -0,0 +1,11 @@
1
+ import type { SegmentManifest } from "@pradip1995/plugin-sdk"
2
+
3
+ const manifest: SegmentManifest = {
4
+ id: "newsletter",
5
+ type: "segment",
6
+ version: "0.1.0",
7
+ compatibleFramework: ["^1.0.0"],
8
+ dataKey: "newsletter",
9
+ }
10
+
11
+ export default manifest
@@ -0,0 +1,110 @@
1
+ .newsletter-section {
2
+ width: 100%;
3
+ padding: 2rem 0 4rem;
4
+ }
5
+
6
+ .newsletter-section__inner {
7
+ max-width: var(--container-max);
8
+ margin: 0 auto;
9
+ padding: 0 1rem;
10
+ }
11
+
12
+ .newsletter-section__card {
13
+ display: grid;
14
+ grid-template-columns: 1fr;
15
+ border-radius: 2rem;
16
+ overflow: hidden;
17
+ background: linear-gradient(to right, var(--color-surface-muted), var(--color-surface));
18
+ box-shadow: var(--shadow-card);
19
+ }
20
+
21
+ @media (min-width: 1025px) {
22
+ .newsletter-section__card {
23
+ grid-template-columns: 1fr 1fr;
24
+ border-radius: 3rem;
25
+ }
26
+ }
27
+
28
+ .newsletter-section__media {
29
+ position: relative;
30
+ min-height: 18rem;
31
+ background: var(--color-brand-accent-muted);
32
+ }
33
+
34
+ .newsletter-section__media-fallback {
35
+ width: 100%;
36
+ height: 100%;
37
+ min-height: 18rem;
38
+ background: var(--color-brand-accent-muted);
39
+ }
40
+
41
+ .newsletter-section__form-panel {
42
+ display: flex;
43
+ align-items: center;
44
+ justify-content: center;
45
+ padding: 2rem 1.5rem;
46
+ background: var(--color-surface-muted);
47
+ }
48
+
49
+ .newsletter-section__tagline {
50
+ text-align: center;
51
+ font-style: italic;
52
+ margin-bottom: 1.5rem;
53
+ color: var(--color-text-heading);
54
+ }
55
+
56
+ .newsletter-section__form {
57
+ width: 100%;
58
+ max-width: 28rem;
59
+ display: grid;
60
+ gap: 1rem;
61
+ }
62
+
63
+ .newsletter-section__field {
64
+ display: flex;
65
+ align-items: center;
66
+ gap: 0.75rem;
67
+ }
68
+
69
+ .newsletter-section__label {
70
+ width: 4rem;
71
+ font-size: 0.75rem;
72
+ text-transform: uppercase;
73
+ letter-spacing: 0.08em;
74
+ color: var(--color-text-heading);
75
+ }
76
+
77
+ .newsletter-section__input {
78
+ flex: 1;
79
+ border: none;
80
+ border-radius: 9999px;
81
+ padding: 0.75rem 1rem;
82
+ background: var(--color-brand-accent);
83
+ color: var(--color-text-inverse);
84
+ }
85
+
86
+ .newsletter-section__actions {
87
+ display: flex;
88
+ flex-direction: column;
89
+ align-items: center;
90
+ gap: 0.5rem;
91
+ padding-top: 1rem;
92
+ }
93
+
94
+ .newsletter-section__submit {
95
+ min-width: 12rem;
96
+ border-radius: 1.125rem;
97
+ padding: 0.875rem 1.5rem;
98
+ background: var(--color-brand-accent-muted);
99
+ color: var(--color-text-heading);
100
+ font-weight: 600;
101
+ text-transform: uppercase;
102
+ }
103
+
104
+ .newsletter-section__status--success {
105
+ color: #059669;
106
+ }
107
+
108
+ .newsletter-section__status--error {
109
+ color: #e11d48;
110
+ }
@@ -0,0 +1,120 @@
1
+ "use client"
2
+
3
+ import "./segment.css"
4
+ import { FormEvent, useState } from "react"
5
+ import Image from "next/image"
6
+ import { subscribeNewsletter } from "./subscribe-newsletter"
7
+
8
+ export default function Newsletter({
9
+ imageUrl,
10
+ tagline = "Join our family & receive exclusive offers!",
11
+ submitLabel = "Join us",
12
+ }: {
13
+ imageUrl?: string | null
14
+ tagline?: string
15
+ submitLabel?: string
16
+ }) {
17
+ const [name, setName] = useState("")
18
+ const [email, setEmail] = useState("")
19
+ const [submitStatus, setSubmitStatus] = useState<"idle" | "loading" | "success" | "error">("idle")
20
+ const [submitMessage, setSubmitMessage] = useState("")
21
+
22
+ const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
23
+ e.preventDefault()
24
+ if (submitStatus === "loading") return
25
+
26
+ const sanitizedEmail = email.trim()
27
+ const sanitizedName = name.trim()
28
+
29
+ if (!sanitizedEmail) {
30
+ setSubmitStatus("error")
31
+ setSubmitMessage("Please provide an email address to join the list.")
32
+ return
33
+ }
34
+
35
+ setSubmitStatus("loading")
36
+ setSubmitMessage("")
37
+
38
+ const result = await subscribeNewsletter({
39
+ email: sanitizedEmail,
40
+ name: sanitizedName || undefined,
41
+ })
42
+
43
+ if (result.success) {
44
+ setSubmitStatus("success")
45
+ setSubmitMessage("You're in! Expect curated updates in your inbox soon.")
46
+ setName("")
47
+ setEmail("")
48
+ } else {
49
+ setSubmitStatus("error")
50
+ setSubmitMessage(result.error || "Something went wrong while subscribing. Please try again.")
51
+ }
52
+ }
53
+
54
+ return (
55
+ <section className="newsletter-section" aria-label="Newsletter signup">
56
+ <div className="newsletter-section__inner">
57
+ <div className="newsletter-section__card">
58
+ <div className="newsletter-section__media">
59
+ {imageUrl ? (
60
+ <Image
61
+ src={imageUrl}
62
+ alt=""
63
+ fill
64
+ className="newsletter-section__image object-cover"
65
+ sizes="(max-width: 1024px) 100vw, 50vw"
66
+ unoptimized
67
+ />
68
+ ) : (
69
+ <div className="newsletter-section__media-fallback" aria-hidden />
70
+ )}
71
+ </div>
72
+
73
+ <div className="newsletter-section__form-panel">
74
+ <p className="newsletter-section__tagline">{tagline}</p>
75
+ <form onSubmit={handleSubmit} className="newsletter-section__form">
76
+ <label className="newsletter-section__field">
77
+ <span className="newsletter-section__label">Name</span>
78
+ <input
79
+ type="text"
80
+ value={name}
81
+ onChange={(e) => setName(e.target.value)}
82
+ className="newsletter-section__input"
83
+ required
84
+ />
85
+ </label>
86
+ <label className="newsletter-section__field">
87
+ <span className="newsletter-section__label">Email</span>
88
+ <input
89
+ type="email"
90
+ value={email}
91
+ onChange={(e) => setEmail(e.target.value)}
92
+ className="newsletter-section__input"
93
+ required
94
+ />
95
+ </label>
96
+ <div className="newsletter-section__actions">
97
+ <button
98
+ type="submit"
99
+ disabled={submitStatus === "loading"}
100
+ className="newsletter-section__submit"
101
+ >
102
+ {submitStatus === "loading" ? "Sending…" : submitLabel}
103
+ </button>
104
+ {submitStatus !== "idle" && (
105
+ <p
106
+ role="status"
107
+ aria-live="polite"
108
+ className={`newsletter-section__status newsletter-section__status--${submitStatus}`}
109
+ >
110
+ {submitMessage}
111
+ </p>
112
+ )}
113
+ </div>
114
+ </form>
115
+ </div>
116
+ </div>
117
+ </div>
118
+ </section>
119
+ )
120
+ }
@@ -0,0 +1,50 @@
1
+ import { loaderBus } from "@pradip1995/commerce-core/util/loader-bus"
2
+
3
+ export async function subscribeNewsletter({
4
+ email,
5
+ name,
6
+ source = "newsletter",
7
+ }: {
8
+ email: string
9
+ name?: string
10
+ source?: string
11
+ }): Promise<{ success: boolean; error?: string }> {
12
+ const publishableKey = process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY
13
+ const baseUrl =
14
+ process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL || process.env.MEDUSA_BACKEND_URL || "http://localhost:9000"
15
+
16
+ if (!publishableKey) {
17
+ return { success: false, error: "Configuration error: Publishable API key is missing." }
18
+ }
19
+
20
+ try {
21
+ loaderBus.begin()
22
+ const response = await fetch(`${baseUrl}/store/contact-email-subscriptions`, {
23
+ method: "POST",
24
+ headers: {
25
+ "Content-Type": "application/json",
26
+ "x-publishable-api-key": publishableKey,
27
+ },
28
+ body: JSON.stringify({
29
+ email,
30
+ status: "subscribed",
31
+ source,
32
+ ...(name ? { name } : {}),
33
+ }),
34
+ })
35
+
36
+ if (!response.ok) {
37
+ const error = await response.text().catch(() => "Unknown error")
38
+ return { success: false, error: error || `HTTP ${response.status}: Failed to subscribe` }
39
+ }
40
+
41
+ return { success: true }
42
+ } catch (error) {
43
+ return {
44
+ success: false,
45
+ error: error instanceof Error ? error.message : "An error occurred while subscribing",
46
+ }
47
+ } finally {
48
+ loaderBus.end()
49
+ }
50
+ }