From 697cd4ce66cfaaea789f3168db12db781aeb1b70 Mon Sep 17 00:00:00 2001 From: zmeyer44 Date: Sun, 29 Oct 2023 11:08:12 -0400 Subject: [PATCH] adding image upload to long form --- components/LongForm/ToolBar.tsx | 31 +++++++++++++++++++++---------- components/LongForm/Viewer.tsx | 1 - containers/Article/Editor.tsx | 24 +++++++++++++++++++++++- containers/Article/index.tsx | 4 ++-- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/components/LongForm/ToolBar.tsx b/components/LongForm/ToolBar.tsx index aca9ffb..8789ce2 100644 --- a/components/LongForm/ToolBar.tsx +++ b/components/LongForm/ToolBar.tsx @@ -8,6 +8,7 @@ import { Textarea } from "../ui/textarea"; import useAutosizeTextArea from "@/lib/hooks/useAutoSizeTextArea"; import { HiOutlinePhoto } from "react-icons/hi2"; import { cn } from "@/lib/utils"; +import useImageUpload from "@/lib/hooks/useImageUpload"; interface ToolbarProps { initialData?: { @@ -40,6 +41,8 @@ export const Toolbar = ({ initialData, preview, onSubmit }: ToolbarProps) => { const [isEditingSummary, setIsEditingSummary] = useState(false); const [value, setValue] = useState(initialData?.title); + const { ImageUploadButton, imageUrl, ImagePreview, status } = + useImageUpload("event"); const update = () => { console.log("Update"); }; @@ -68,7 +71,7 @@ export const Toolbar = ({ initialData, preview, onSubmit }: ToolbarProps) => { const handleSubmit = async () => { try { setLoading(true); - await onSubmit({ title, summary, image }); + await onSubmit({ title, summary, image: imageUrl ?? "" }); setLoading(false); } catch (err) { console.log("Error", err); @@ -87,17 +90,24 @@ export const Toolbar = ({ initialData, preview, onSubmit }: ToolbarProps) => {
{!initialData?.image && !preview && ( - + + + )}
-
@@ -144,6 +154,7 @@ export const Toolbar = ({ initialData, preview, onSubmit }: ToolbarProps) => { {summary || "Write a short summary of your content..."} )} + ); }; diff --git a/components/LongForm/Viewer.tsx b/components/LongForm/Viewer.tsx index e0b4730..c409ec3 100644 --- a/components/LongForm/Viewer.tsx +++ b/components/LongForm/Viewer.tsx @@ -21,7 +21,6 @@ const Viewer = ({ initialMarkdown }: EditorProps) => { }); useEffect(() => { - console.log("ERFE", editor); if (editor) { if (!initialContent && initialMarkdown) { console.log("initial md", initialMarkdown); diff --git a/containers/Article/Editor.tsx b/containers/Article/Editor.tsx index 133b8fc..14564ff 100644 --- a/containers/Article/Editor.tsx +++ b/containers/Article/Editor.tsx @@ -7,11 +7,14 @@ import { RiCloseFill } from "react-icons/ri"; import { Avatar, AvatarImage, AvatarFallback } from "@radix-ui/react-avatar"; import { useRouter } from "next/navigation"; import { formatDate } from "@/lib/utils/dates"; -import Actions from "./Actions"; +import { toast } from "sonner"; import { NDKEvent } from "@nostr-dev-kit/ndk"; import { getTagAllValues, getTagValues } from "@/lib/nostr/utils"; import Editor from "@/components/LongForm/Editor"; import { Toolbar } from "@/components/LongForm/ToolBar"; +import { createEvent } from "@/lib/actions/create"; +import { useNDK } from "@/app/_providers/ndk"; +import { unixTimeNowInSeconds } from "@/lib/nostr/dates"; type ArticleProps = { event?: NDKEvent; }; @@ -19,6 +22,7 @@ type ArticleProps = { export default function EditorPage({ event }: ArticleProps) { const router = useRouter(); const [content, setContent] = useState(""); + const { ndk } = useNDK(); async function handleSubmit({ title, summary, @@ -28,6 +32,24 @@ export default function EditorPage({ event }: ArticleProps) { summary: string; image?: string; }) { + if (!ndk) return; + const tags = [ + ["title", title], + ["summary", summary], + ["published_at", unixTimeNowInSeconds().toString()], + ]; + if (image) { + tags.push(["image", image]); + } + const event = await createEvent(ndk, { + content: content, + kind: 30023, + tags: tags, + }); + if (event) { + toast.success("Event Created!"); + router.push(`/article/${event.encode()}`); + } console.log("Writing", title, summary, image, content); } diff --git a/containers/Article/index.tsx b/containers/Article/index.tsx index 1e06af0..b71f1ca 100644 --- a/containers/Article/index.tsx +++ b/containers/Article/index.tsx @@ -69,7 +69,7 @@ export default function ArticlePage({ event }: ArticleProps) {
-
+
{tags.map((t) => (
-

{title}

+

{title}