|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { toast } from "sonner"; |
| 6 | +import { addSharedItem, createCart } from "@/lib/api-client"; |
| 7 | +import { parseShare, type ParsedShare } from "@/lib/parse-share"; |
| 8 | + |
| 9 | +type CartOpt = { id: string; title: string; slug: string }; |
| 10 | +const LAST_CART_KEY = "shoplit:lastCart"; |
| 11 | + |
| 12 | +const inputCls = |
| 13 | + "w-full rounded-md border border-rule bg-cream px-3 py-2 focus:outline-none focus:ring-2 focus:ring-accent"; |
| 14 | + |
| 15 | +export function AddForm({ carts, initial }: { carts: CartOpt[]; initial: ParsedShare }) { |
| 16 | + const [cartList, setCartList] = useState<CartOpt[]>(carts); |
| 17 | + const [cartId, setCartId] = useState(carts[0]?.id ?? ""); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const last = localStorage.getItem(LAST_CART_KEY); |
| 21 | + if (last && carts.some((c) => c.id === last)) setCartId(last); |
| 22 | + }, [carts]); |
| 23 | + const [title, setTitle] = useState(initial.title); |
| 24 | + const [priceText, setPriceText] = useState(initial.priceText); |
| 25 | + const [imageUrl, setImageUrl] = useState(""); |
| 26 | + const [originalUrl, setOriginalUrl] = useState(initial.productUrl); |
| 27 | + const [note, setNote] = useState(""); |
| 28 | + const [busy, setBusy] = useState(false); |
| 29 | + const [cartBusy, setCartBusy] = useState(false); |
| 30 | + const [added, setAdded] = useState<CartOpt | null>(null); |
| 31 | + const [newCartTitle, setNewCartTitle] = useState(""); |
| 32 | + |
| 33 | + const onPaste = (raw: string) => { |
| 34 | + const p = parseShare({ text: raw, url: raw }); |
| 35 | + if (p.productUrl) setOriginalUrl(p.productUrl); |
| 36 | + if (p.title) setTitle(p.title); |
| 37 | + if (p.priceText) setPriceText(p.priceText); |
| 38 | + }; |
| 39 | + |
| 40 | + const submit = async () => { |
| 41 | + if (!cartId) return toast.error("Pick a cart first."); |
| 42 | + if (!title.trim()) return toast.error("Add a title."); |
| 43 | + if (!originalUrl.trim()) return toast.error("Add the product link."); |
| 44 | + setBusy(true); |
| 45 | + try { |
| 46 | + await addSharedItem(cartId, { title: title.trim(), priceText, imageUrl, originalUrl: originalUrl.trim(), note }); |
| 47 | + window.localStorage.setItem(LAST_CART_KEY, cartId); |
| 48 | + setAdded(cartList.find((c) => c.id === cartId) ?? null); |
| 49 | + } catch { |
| 50 | + toast.error("Couldn't add — try again."); |
| 51 | + } finally { |
| 52 | + setBusy(false); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + const addAnother = () => { |
| 57 | + setAdded(null); |
| 58 | + setTitle(""); |
| 59 | + setPriceText(""); |
| 60 | + setImageUrl(""); |
| 61 | + setOriginalUrl(""); |
| 62 | + setNote(""); |
| 63 | + }; |
| 64 | + |
| 65 | + const makeCart = async () => { |
| 66 | + if (!newCartTitle.trim()) return; |
| 67 | + if (cartBusy) return; |
| 68 | + setCartBusy(true); |
| 69 | + try { |
| 70 | + const c = await createCart(newCartTitle.trim()); |
| 71 | + const opt = { id: c.id, title: c.title, slug: c.slug }; |
| 72 | + setCartList((l) => [opt, ...l]); |
| 73 | + setCartId(opt.id); |
| 74 | + setNewCartTitle(""); |
| 75 | + } catch { |
| 76 | + toast.error("Couldn't create the cart."); |
| 77 | + } finally { |
| 78 | + setCartBusy(false); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + if (added) { |
| 83 | + return ( |
| 84 | + <div className="rounded-2xl border border-rule bg-paper p-6 text-center"> |
| 85 | + <p className="font-serif text-2xl mb-1">Added ✓</p> |
| 86 | + <p className="text-sm text-muted mb-5">Saved to “{added.title}”.</p> |
| 87 | + <div className="flex flex-col gap-3"> |
| 88 | + <button onClick={addAnother} className="rounded-full bg-ink text-cream py-3 font-medium hover:opacity-90"> |
| 89 | + Add another |
| 90 | + </button> |
| 91 | + <Link href={`/dashboard/carts/${added.id}`} className="rounded-full border border-ink py-3 font-medium hover:bg-paper"> |
| 92 | + View cart |
| 93 | + </Link> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + if (cartList.length === 0) { |
| 100 | + return ( |
| 101 | + <div className="space-y-3"> |
| 102 | + <p className="text-sm text-muted">You don't have a cart yet — create one to start adding.</p> |
| 103 | + <input value={newCartTitle} onChange={(e) => setNewCartTitle(e.target.value)} placeholder="Cart name (e.g. My picks)" className={inputCls} /> |
| 104 | + <button onClick={makeCart} disabled={cartBusy} className="w-full rounded-full bg-ink text-cream py-3 font-medium hover:opacity-90 disabled:opacity-60"> |
| 105 | + {cartBusy ? "Creating…" : "Create cart"} |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + return ( |
| 112 | + <div className="space-y-4"> |
| 113 | + {!originalUrl && ( |
| 114 | + <label className="block"> |
| 115 | + <span className="block text-sm font-medium mb-1">Paste a product link</span> |
| 116 | + <textarea |
| 117 | + rows={2} |
| 118 | + onChange={(e) => onPaste(e.target.value)} |
| 119 | + placeholder={'Paste the link or the whole “Check out this product…” text'} |
| 120 | + className={inputCls} |
| 121 | + /> |
| 122 | + </label> |
| 123 | + )} |
| 124 | + |
| 125 | + <label className="block"> |
| 126 | + <span className="block text-sm font-medium mb-1">Cart</span> |
| 127 | + <select value={cartId} onChange={(e) => setCartId(e.target.value)} className={inputCls}> |
| 128 | + {cartList.map((c) => ( |
| 129 | + <option key={c.id} value={c.id}>{c.title}</option> |
| 130 | + ))} |
| 131 | + </select> |
| 132 | + </label> |
| 133 | + |
| 134 | + <label className="block"> |
| 135 | + <span className="block text-sm font-medium mb-1">Title</span> |
| 136 | + <input value={title} onChange={(e) => setTitle(e.target.value)} className={inputCls} /> |
| 137 | + </label> |
| 138 | + |
| 139 | + <label className="block"> |
| 140 | + <span className="block text-sm font-medium mb-1">Price</span> |
| 141 | + <input value={priceText} onChange={(e) => setPriceText(e.target.value)} placeholder="₹ price" className={inputCls} /> |
| 142 | + </label> |
| 143 | + |
| 144 | + <label className="block"> |
| 145 | + <span className="block text-sm font-medium mb-1">Product link</span> |
| 146 | + <input value={originalUrl} onChange={(e) => setOriginalUrl(e.target.value)} className={inputCls} /> |
| 147 | + </label> |
| 148 | + |
| 149 | + <label className="block"> |
| 150 | + <span className="block text-sm font-medium mb-1">Image URL <span className="text-muted font-normal">(optional)</span></span> |
| 151 | + <input value={imageUrl} onChange={(e) => setImageUrl(e.target.value)} placeholder="Leave blank for a placeholder" className={inputCls} /> |
| 152 | + </label> |
| 153 | + |
| 154 | + <label className="block"> |
| 155 | + <span className="block text-sm font-medium mb-1">Note <span className="text-muted font-normal">(optional)</span></span> |
| 156 | + <input value={note} onChange={(e) => setNote(e.target.value)} className={inputCls} /> |
| 157 | + </label> |
| 158 | + |
| 159 | + <button onClick={submit} disabled={busy} className="w-full rounded-full bg-ink text-cream py-3 font-medium hover:opacity-90 disabled:opacity-60"> |
| 160 | + {busy ? "Adding…" : "+ Add to cart"} |
| 161 | + </button> |
| 162 | + </div> |
| 163 | + ); |
| 164 | +} |
0 commit comments