[WiP] init & test

This commit is contained in:
ilya-bkv 2022-12-16 08:35:53 +03:00
parent 2e74624240
commit 2f252da9db
4 changed files with 235 additions and 136 deletions

View File

@ -3,6 +3,7 @@
margin: 0 -2.4rem 1.5em; margin: 0 -2.4rem 1.5em;
padding: 0.8rem 2.4rem; padding: 0.8rem 2.4rem;
transition: background-color 0.3s; transition: background-color 0.3s;
border: 1px solid red;
&:hover { &:hover {
background-color: #f6f6f6; background-color: #f6f6f6;
@ -32,25 +33,25 @@
} }
} }
.commentLevel1 { //.commentLevel1 {
margin-left: 3.2rem; // margin-left: 3.2rem;
} //}
//
.commentLevel2 { //.commentLevel2 {
margin-left: 6.4rem; // margin-left: 6.4rem;
} //}
//
.commentLevel3 { //.commentLevel3 {
margin-left: 9.6rem; // margin-left: 9.6rem;
} //}
//
.commentLevel4 { //.commentLevel4 {
margin-left: 12.8rem; // margin-left: 12.8rem;
} //}
//
.commentLevel5 { //.commentLevel5 {
margin-left: 16rem; // margin-left: 16rem;
} //}
.commentControls { .commentControls {
@include font-size(1.2rem); @include font-size(1.2rem);

View File

@ -12,6 +12,7 @@ import { formatDate } from '../../utils'
import { SharePopup } from './SharePopup' 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 CommentWrapper from './CommentWrapper'
export default (props: { export default (props: {
level?: number level?: number
@ -34,7 +35,8 @@ export default (props: {
) )
return ( return (
<div class={clsx(styles.comment, { [styles[`commentLevel${props.level}`]]: Boolean(props.level) })}> <CommentWrapper level={props.level}>
<li class={clsx(styles.comment, { [styles[`commentLevel${props.level}`]]: Boolean(props.level) })}>
<Show when={!!body()}> <Show when={!!body()}>
<div class={styles.commentContent}> <div class={styles.commentContent}>
<Show <Show
@ -142,6 +144,7 @@ export default (props: {
</Show> </Show>
</div> </div>
</Show> </Show>
</div> </li>
</CommentWrapper>
) )
} }

View File

@ -0,0 +1,71 @@
import type { JSX } from 'solid-js/jsx-runtime'
import { Switch, Match } from 'solid-js'
type Props = {
level?: number
children: JSX.Element
}
const CommentWrapper = (props: Props) => {
return (
<Switch fallback={props.children}>
<Match when={props.level === 1}>
<ul>{props.children}</ul>
</Match>
<Match when={props.level === 2}>
<ul>
<li>
<ul>{props.children}</ul>
</li>
</ul>
</Match>
<Match when={props.level === 3}>
<ul>
<li>
<ul>
<li>
<ul>{props.children}</ul>
</li>
</ul>
</li>
</ul>
</Match>
<Match when={props.level === 4}>
<ul>
<li>
<ul>
<li>
<ul>
<li>
<ul>{props.children}</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</Match>
<Match when={props.level === 5}>
<ul>
<li>
<ul>
<li>
<ul>
<li>
<ul>
<li>
<ul>{props.children}</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</Match>
</Switch>
)
}
export default CommentWrapper

View File

@ -1,4 +1,4 @@
import { For, Show, createMemo, createSignal, onMount } from 'solid-js' import { For, Show, createMemo, createSignal, onMount, createEffect } from 'solid-js'
import { useSession } from '../../context/session' import { useSession } from '../../context/session'
import Comment from './Comment' import Comment from './Comment'
import { t } from '../../utils/intl' import { t } from '../../utils/intl'
@ -49,6 +49,25 @@ export const CommentsTree = (props: { shoutSlug: string }) => {
return level return level
} }
onMount(async () => await loadMore()) onMount(async () => await loadMore())
function nestComments(commentList) {
const commentMap = {}
commentList.forEach((comment) => (commentMap[comment.id] = comment))
commentList.forEach((comment) => {
if (comment.replyTo !== null) {
const parent = commentMap[comment.replyTo]
;(parent.children = parent.children || []).push(comment)
}
})
return commentList.filter((comment) => {
return comment.replyTo === null
})
}
createEffect(() => {
console.log('!!! reactions():', nestComments(reactions()))
})
return ( return (
<> <>
<Show when={!isCommentsLoading()} fallback={<Loading />}> <Show when={!isCommentsLoading()} fallback={<Loading />}>
@ -83,15 +102,20 @@ export const CommentsTree = (props: { shoutSlug: string }) => {
</ul> </ul>
</div> </div>
<ul>
<For each={reactions().reverse()}> <For each={reactions().reverse()}>
{(reaction: Reaction) => ( {(reaction: Reaction) => (
<>
{JSON.stringify(getCommentLevel(reaction))}
<Comment <Comment
comment={reaction} comment={reaction}
level={getCommentLevel(reaction)} level={getCommentLevel(reaction)}
canEdit={reaction.createdBy?.slug === session()?.user?.slug} canEdit={reaction.createdBy?.slug === session()?.user?.slug}
/> />
</>
)} )}
</For> </For>
</ul>
<Show when={isLoadMoreButtonVisible()}> <Show when={isLoadMoreButtonVisible()}>
<button onClick={loadMore}>{t('Load more')}</button> <button onClick={loadMore}>{t('Load more')}</button>