feat: share shopping lists via a public link
Mirrors the existing recipe public-share pattern (/r/[id], gated by a visibility flag, no auth required) — shopping lists previously only supported private per-user collaborator invites (email + viewer/editor role), with no way to hand someone a link who isn't already a member. - shoppingLists.isPublic (owner-only toggle, same access-control pattern already used for renaming/deleting the list). - New public /s/[id] route added to PUBLIC_PATHS — read-only aisle-grouped view with a signup CTA for logged-out visitors, styled after /r/[id]. - The existing collaborator-invite dialog (ShareShoppingListButton) now also has a "Public link" toggle + copy-link button at the top, so there's one Share entry point for both private and public sharing. Verified locally: toggling public via the real dialog UI persists correctly (confirmed via DB + a fresh curl PATCH to rule out a client race in my own test script), and the public link loads with zero auth for a logged-out browser context.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import { useState } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { UserPlus, X } from "lucide-react";
|
||||
import { UserPlus, X, Link2, Copy, Check } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
Dialog,
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
} from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
@@ -21,6 +22,7 @@ import {
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Separator } from "@/components/ui/separator";
|
||||
|
||||
type Role = "viewer" | "editor";
|
||||
|
||||
@@ -38,9 +40,10 @@ interface Member {
|
||||
|
||||
interface Props {
|
||||
listId: string;
|
||||
initialIsPublic: boolean;
|
||||
}
|
||||
|
||||
export function ShareShoppingListButton({ listId }: Props) {
|
||||
export function ShareShoppingListButton({ listId, initialIsPublic }: Props) {
|
||||
const t = useTranslations("shoppingLists");
|
||||
const ts = useTranslations("shareDialog");
|
||||
const tCommon = useTranslations("common");
|
||||
@@ -50,6 +53,42 @@ export function ShareShoppingListButton({ listId }: Props) {
|
||||
const [members, setMembers] = useState<Member[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [inviting, setInviting] = useState(false);
|
||||
const [isPublic, setIsPublic] = useState(initialIsPublic);
|
||||
const [savingPublic, setSavingPublic] = useState(false);
|
||||
const [copied, setCopied] = useState(false);
|
||||
|
||||
async function togglePublic(checked: boolean) {
|
||||
setSavingPublic(true);
|
||||
const previous = isPublic;
|
||||
setIsPublic(checked);
|
||||
try {
|
||||
const res = await fetch(`/api/v1/shopping-lists/${listId}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ isPublic: checked }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
setIsPublic(previous);
|
||||
toast.error(ts("publicLinkToggleFailed"));
|
||||
}
|
||||
} catch {
|
||||
setIsPublic(previous);
|
||||
toast.error(ts("publicLinkToggleFailed"));
|
||||
} finally {
|
||||
setSavingPublic(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function copyLink() {
|
||||
const url = `${window.location.origin}/s/${listId}`;
|
||||
try {
|
||||
await navigator.clipboard.writeText(url);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
} catch {
|
||||
toast.error(ts("copyLinkFailed"));
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchMembers() {
|
||||
setLoading(true);
|
||||
@@ -130,6 +169,25 @@ export function ShareShoppingListButton({ listId }: Props) {
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="flex items-center justify-between gap-3 rounded-lg border p-3">
|
||||
<div className="flex items-center gap-2 min-w-0">
|
||||
<Link2 className="h-4 w-4 text-muted-foreground shrink-0" />
|
||||
<div className="min-w-0">
|
||||
<p className="text-sm font-medium">{ts("publicLinkTitle")}</p>
|
||||
<p className="text-xs text-muted-foreground">{ts("publicLinkDescription")}</p>
|
||||
</div>
|
||||
</div>
|
||||
<Switch checked={isPublic} disabled={savingPublic} onCheckedChange={(v) => { void togglePublic(v); }} />
|
||||
</div>
|
||||
{isPublic && (
|
||||
<Button type="button" variant="outline" size="sm" className="w-full" onClick={() => void copyLink()}>
|
||||
{copied ? <Check className="h-4 w-4" /> : <Copy className="h-4 w-4" />}
|
||||
{copied ? ts("linkCopied") : ts("copyLink")}
|
||||
</Button>
|
||||
)}
|
||||
|
||||
<Separator />
|
||||
|
||||
<div className="flex gap-2 mt-2">
|
||||
<Input
|
||||
type="email"
|
||||
|
||||
Reference in New Issue
Block a user