From 61a598e0bbf0f62e3bd3516d8aaec9e21423f6d5 Mon Sep 17 00:00:00 2001 From: ilya-bkv Date: Mon, 5 Dec 2022 08:41:53 +0300 Subject: [PATCH] [WiP] messages update --- src/components/Inbox/Message.tsx | 28 ++++++-- src/components/Views/Inbox.tsx | 80 ++++++--------------- src/graphql/mutation/create-chat-message.ts | 9 ++- src/utils/apiClient.ts | 5 +- 4 files changed, 50 insertions(+), 72 deletions(-) diff --git a/src/components/Inbox/Message.tsx b/src/components/Inbox/Message.tsx index 2b31935b..d31c12cf 100644 --- a/src/components/Inbox/Message.tsx +++ b/src/components/Inbox/Message.tsx @@ -1,12 +1,15 @@ -import { Show } from 'solid-js' +import { createMemo, Show } from 'solid-js' import MarkdownIt from 'markdown-it' import { clsx } from 'clsx' import styles from './Message.module.scss' import DialogAvatar from './DialogAvatar' +import { locale } from '../../stores/ui' +import type { Message, ChatMember } from '../../graphql/types.gen' type Props = { - body: string - isOwn: boolean + content: Message + ownId: number + members: ChatMember[] } const md = new MarkdownIt({ @@ -14,18 +17,29 @@ const md = new MarkdownIt({ }) const Message = (props: Props) => { + const formattedTime = createMemo(() => { + 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 ( -
- +
+
Message Author
-
+
-
12:24
+
{formattedTime()}
) } diff --git a/src/components/Views/Inbox.tsx b/src/components/Views/Inbox.tsx index b8985a7e..9e96d100 100644 --- a/src/components/Views/Inbox.tsx +++ b/src/components/Views/Inbox.tsx @@ -1,8 +1,6 @@ import { For, createSignal, Show, onMount, createEffect, createMemo } from 'solid-js' -import type { Author, Chat } from '../../graphql/types.gen' -import { AuthorCard } from '../Author/Card' +import type { Author, Chat, Message as MessageType } from '../../graphql/types.gen' import { Icon } from '../_shared/Icon' -import { Loading } from '../Loading' import DialogCard from '../Inbox/DialogCard' import Search from '../Inbox/Search' import { useSession } from '../../context/session' @@ -17,35 +15,7 @@ import { clsx } from 'clsx' import '../../styles/Inbox.scss' import { useInbox } from '../../context/inbox' import DialogHeader from '../Inbox/DialogHeader' - -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 - } -} -` +import { apiClient } from '../../utils/apiClient' const userSearch = (array: Author[], keyword: string) => { 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 = () => { const { chats, actions: { loadChats } } = useInbox() - const [messages, setMessages] = createSignal([]) + const [messages, setMessages] = createSignal([]) const [recipients, setRecipients] = createSignal([]) - const [cashedRecipients, setCashedRecipients] = createSignal([]) const [postMessageText, setPostMessageText] = createSignal('') - const [loading, setLoading] = createSignal(false) const [sortByGroup, setSortByGroup] = createSignal(false) const [sortByPerToPer, setSortByPerToPer] = createSignal(false) - const [selectedChat, setSelectedChat] = createSignal() + const [currentDialog, setCurrentDialog] = createSignal() const { session } = useSession() const currentUserId = createMemo(() => session()?.user?.id) @@ -87,24 +50,19 @@ export const InboxView = () => { let chatWindow const handleOpenChat = async (chat) => { - setLoading(true) - setSelectedChat(chat) + setCurrentDialog(chat) try { await loadMessages({ chat: chat.id }) } catch (error) { - setLoading(false) console.error('[loadMessages]', error) } finally { - setLoading(false) chatWindow.scrollTop = chatWindow.scrollHeight } } onMount(async () => { - setLoading(true) try { const response = await loadRecipients({ days: 365 }) setRecipients(response as unknown as Author[]) - setCashedRecipients(response as unknown as Author[]) } catch (error) { console.log(error) } @@ -113,8 +71,11 @@ export const InboxView = () => { const handleSubmit = async () => { try { - const post = await postMessage(postMessageText()) - setMessages((prev) => [...prev, post]) + const post = await apiClient.createMessage({ + body: postMessageText().toString(), + chat: currentDialog().id.toString() + }) + setMessages((prev) => [...prev, post.message]) setPostMessageText('') chatWindow.scrollTop = chatWindow.scrollHeight } catch (error) { @@ -135,10 +96,6 @@ export const InboxView = () => { showModal('inviteToChat') } - createEffect(() => { - console.log('!!! chats():', chats()) - }) - const chatsToShow = () => { if (sortByPerToPer()) { 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 (
@@ -211,18 +173,15 @@ export const InboxView = () => {
- - + +
- - - - {(comment: { body: string; id: string; email: string }) => ( - + {(message) => ( + )} @@ -240,6 +199,7 @@ export const InboxView = () => { rows={1} onInput={(event) => handleChangeMessage(event)} placeholder="Написать сообщение" + disabled={!currentDialog()?.id} />