From 6e3858bf835c7b4dbab970610bdbddcb12e2af5f Mon Sep 17 00:00:00 2001 From: Samson Zhang Date: Sat, 10 Jul 2021 11:15:55 -0700 Subject: [PATCH 1/3] add editing, deletion, and linked snippets (basic) back to post page --- pages/[username]/p/[postUrlName].tsx | 88 +++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 2 deletions(-) diff --git a/pages/[username]/p/[postUrlName].tsx b/pages/[username]/p/[postUrlName].tsx index ed9b66d..f0672a2 100644 --- a/pages/[username]/p/[postUrlName].tsx +++ b/pages/[username]/p/[postUrlName].tsx @@ -26,6 +26,15 @@ import CommentContainerItem from "../../../components/comment-container-item"; import {useSession} from "next-auth/client"; import {RiHeartFill, RiHeartLine} from "react-icons/ri"; import axios from "axios"; +import MoreMenu from "../../../components/more-menu"; +import MoreMenuItem from "../../../components/more-menu-item"; +import SpinnerButton from "../../../components/spinner-button"; +import {FiChevronDown, FiChevronUp, FiEdit2, FiTrash} from "react-icons/fi"; +import UpModal from "../../../components/up-modal"; +import {useRouter} from "next/router"; +import UpBanner from "../../../components/UpBanner"; +import SnippetItemReduced from "../../../components/snippet-item-reduced"; +import Accordion from "react-robust-accordion"; export default function PostPage({postData, linkedSnippets, projectData, thisOwner, thisAuthor, thisLinks}: { postData: DatedObj, @@ -36,10 +45,14 @@ export default function PostPage({postData, linkedSnippets, projectData, thisOwn thisLinks: DatedObj[], }) { const [session, loading] = useSession(); + const router = useRouter(); const [reactionsIteration, setReactionsIteration] = useState(0); const [commentsIteration, setCommentsIteration] = useState(0); const [reactionsUnauthModalOpen, setReactionsUnauthModalOpen] = useState(false); + const [isDeleteOpen, setIsDeleteOpen] = useState(false); + const [isDeleteLoading, setIsDeleteLoading] = useState(false); + const [viewLinkedSnippetsOpen, setViewLinkedSnippetsOpen] = useState(false); const {_id: projectId, userId, name: projectName, description, urlName: projectUrlName} = projectData; const isOwner = session && (session.userId === thisAuthor._id); @@ -48,6 +61,8 @@ export default function PostPage({postData, linkedSnippets, projectData, thisOwn const {data: reactions, error: reactionsError}: responseInterface<{data: DatedObj[]}, any> = useSWR(`/api/reaction?targetId=${postData._id}&iter=${reactionsIteration}`); const {data: comments, error: commentsError}: responseInterface<{data: DatedObj[]}, any> = useSWR(`/api/comment?targetId=${postData._id}&iter=${commentsIteration}`); + const linkedSnippetsReady = linkedSnippetsData && linkedSnippetsData.snippets && !!linkedSnippetsData.snippets.length; + function onLike() { if (session) { axios.post("/api/reaction", { @@ -62,6 +77,22 @@ export default function PostPage({postData, linkedSnippets, projectData, thisOwn } } + function onDelete() { + setIsDeleteLoading(true); + + axios.delete("/api/post", { + data: { + postId: postData._id, + tempId: postData.urlName, + } + }).then(() => { + router.push(`/@${thisOwner.username}/${projectUrlName}`); + }).catch(e => { + setIsDeleteLoading(false); + console.log(e); + }); + } + return ( / -

{postData.title}

+
+

{postData.title}

+
+ {isOwner && ( +
+ + } href={`/post/${postData._id}?back=${encodeURIComponent(document.location.href)}`}/> + } onClick={() => setIsDeleteOpen(true)}/> + + +

Are you sure you want to delete this post? This action cannot be undone.

+
+ + Delete + + +
+
+
+ )} +
+
-

{format(new Date(postData.createdAt), "MMMM d, yyyy")} | {readingTime(postData.body).text}

+

{format(new Date(postData.createdAt), "MMMM d, yyyy")} | {readingTime(postData.body).text}{!isOwner && !!linkedSnippets.length && ` | ${linkedSnippets.length} linked snippets`}

{reactions && reactions.data && (
{reactions.data.length > 0 && ( @@ -102,6 +154,38 @@ export default function PostPage({postData, linkedSnippets, projectData, thisOwn
)}
+ {isOwner && !!linkedSnippets.length && ( + + +

View linked snippets ({linkedSnippets.length})

+
+ {viewLinkedSnippetsOpen ? ( + + ) : ( + + )} +
+ + )} + className="w-full" + openState={viewLinkedSnippetsOpen} + setOpenState={setViewLinkedSnippetsOpen} + > +

Only you (the author of this post) can see linked snippets. Public viewers will only be able to see the count of linked snippets.

+
+ {linkedSnippetsReady && linkedSnippetsData.snippets.map((snippet, i, a) => ( +
+ {(i === 0 || format(new Date(snippet.createdAt), "yyyy-MM-dd") !== format(new Date(a[i-1].createdAt), "yyyy-MM-dd")) && ( +

{format(new Date(snippet.createdAt), "EEEE, MMMM d")}

+ )} + +
+ ))} +
+
+ )}
Date: Sat, 10 Jul 2021 11:17:55 -0700 Subject: [PATCH 2/3] bring back notification code --- pages/[username]/p/[postUrlName].tsx | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pages/[username]/p/[postUrlName].tsx b/pages/[username]/p/[postUrlName].tsx index f0672a2..6f5d7a8 100644 --- a/pages/[username]/p/[postUrlName].tsx +++ b/pages/[username]/p/[postUrlName].tsx @@ -15,7 +15,7 @@ import { } from "../../../utils/types"; import ProfileShell from "../../../components/ProfileShell"; import UpSEO from "../../../components/up-seo"; -import React, {useState} from "react"; +import React, {useContext, useEffect, useState} from "react"; import SlateReadOnly from "../../../components/SlateReadOnly"; import UpInlineButton from "../../../components/style/UpInlineButton"; import {format} from "date-fns"; @@ -35,6 +35,7 @@ import {useRouter} from "next/router"; import UpBanner from "../../../components/UpBanner"; import SnippetItemReduced from "../../../components/snippet-item-reduced"; import Accordion from "react-robust-accordion"; +import {NotifsContext} from "../../_app"; export default function PostPage({postData, linkedSnippets, projectData, thisOwner, thisAuthor, thisLinks}: { postData: DatedObj, @@ -46,6 +47,7 @@ export default function PostPage({postData, linkedSnippets, projectData, thisOwn }) { const [session, loading] = useSession(); const router = useRouter(); + const {notifsIteration, setNotifsIteration} = useContext(NotifsContext); const [reactionsIteration, setReactionsIteration] = useState(0); const [commentsIteration, setCommentsIteration] = useState(0); @@ -93,6 +95,18 @@ export default function PostPage({postData, linkedSnippets, projectData, thisOwn }); } + useEffect(() => { + if (router.query.notif) { + axios.post("/api/notification", { + id: router.query.notif, + }).then(() => { + setNotifsIteration(notifsIteration + 1); + }).catch(e => { + console.log(e); + }) + } + }, [router.query]); + return ( Date: Sat, 10 Jul 2021 11:18:09 -0700 Subject: [PATCH 3/3] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bde380a..dedf83b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "updately-pro", - "version": "0.20.0", + "version": "0.20.1", "private": true, "scripts": { "dev": "next dev",