[WiP] messages update
This commit is contained in:
parent
75f928a71b
commit
61a598e0bb
|
@ -1,12 +1,15 @@
|
||||||
import { Show } from 'solid-js'
|
import { createMemo, Show } from 'solid-js'
|
||||||
import MarkdownIt from 'markdown-it'
|
import MarkdownIt from 'markdown-it'
|
||||||
import { clsx } from 'clsx'
|
import { clsx } from 'clsx'
|
||||||
import styles from './Message.module.scss'
|
import styles from './Message.module.scss'
|
||||||
import DialogAvatar from './DialogAvatar'
|
import DialogAvatar from './DialogAvatar'
|
||||||
|
import { locale } from '../../stores/ui'
|
||||||
|
import type { Message, ChatMember } from '../../graphql/types.gen'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
body: string
|
content: Message
|
||||||
isOwn: boolean
|
ownId: number
|
||||||
|
members: ChatMember[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const md = new MarkdownIt({
|
const md = new MarkdownIt({
|
||||||
|
@ -14,18 +17,29 @@ const md = new MarkdownIt({
|
||||||
})
|
})
|
||||||
|
|
||||||
const Message = (props: Props) => {
|
const Message = (props: Props) => {
|
||||||
|
const formattedTime = createMemo<string>(() => {
|
||||||
|
return new Date(props.content.createdAt * 1000).toLocaleTimeString(locale(), {
|
||||||
|
hour: 'numeric',
|
||||||
|
minute: 'numeric'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('!!! props.ownId:', props.ownId)
|
||||||
|
// возвращать ID автора
|
||||||
|
const isOwn = props.ownId === Number(props.content.author)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class={clsx(styles.Message, props.isOwn && styles.own)}>
|
<div class={clsx(styles.Message, isOwn && styles.own)}>
|
||||||
<Show when={!props.isOwn}>
|
<Show when={!isOwn}>
|
||||||
<div class={styles.author}>
|
<div class={styles.author}>
|
||||||
<DialogAvatar size="small" name={'Message Author'} />
|
<DialogAvatar size="small" name={'Message Author'} />
|
||||||
<div class={styles.name}>Message Author</div>
|
<div class={styles.name}>Message Author</div>
|
||||||
</div>
|
</div>
|
||||||
</Show>
|
</Show>
|
||||||
<div class={styles.body}>
|
<div class={styles.body}>
|
||||||
<div innerHTML={md.render(props.body)} />
|
<div innerHTML={md.render(props.content.body)} />
|
||||||
</div>
|
</div>
|
||||||
<div class={styles.time}>12:24</div>
|
<div class={styles.time}>{formattedTime()}</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
import { For, createSignal, Show, onMount, createEffect, createMemo } from 'solid-js'
|
import { For, createSignal, Show, onMount, createEffect, createMemo } from 'solid-js'
|
||||||
import type { Author, Chat } from '../../graphql/types.gen'
|
import type { Author, Chat, Message as MessageType } from '../../graphql/types.gen'
|
||||||
import { AuthorCard } from '../Author/Card'
|
|
||||||
import { Icon } from '../_shared/Icon'
|
import { Icon } from '../_shared/Icon'
|
||||||
import { Loading } from '../Loading'
|
|
||||||
import DialogCard from '../Inbox/DialogCard'
|
import DialogCard from '../Inbox/DialogCard'
|
||||||
import Search from '../Inbox/Search'
|
import Search from '../Inbox/Search'
|
||||||
import { useSession } from '../../context/session'
|
import { useSession } from '../../context/session'
|
||||||
|
@ -17,35 +15,7 @@ import { clsx } from 'clsx'
|
||||||
import '../../styles/Inbox.scss'
|
import '../../styles/Inbox.scss'
|
||||||
import { useInbox } from '../../context/inbox'
|
import { useInbox } from '../../context/inbox'
|
||||||
import DialogHeader from '../Inbox/DialogHeader'
|
import DialogHeader from '../Inbox/DialogHeader'
|
||||||
|
import { apiClient } from '../../utils/apiClient'
|
||||||
const OWNER_ID = '501'
|
|
||||||
const client = createClient({
|
|
||||||
url: 'https://graphqlzero.almansi.me/api'
|
|
||||||
})
|
|
||||||
|
|
||||||
const messageQuery = `
|
|
||||||
query Comments ($options: PageQueryOptions) {
|
|
||||||
comments(options: $options) {
|
|
||||||
data {
|
|
||||||
id
|
|
||||||
body
|
|
||||||
email
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
const newMessageQuery = `
|
|
||||||
mutation postComment($messageBody: String!) {
|
|
||||||
createComment(
|
|
||||||
input: { body: $messageBody, email: "test@test.com", name: "User" }
|
|
||||||
) {
|
|
||||||
id
|
|
||||||
body
|
|
||||||
name
|
|
||||||
email
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`
|
|
||||||
|
|
||||||
const userSearch = (array: Author[], keyword: string) => {
|
const userSearch = (array: Author[], keyword: string) => {
|
||||||
const searchTerm = keyword.toLowerCase()
|
const searchTerm = keyword.toLowerCase()
|
||||||
|
@ -54,24 +24,17 @@ const userSearch = (array: Author[], keyword: string) => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const postMessage = async (msg: string) => {
|
|
||||||
const response = await client.mutation(newMessageQuery, { messageBody: msg }).toPromise()
|
|
||||||
return response.data.createComment
|
|
||||||
}
|
|
||||||
|
|
||||||
export const InboxView = () => {
|
export const InboxView = () => {
|
||||||
const {
|
const {
|
||||||
chats,
|
chats,
|
||||||
actions: { loadChats }
|
actions: { loadChats }
|
||||||
} = useInbox()
|
} = useInbox()
|
||||||
const [messages, setMessages] = createSignal([])
|
const [messages, setMessages] = createSignal<MessageType[]>([])
|
||||||
const [recipients, setRecipients] = createSignal<Author[]>([])
|
const [recipients, setRecipients] = createSignal<Author[]>([])
|
||||||
const [cashedRecipients, setCashedRecipients] = createSignal<Author[]>([])
|
|
||||||
const [postMessageText, setPostMessageText] = createSignal('')
|
const [postMessageText, setPostMessageText] = createSignal('')
|
||||||
const [loading, setLoading] = createSignal<boolean>(false)
|
|
||||||
const [sortByGroup, setSortByGroup] = createSignal<boolean>(false)
|
const [sortByGroup, setSortByGroup] = createSignal<boolean>(false)
|
||||||
const [sortByPerToPer, setSortByPerToPer] = createSignal<boolean>(false)
|
const [sortByPerToPer, setSortByPerToPer] = createSignal<boolean>(false)
|
||||||
const [selectedChat, setSelectedChat] = createSignal<Chat>()
|
const [currentDialog, setCurrentDialog] = createSignal<Chat>()
|
||||||
const { session } = useSession()
|
const { session } = useSession()
|
||||||
const currentUserId = createMemo(() => session()?.user?.id)
|
const currentUserId = createMemo(() => session()?.user?.id)
|
||||||
|
|
||||||
|
@ -87,24 +50,19 @@ export const InboxView = () => {
|
||||||
|
|
||||||
let chatWindow
|
let chatWindow
|
||||||
const handleOpenChat = async (chat) => {
|
const handleOpenChat = async (chat) => {
|
||||||
setLoading(true)
|
setCurrentDialog(chat)
|
||||||
setSelectedChat(chat)
|
|
||||||
try {
|
try {
|
||||||
await loadMessages({ chat: chat.id })
|
await loadMessages({ chat: chat.id })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setLoading(false)
|
|
||||||
console.error('[loadMessages]', error)
|
console.error('[loadMessages]', error)
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
|
||||||
chatWindow.scrollTop = chatWindow.scrollHeight
|
chatWindow.scrollTop = chatWindow.scrollHeight
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
setLoading(true)
|
|
||||||
try {
|
try {
|
||||||
const response = await loadRecipients({ days: 365 })
|
const response = await loadRecipients({ days: 365 })
|
||||||
setRecipients(response as unknown as Author[])
|
setRecipients(response as unknown as Author[])
|
||||||
setCashedRecipients(response as unknown as Author[])
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
}
|
}
|
||||||
|
@ -113,8 +71,11 @@ export const InboxView = () => {
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
try {
|
try {
|
||||||
const post = await postMessage(postMessageText())
|
const post = await apiClient.createMessage({
|
||||||
setMessages((prev) => [...prev, post])
|
body: postMessageText().toString(),
|
||||||
|
chat: currentDialog().id.toString()
|
||||||
|
})
|
||||||
|
setMessages((prev) => [...prev, post.message])
|
||||||
setPostMessageText('')
|
setPostMessageText('')
|
||||||
chatWindow.scrollTop = chatWindow.scrollHeight
|
chatWindow.scrollTop = chatWindow.scrollHeight
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -135,10 +96,6 @@ export const InboxView = () => {
|
||||||
showModal('inviteToChat')
|
showModal('inviteToChat')
|
||||||
}
|
}
|
||||||
|
|
||||||
createEffect(() => {
|
|
||||||
console.log('!!! chats():', chats())
|
|
||||||
})
|
|
||||||
|
|
||||||
const chatsToShow = () => {
|
const chatsToShow = () => {
|
||||||
if (sortByPerToPer()) {
|
if (sortByPerToPer()) {
|
||||||
return chats().filter((chat) => chat.title.trim().length === 0)
|
return chats().filter((chat) => chat.title.trim().length === 0)
|
||||||
|
@ -149,6 +106,11 @@ export const InboxView = () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createEffect(() => {
|
||||||
|
console.log('!!! messages():', messages())
|
||||||
|
console.log('!!! currentDialog():', currentDialog())
|
||||||
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class="messages container">
|
<div class="messages container">
|
||||||
<Modal variant="narrow" name="inviteToChat">
|
<Modal variant="narrow" name="inviteToChat">
|
||||||
|
@ -211,18 +173,15 @@ export const InboxView = () => {
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-md-8 conversation">
|
<div class="col-md-8 conversation">
|
||||||
<Show when={selectedChat()}>
|
<Show when={currentDialog()}>
|
||||||
<DialogHeader ownId={currentUserId()} chat={selectedChat()} />
|
<DialogHeader ownId={currentUserId()} chat={currentDialog()} />
|
||||||
</Show>
|
</Show>
|
||||||
|
|
||||||
<div class="conversation__messages">
|
<div class="conversation__messages">
|
||||||
<div class="conversation__messages-container" ref={chatWindow}>
|
<div class="conversation__messages-container" ref={chatWindow}>
|
||||||
<Show when={loading()}>
|
|
||||||
<Loading />
|
|
||||||
</Show>
|
|
||||||
<For each={messages()}>
|
<For each={messages()}>
|
||||||
{(comment: { body: string; id: string; email: string }) => (
|
{(message) => (
|
||||||
<Message body={comment.body} isOwn={OWNER_ID === comment.id} />
|
<Message content={message} ownId={currentUserId()} members={currentDialog().members} />
|
||||||
)}
|
)}
|
||||||
</For>
|
</For>
|
||||||
|
|
||||||
|
@ -240,6 +199,7 @@ export const InboxView = () => {
|
||||||
rows={1}
|
rows={1}
|
||||||
onInput={(event) => handleChangeMessage(event)}
|
onInput={(event) => handleChangeMessage(event)}
|
||||||
placeholder="Написать сообщение"
|
placeholder="Написать сообщение"
|
||||||
|
disabled={!currentDialog()?.id}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit" disabled={postMessageText().length === 0} onClick={handleSubmit}>
|
<button type="submit" disabled={postMessageText().length === 0} onClick={handleSubmit}>
|
||||||
|
|
|
@ -4,10 +4,13 @@ export default gql`
|
||||||
mutation createMessage($chat: String!, $body: String!) {
|
mutation createMessage($chat: String!, $body: String!) {
|
||||||
createMessage(chat: $chat, body: $body) {
|
createMessage(chat: $chat, body: $body) {
|
||||||
error
|
error
|
||||||
author {
|
message {
|
||||||
slug
|
|
||||||
id
|
id
|
||||||
chat
|
body
|
||||||
|
author
|
||||||
|
createdAt
|
||||||
|
replyTo
|
||||||
|
updatedAt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -283,13 +283,14 @@ export const apiClient = {
|
||||||
|
|
||||||
createMessage: async (options: MutationCreateMessageArgs) => {
|
createMessage: async (options: MutationCreateMessageArgs) => {
|
||||||
const resp = await privateGraphQLClient.mutation(createMessage, options).toPromise()
|
const resp = await privateGraphQLClient.mutation(createMessage, options).toPromise()
|
||||||
|
console.log('!!! resp:', resp)
|
||||||
return resp.data.createMessage
|
return resp.data.createMessage
|
||||||
},
|
},
|
||||||
|
|
||||||
getChatMessages: async (options: QueryLoadMessagesByArgs) => {
|
getChatMessages: async (options: QueryLoadMessagesByArgs) => {
|
||||||
const resp = await privateGraphQLClient.query(chatMessagesLoadBy, options).toPromise()
|
const resp = await privateGraphQLClient.query(chatMessagesLoadBy, options).toPromise()
|
||||||
console.log('!!! resp:', resp)
|
console.log('[getChatMessages]', resp)
|
||||||
return resp.data
|
return resp.data.loadMessagesBy.messages
|
||||||
},
|
},
|
||||||
|
|
||||||
getRecipients: async (options: QueryLoadRecipientsArgs) => {
|
getRecipients: async (options: QueryLoadRecipientsArgs) => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user