[WiP] createReaction debug

This commit is contained in:
ilya-bkv 2022-12-23 08:24:31 +03:00
parent 9ec3e9a525
commit 3411a23cf7
6 changed files with 53 additions and 30 deletions

View File

@ -2,26 +2,30 @@ import styles from './Comment.module.scss'
import type { JSX } from 'solid-js/jsx-runtime' import type { JSX } from 'solid-js/jsx-runtime'
import { Icon } from '../_shared/Icon' import { Icon } from '../_shared/Icon'
import { AuthorCard } from '../Author/Card' import { AuthorCard } from '../Author/Card'
import { Show, createMemo, createSignal } from 'solid-js' import { Show, createMemo, createSignal, createEffect } from 'solid-js'
import { clsx } from 'clsx' import { clsx } from 'clsx'
import type { Author, Reaction as Point } from '../../graphql/types.gen' import type { Author, Reaction as Point } from '../../graphql/types.gen'
import { t } from '../../utils/intl' import { t } from '../../utils/intl'
// import { createReaction, updateReaction, deleteReaction } from '../../stores/zine/reactions' import { createReaction, updateReaction, deleteReaction } from '../../stores/zine/reactions'
import MD from './MD' import MD from './MD'
import { deleteReaction } from '../../stores/zine/reactions'
import { formatDate } from '../../utils' 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 { apiClient } from '../../utils/apiClient'
export default (props: { type Props = {
level?: number level?: number
comment: Partial<Point> comment: Partial<Point>
canEdit?: boolean canEdit?: boolean
compact?: boolean compact?: boolean
children?: JSX.Element[] children?: JSX.Element[]
}) => { parent?: number
}
export default (props: Props) => {
const [isReplyVisible, setIsReplyVisible] = createSignal(false) const [isReplyVisible, setIsReplyVisible] = createSignal(false)
const [postMessageText, setPostMessageText] = createSignal('')
const comment = createMemo(() => props.comment) const comment = createMemo(() => props.comment)
const body = createMemo(() => (comment().body || '').trim()) const body = createMemo(() => (comment().body || '').trim())
@ -31,6 +35,18 @@ export default (props: {
deleteReaction(comment().id) deleteReaction(comment().id)
} }
} }
const compose = (event) => setPostMessageText(event.target.value)
const handleCreate = async (event) => {
event.preventDefault()
// await createReaction({
await apiClient.createReaction({
kind: 7,
replyTo: props.parent,
body: postMessageText(),
shout: comment().shout.slug
})
}
const formattedDate = createMemo(() => const formattedDate = createMemo(() =>
formatDate(new Date(comment()?.createdAt), { hour: 'numeric', minute: 'numeric' }) formatDate(new Date(comment()?.createdAt), { hour: 'numeric', minute: 'numeric' })
) )
@ -131,13 +147,21 @@ export default (props: {
</div> </div>
<Show when={isReplyVisible()}> <Show when={isReplyVisible()}>
<form class={styles.replyForm}> <form class={styles.replyForm} onSubmit={(event) => handleCreate(event)}>
<textarea name="reply" id="reply" rows="5" /> <textarea
value={postMessageText()}
rows={1}
onInput={(event) => compose(event)}
placeholder="Написать сообщение"
/>
<div class={styles.replyFormControls}> <div class={styles.replyFormControls}>
<button class="button button--light" onClick={() => setIsReplyVisible(false)}> <button class="button button--light" onClick={() => setIsReplyVisible(false)}>
{t('cancel')} {t('cancel')}
</button> </button>
<button class="button">{t('Send')}</button> <button type="submit" class="button">
{t('Send')}
</button>
</div> </div>
</form> </form>
</Show> </Show>

View File

@ -34,7 +34,6 @@ export const CommentsTree = (props: { shoutSlug: string }) => {
try { try {
const page = getCommentsPage() const page = getCommentsPage()
setIsCommentsLoading(true) setIsCommentsLoading(true)
const { hasMore } = await loadReactionsBy({ const { hasMore } = await loadReactionsBy({
by: { shout: props.shoutSlug, comment: true }, by: { shout: props.shoutSlug, comment: true },
limit: ARTICLE_COMMENTS_PAGE_SIZE, limit: ARTICLE_COMMENTS_PAGE_SIZE,
@ -68,6 +67,10 @@ export const CommentsTree = (props: { shoutSlug: string }) => {
}) })
} }
createEffect(() => {
console.log('!!! re:', nestComments(reactions()))
})
return ( return (
<> <>
<Show when={!isCommentsLoading()} fallback={<Loading />}> <Show when={!isCommentsLoading()} fallback={<Loading />}>
@ -107,10 +110,11 @@ export const CommentsTree = (props: { shoutSlug: string }) => {
{(reaction: NestedReaction) => ( {(reaction: NestedReaction) => (
<Comment <Comment
comment={reaction} comment={reaction}
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) => { children={(reaction.children || []).map((r) => {
return <Comment comment={r} /> return <Comment comment={r} parent={reaction.id} />
})} })}
/> />
)} )}

View File

@ -15,17 +15,11 @@ export default gql`
kind kind
range range
createdAt createdAt
shout shout {
replyTo {
id id
createdBy { slug
slug
userpic
name
}
body
kind
} }
replyTo
} }
} }
} }

View File

@ -146,7 +146,7 @@ export type Message = {
chatId: Scalars['String'] chatId: Scalars['String']
createdAt: Scalars['Int'] createdAt: Scalars['Int']
id: Scalars['Int'] id: Scalars['Int']
replyTo?: Maybe<Scalars['String']> replyTo?: Maybe<Scalars['Int']>
seen?: Maybe<Scalars['Boolean']> seen?: Maybe<Scalars['Boolean']>
updatedAt?: Maybe<Scalars['Int']> updatedAt?: Maybe<Scalars['Int']>
} }

View File

@ -1,4 +1,4 @@
import type { Reaction } from '../../graphql/types.gen' import type { Reaction, ReactionInput } from '../../graphql/types.gen'
import { apiClient } from '../../utils/apiClient' import { apiClient } from '../../utils/apiClient'
import { createSignal } from 'solid-js' import { createSignal } from 'solid-js'
// TODO: import { roomConnect } from '../../utils/p2p' // TODO: import { roomConnect } from '../../utils/p2p'
@ -23,8 +23,8 @@ export const loadReactionsBy = async ({
setSortedReactions(data) setSortedReactions(data)
return { hasMore } return { hasMore }
} }
export const createReaction = async (reaction: Reaction) => { export const createReaction = async (reaction: ReactionInput) => {
const { reaction: r } = await apiClient.createReaction({ reaction }) const { reaction: r } = await apiClient.createReaction(reaction)
return r return r
} }
export const updateReaction = async (reaction: Reaction) => { export const updateReaction = async (reaction: Reaction) => {

View File

@ -12,7 +12,8 @@ import type {
MutationCreateMessageArgs, MutationCreateMessageArgs,
QueryLoadRecipientsArgs, QueryLoadRecipientsArgs,
User, User,
ProfileInput ProfileInput,
ReactionInput
} from '../graphql/types.gen' } from '../graphql/types.gen'
import { publicGraphQLClient } from '../graphql/publicGraphQLClient' import { publicGraphQLClient } from '../graphql/publicGraphQLClient'
import { getToken, privateGraphQLClient } from '../graphql/privateGraphQLClient' import { getToken, privateGraphQLClient } from '../graphql/privateGraphQLClient'
@ -229,16 +230,16 @@ export const apiClient = {
console.debug('createArticle response:', response) console.debug('createArticle response:', response)
return response.data.createShout return response.data.createShout
}, },
createReaction: async ({ reaction }) => { createReaction: async (input: ReactionInput) => {
const response = await privateGraphQLClient.mutation(reactionCreate, { reaction }).toPromise() const response = await privateGraphQLClient.mutation(reactionCreate, { reaction: input }).toPromise()
console.debug('[api-client] [api] create reaction mutation called') console.log('!!! response:', response)
return response.data.createReaction return response.data
}, },
// CUDL // CUDL
updateReaction: async ({ reaction }) => { updateReaction: async (reaction) => {
const response = await privateGraphQLClient.mutation(reactionUpdate, { reaction }).toPromise() const response = await privateGraphQLClient.mutation(reactionUpdate, reaction).toPromise()
return response.data.createReaction return response.data.createReaction
}, },