Skip to content

Commit 494f044

Browse files
committed
[CHG] Fixed route response handling
1 parent f5761f6 commit 494f044

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/app/(default)/api/newsletter/signup/route.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export async function POST(req: Request) {
6666
) {
6767
await conn?.query("COMMIT");
6868
return NextResponse.json(
69-
{ message: "This email is already subscribed to our newsletter" },
69+
{ error: "This email is already subscribed to our newsletter" },
7070
{ status: 409 },
7171
);
7272
}
@@ -107,7 +107,11 @@ export async function POST(req: Request) {
107107
const cipherText = cryptService.encrypt(email);
108108
if (!cipherText) {
109109
await conn?.query("ROLLBACK");
110-
return NextResponse.json({ error: "Encryption failed" }, { status: 500 });
110+
console.error("Encryption failed!");
111+
return NextResponse.json(
112+
{ error: "Something wen't wrong!" },
113+
{ status: 500 },
114+
);
111115
}
112116

113117
const verificationToken = cryptService.createVerificationToken();

src/app/(default)/newsletter/unsubscribe/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default function UnsubscribeNewsletter() {
5757
});
5858
}
5959
}
60-
}, [searchParams]);
60+
}, [searchParams, statusParam]);
6161

6262
const handleEmailUnsubscribe = async () => {
6363
if (!email) {

src/components/sections/NewsletterSection.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,20 @@ export default function NewsletterSection() {
6464
});
6565

6666
if (!response.ok) {
67-
const errorData = await response.text();
68-
throw new Error(errorData || "Failed to subscribe");
67+
const errorData = await response.json();
68+
throw new Error(errorData?.error || "Failed to subscribe");
69+
}
70+
71+
const body = await response.json();
72+
if (!body.message) {
73+
console.error("Couldn't find body.message from /api/newsletter/signup");
6974
}
7075

7176
setStatus({ type: "idle", message: "" });
72-
showToast("success", "Thank you for subscribing!");
77+
showToast(
78+
"success",
79+
body.message || "Verification email sent. Please check your inbox.",
80+
);
7381
setEmail("");
7482
setExtensive(false);
7583
} catch (error) {
@@ -157,10 +165,11 @@ export default function NewsletterSection() {
157165

158166
{toast.visible && (
159167
<div
160-
className={`fixed bottom-4 right-4 z-50 flex items-center gap-2 rounded-lg p-4 shadow-lg transition-all duration-300 ${toast.type === "success"
168+
className={`fixed bottom-4 right-4 z-50 flex items-center gap-2 rounded-lg p-4 shadow-lg transition-all duration-300 ${
169+
toast.type === "success"
161170
? "bg-green text-white"
162171
: "bg-red text-white"
163-
}`}
172+
}`}
164173
>
165174
<div className="text-sm font-medium">{toast.message}</div>
166175
<button

0 commit comments

Comments
 (0)