From 95d6d789fafdaf332bd629f5c6570a8aba62b740 Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 15 Apr 2021 14:41:02 -0500 Subject: [PATCH] pre recursive comments --- api/resolvers/item.js | 40 +++++++++++++++++--------------------- api/typeDefs/item.js | 3 ++- components/comment.js | 44 +++++++++++++++++++++++++++++++++++++++--- components/comments.js | 35 +++++++++++++++++++++++++++++++++ components/item.js | 9 +++------ pages/items/[id].js | 7 ++++--- 6 files changed, 103 insertions(+), 35 deletions(-) create mode 100644 components/comments.js diff --git a/api/resolvers/item.js b/api/resolvers/item.js index 6dc160d0..760f3839 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -31,24 +31,33 @@ export default { Query: { items: async (parent, args, { models }) => { return await models.$queryRaw(` - SELECT id, "created_at" as "createdAt", title, url, text, "userId", ltree2text("path") AS "path" + SELECT id, "created_at" as "createdAt", title, url, text, + "userId", nlevel(path)-1 AS depth, ltree2text("path") AS "path" FROM "Item" - WHERE "parentId" IS NULL - ORDER BY "path"`) + WHERE "parentId" IS NULL`) }, item: async (parent, { id }, { models }) => { const res = await models.$queryRaw(` - SELECT id, "created_at" as "createdAt", title, url, text, "parentId", "userId", ltree2text("path") AS "path" + SELECT id, "created_at" as "createdAt", title, url, text, + "parentId", "userId", nlevel(path)-1 AS depth, ltree2text("path") AS "path" FROM "Item" - WHERE id = ${id} - ORDER BY "path"`) + WHERE id = ${id}`) return res.length ? res[0] : null }, - ncomments: async (parent, { parentId }, { models }) => { + comments: async (parent, { parentId }, { models }) => { return await models.$queryRaw(` - SELECT id, "created_at" as "createdAt", title, url, text, "userId", ltree2text("path") AS "path" + SELECT id, "created_at" as "createdAt", text, "parentId", + "userId", nlevel(path)-1 AS depth, ltree2text("path") AS "path" FROM "Item" + WHERE path <@ (SELECT path FROM "Item" where id = ${parentId}) AND id != ${parentId} ORDER BY "path"`) + }, + root: async (parent, { id }, { models }) => { + const res = await models.$queryRaw(` + SELECT id, title + FROM "Item" + WHERE id = (SELECT ltree2text(subltree(path, 0, 1))::integer FROM "Item" WHERE id = ${id})`) + return res.length ? res[0] : null } }, @@ -87,24 +96,11 @@ export default { Item: { user: async (item, args, { models }) => await models.user.findUnique({ where: { id: item.userId } }), - depth: async (item, args, { models }) => { - if (item.path) { - return item.path.split('.').length - 1 - } - - // as the result of a mutation, path is not populated - const [{ path }] = await models.$queryRaw` - SELECT ltree2text("path") AS "path" - FROM "Item" - WHERE id = ${item.id}` - - return path.split('.').length - 1 - }, ncomments: async (item, args, { models }) => { const [{ count }] = await models.$queryRaw` SELECT count(*) FROM "Item" - WHERE path <@ text2ltree(${item.id}) AND id != ${item.id}` + WHERE path <@ text2ltree(${item.path}) AND id != ${item.id}` return count }, sats: () => 0 diff --git a/api/typeDefs/item.js b/api/typeDefs/item.js index 20319998..9124f731 100644 --- a/api/typeDefs/item.js +++ b/api/typeDefs/item.js @@ -4,7 +4,8 @@ export default gql` extend type Query { items: [Item!]! item(id: ID!): Item - ncomments(id: ID!): [Item!]! + comments(parentId: ID!): [Item!]! + root(id: ID!): Item } extend type Mutation { diff --git a/components/comment.js b/components/comment.js index 03418f45..a587442d 100644 --- a/components/comment.js +++ b/components/comment.js @@ -5,6 +5,7 @@ import Text from './text' import Link from 'next/link' import Reply from './reply' import { useState } from 'react' +import { gql, useQuery } from '@apollo/client' function timeSince (timeStamp) { const now = new Date() @@ -26,8 +27,42 @@ function timeSince (timeStamp) { } } -export default function Comment ({ item, children }) { - const [reply, setReply] = useState(false) +function Parent ({ item }) { + const { data } = useQuery( + gql`{ + root(id: ${item.id}) { + id + title + } + }` + ) + + const ParentFrag = () => ( + <> + \ + + parent + + + ) + + if (!data) { + return + } + + return ( + <> + {data.root.id !== item.parentId && } + \ + + {data.root.title} + + + ) +} + +export default function Comment ({ item, children, replyOpen, includeParent }) { + const [reply, setReply] = useState(replyOpen) return ( <> @@ -43,7 +78,10 @@ export default function Comment ({ item, children }) { \ {item.sats} sats \ - {item.ncomments} replies + + {item.ncomments} replies + + {includeParent && }
{item.text} diff --git a/components/comments.js b/components/comments.js new file mode 100644 index 00000000..b60a1753 --- /dev/null +++ b/components/comments.js @@ -0,0 +1,35 @@ +import { useQuery, gql } from '@apollo/client' +import Comment from './comment' + +export default function Comments ({ parentId, baseDepth }) { + const { data } = useQuery( + gql`{ + comments(parentId: ${parentId}) { + id + createdAt + text + user { + name + } + depth + sats + ncomments + } + }` + ) + + if (!data) return null + + return ( +
+ {data.comments.map(item => ( +
+ +
+ ))} +
+ ) +} diff --git a/components/item.js b/components/item.js index 6b14e246..291a10a9 100644 --- a/components/item.js +++ b/components/item.js @@ -37,7 +37,9 @@ export default function Item ({ item, children }) {
{item.sats} sats \ - {item.ncomments} comments + + {item.ncomments} comments + \ @{item.user.name} @@ -50,11 +52,6 @@ export default function Item ({ item, children }) { {children && (
{children} - {item.comments - ? item.comments.map((item) => ( - - )) - : null}
)} diff --git a/pages/items/[id].js b/pages/items/[id].js index 75de19af..91f1da21 100644 --- a/pages/items/[id].js +++ b/pages/items/[id].js @@ -5,6 +5,7 @@ import ApolloClient from '../../api/client' import Reply from '../../components/reply' import Comment from '../../components/comment' import Text from '../../components/text' +import Comments from '../../components/comments' export async function getServerSideProps ({ params }) { const { error, data: { item } } = await ApolloClient.query({ @@ -20,6 +21,7 @@ export async function getServerSideProps ({ params }) { user { name } + depth sats ncomments } @@ -43,9 +45,7 @@ export default function FullItem ({ item }) { return ( {item.parentId - ? ( - - ) + ? : ( <> @@ -54,6 +54,7 @@ export default function FullItem ({ item }) { )} + ) }