[WiP] revert tree comments

This commit is contained in:
ilya-bkv 2022-12-23 12:10:17 +03:00
parent e85e011694
commit 8efde302a6
4 changed files with 59 additions and 13 deletions

View File

@ -9,11 +9,32 @@
margin-bottom: 0; margin-bottom: 0;
} }
$levels: 0, 1, 2, 3, 4; .comment {
$padding: 1rem; &:before,
@each $level in $levels { &:after {
&.commentLevel-#{level} { content: '';
padding-left: $padding * $level; left: 0;
position: absolute;
}
&:before {
border-bottom: 2px solid #ccc;
border-left: 2px solid #ccc;
border-radius: 0 0 0 1.2rem;
top: -1rem;
height: 2.4rem;
width: 1.2rem;
}
&:after {
background: #ccc;
height: 100%;
top: 0;
width: 2px;
}
&:last-child:after {
display: none;
} }
} }

View File

@ -13,6 +13,7 @@ import { SharePopup } from './SharePopup'
import stylesHeader from '../Nav/Header.module.scss' import stylesHeader from '../Nav/Header.module.scss'
import Userpic from '../Author/Userpic' import Userpic from '../Author/Userpic'
import { apiClient } from '../../utils/apiClient' import { apiClient } from '../../utils/apiClient'
import { ReactionKind } from '../../graphql/types.gen'
type Props = { type Props = {
level?: number level?: number
@ -41,20 +42,18 @@ export default (props: Props) => {
event.preventDefault() event.preventDefault()
// await createReaction({ // await createReaction({
await apiClient.createReaction({ await apiClient.createReaction({
kind: 7,
replyTo: props.parent, replyTo: props.parent,
body: postMessageText(), body: postMessageText(),
shout: comment().shout.slug, shout: comment().shout.id
kind: 7
}) })
} }
const formattedDate = createMemo(() => const formattedDate = createMemo(() =>
formatDate(new Date(comment()?.createdAt), { hour: 'numeric', minute: 'numeric' }) formatDate(new Date(comment()?.createdAt), { hour: 'numeric', minute: 'numeric' })
) )
console.log('!!! lvl:', props.level)
return ( return (
<li class={clsx(styles.comment, { [styles[`commentLevel-${props.level}`]]: props.level })}> <li class={clsx(styles.comment, { [styles[`commentLevel${props.level}`]]: Boolean(props.level) })}>
{props.level}
<Show when={!!body()}> <Show when={!!body()}>
<div class={styles.commentContent}> <div class={styles.commentContent}>
<Show <Show
@ -170,6 +169,9 @@ export default (props: Props) => {
</Show> </Show>
</div> </div>
</Show> </Show>
<Show when={props.children}>
<ul>{props.children}</ul>
</Show>
</li> </li>
) )
} }

View File

@ -11,7 +11,7 @@ import { byCreated, byStat } from '../../utils/sortby'
import { Loading } from '../Loading' import { Loading } from '../Loading'
type NestedReaction = { type NestedReaction = {
children: Reaction[] children: Reaction[] | []
} & Reaction } & Reaction
const ARTICLE_COMMENTS_PAGE_SIZE = 50 const ARTICLE_COMMENTS_PAGE_SIZE = 50
@ -53,6 +53,24 @@ export const CommentsTree = (props: { shoutSlug: string }) => {
} }
onMount(async () => await loadMore()) onMount(async () => await loadMore())
const nestComments = (commentList) => {
const commentMap = {}
commentList.forEach((comment) => {
commentMap[comment.id] = comment
if (comment.replyTo !== null) {
const parent = commentMap[comment.replyTo] ?? []
;(parent.children = parent.children || []).push(comment)
}
})
return commentList.filter((comment) => {
return !comment.replyTo
})
}
createEffect(() => {
console.log('!!! re:', nestComments(reactions()))
})
return ( return (
<> <>
<Show when={!isCommentsLoading()} fallback={<Loading />}> <Show when={!isCommentsLoading()} fallback={<Loading />}>
@ -88,13 +106,16 @@ export const CommentsTree = (props: { shoutSlug: string }) => {
</div> </div>
<ul class={styles.comments}> <ul class={styles.comments}>
<For each={reactions().reverse()}> <For each={nestComments(reactions().reverse())}>
{(reaction: NestedReaction) => ( {(reaction: NestedReaction) => (
<Comment <Comment
comment={reaction} comment={reaction}
parent={reaction.id} parent={reaction.id}
level={getCommentLevel(reaction)} level={getCommentLevel(reaction)}
canEdit={reaction?.createdBy?.slug === session()?.user?.slug} canEdit={reaction?.createdBy?.slug === session()?.user?.slug}
children={(reaction.children || []).map((r) => {
return <Comment comment={r} parent={reaction.id} />
})}
/> />
)} )}
</For> </For>

View File

@ -231,7 +231,9 @@ export const apiClient = {
return response.data.createShout return response.data.createShout
}, },
createReaction: async (reaction) => { createReaction: async (reaction) => {
const response = await privateGraphQLClient.mutation(reactionCreate, { reaction: reaction }).toPromise() //TODO: add ReactionInput Type after debug
const response = await privateGraphQLClient.mutation(reactionCreate, { reaction }).toPromise()
console.log('!!! response:', response)
return response.data return response.data
}, },