feat: add email notif for post like
This commit is contained in:
@@ -31,6 +31,7 @@ import { ChangePasswordModal } from "@/components/auth/change-password-modal";
|
|||||||
import { ReadingSettings } from "@/components/reading-settings";
|
import { ReadingSettings } from "@/components/reading-settings";
|
||||||
import { PushToggle } from "@/components/push/push-toggle";
|
import { PushToggle } from "@/components/push/push-toggle";
|
||||||
import { ReminderToggle } from "@/components/sadhna/reminder-toggle";
|
import { ReminderToggle } from "@/components/sadhna/reminder-toggle";
|
||||||
|
import { LikeEmailToggle } from "@/components/notifications/like-email-toggle";
|
||||||
|
|
||||||
const BFF_BASE =
|
const BFF_BASE =
|
||||||
process.env.NEXT_PUBLIC_BFF_URL || "http://localhost:8080";
|
process.env.NEXT_PUBLIC_BFF_URL || "http://localhost:8080";
|
||||||
@@ -64,6 +65,7 @@ export default function SettingsPage() {
|
|||||||
>
|
>
|
||||||
<PushToggle />
|
<PushToggle />
|
||||||
<ReminderToggle />
|
<ReminderToggle />
|
||||||
|
<LikeEmailToggle />
|
||||||
</Section>
|
</Section>
|
||||||
|
|
||||||
<Section title="Authentication">
|
<Section title="Authentication">
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LikeEmailToggle — settings control for aggregated "someone liked your post"
|
||||||
|
* emails. Reads the current preference and lets the user opt out (push +
|
||||||
|
* in-app like notifications are unaffected). Matches the other settings rows.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { notifications } from "@/lib/social";
|
||||||
|
|
||||||
|
export function LikeEmailToggle() {
|
||||||
|
const [enabled, setEnabled] = useState<boolean | null>(null);
|
||||||
|
const [busy, setBusy] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let cancelled = false;
|
||||||
|
notifications
|
||||||
|
.emailPrefs()
|
||||||
|
.then((r) => {
|
||||||
|
if (!cancelled) setEnabled(r.like_emails);
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
if (!cancelled) setEnabled(true); // default-on; toggle still works
|
||||||
|
});
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
async function toggle() {
|
||||||
|
if (enabled === null) return;
|
||||||
|
const next = !enabled;
|
||||||
|
setBusy(true);
|
||||||
|
try {
|
||||||
|
await notifications.setEmailPrefs(next);
|
||||||
|
setEnabled(next);
|
||||||
|
toast.success(next ? "Like emails turned on" : "Like emails turned off");
|
||||||
|
} catch {
|
||||||
|
toast.error("Could not update preference");
|
||||||
|
} finally {
|
||||||
|
setBusy(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ul className="divide-y divide-border">
|
||||||
|
<li className="flex items-center justify-between gap-4 py-3">
|
||||||
|
<div className="min-w-0">
|
||||||
|
<span className="text-sm text-foreground">Like emails</span>
|
||||||
|
<p className="text-xs text-muted-foreground mt-0.5">
|
||||||
|
Get an email when someone likes your post (bundled — one email per
|
||||||
|
post). Push and in-app notifications stay on either way.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="shrink-0">
|
||||||
|
{enabled === null ? (
|
||||||
|
<span className="text-xs text-muted-foreground">…</span>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={toggle}
|
||||||
|
disabled={busy}
|
||||||
|
aria-pressed={enabled}
|
||||||
|
className={[
|
||||||
|
"relative inline-flex h-6 w-11 items-center rounded-full transition-colors disabled:opacity-60",
|
||||||
|
enabled ? "bg-primary" : "bg-muted-foreground/30",
|
||||||
|
].join(" ")}
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
className={[
|
||||||
|
"inline-block size-5 transform rounded-full bg-white shadow transition-transform",
|
||||||
|
enabled ? "translate-x-5" : "translate-x-0.5",
|
||||||
|
].join(" ")}
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -468,6 +468,16 @@ export const notifications = {
|
|||||||
markAllRead() {
|
markAllRead() {
|
||||||
return call<void>("/notifications/read", { method: "POST" });
|
return call<void>("/notifications/read", { method: "POST" });
|
||||||
},
|
},
|
||||||
|
/** Email-notification preferences (currently aggregated like emails). */
|
||||||
|
emailPrefs() {
|
||||||
|
return call<{ like_emails: boolean }>("/notifications/email-prefs");
|
||||||
|
},
|
||||||
|
setEmailPrefs(likeEmails: boolean) {
|
||||||
|
return call<{ like_emails: boolean }>("/notifications/email-prefs", {
|
||||||
|
method: "PUT",
|
||||||
|
body: JSON.stringify({ like_emails: likeEmails }),
|
||||||
|
});
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const relationships = {
|
export const relationships = {
|
||||||
|
|||||||
Reference in New Issue
Block a user