diff --git a/src/components/Inbox/CreateModalContent.tsx b/src/components/Inbox/CreateModalContent.tsx index d9a04d0a..6f8146fe 100644 --- a/src/components/Inbox/CreateModalContent.tsx +++ b/src/components/Inbox/CreateModalContent.tsx @@ -13,7 +13,7 @@ type Props = { const CreateModalContent = (props: Props) => { const inviteUsers: inviteUser[] = props.users.map((user) => ({ ...user, selected: false })) - const [theme, setTheme] = createSignal('') + const [theme, setTheme] = createSignal(' ') const [slugs, setSlugs] = createSignal([]) const [collectionToInvite, setCollectionToInvite] = createSignal(inviteUsers) let textInput: HTMLInputElement @@ -34,7 +34,8 @@ const CreateModalContent = (props: Props) => { return user['slug'] }) }) - if (slugs().length > 2 && theme().length === 0) { + + if (slugs().length > 1 && theme().length === 1) { setTheme(t('group_chat')) } }) @@ -51,13 +52,14 @@ const CreateModalContent = (props: Props) => { }) } - const { chatEntities, actions } = useInbox() + const { actions } = useInbox() const handleCreate = async () => { try { const initChat = await actions.createChat(slugs(), theme()) console.debug('[initChat]', initChat) hideModal() + await actions.loadChats() } catch (error) { console.error(error) } @@ -66,7 +68,7 @@ const CreateModalContent = (props: Props) => { return (

{t('create_chat')}

- {slugs().length > 2 && ( + {slugs().length > 1 && ( { onClick={handleCreate} disabled={slugs().length === 0} > - {slugs().length > 2 ? t('create_group') : t('create_chat')} + {slugs().length > 1 ? t('create_group') : t('create_chat')}
diff --git a/src/components/Inbox/DialogCard.tsx b/src/components/Inbox/DialogCard.tsx index ef9ae13c..557dcb0b 100644 --- a/src/components/Inbox/DialogCard.tsx +++ b/src/components/Inbox/DialogCard.tsx @@ -10,12 +10,14 @@ type DialogProps = { title?: string ownSlug: string members: ChatMember[] + onClick: () => void } const DialogCard = (props: DialogProps) => { + if (!props.members) return const companions = props.members.filter((member) => member.slug !== props.ownSlug) return ( -
+
{companions.length > 2 ? ( diff --git a/src/components/Views/Inbox.tsx b/src/components/Views/Inbox.tsx index 2108c579..93fb5cd6 100644 --- a/src/components/Views/Inbox.tsx +++ b/src/components/Views/Inbox.tsx @@ -8,13 +8,14 @@ import Search from '../Inbox/Search' import { useSession } from '../../context/session' import { createClient } from '@urql/core' import Message from '../Inbox/Message' -import { loadRecipients, loadChats } from '../../stores/inbox' +import { loadRecipients, loadMessages } from '../../stores/inbox' import { t } from '../../utils/intl' import { Modal } from '../Nav/Modal' import { showModal } from '../../stores/ui' import CreateModalContent from '../Inbox/CreateModalContent' import { clsx } from 'clsx' import '../../styles/Inbox.scss' +import { useInbox } from '../../context/inbox' const OWNER_ID = '501' const client = createClient({ @@ -58,9 +59,12 @@ const postMessage = async (msg: string) => { } export const InboxView = () => { + const { + chats, + actions: { loadChats } + } = useInbox() const [messages, setMessages] = createSignal([]) const [recipients, setRecipients] = createSignal([]) - const [chats, setChats] = createSignal([]) const [cashedRecipients, setCashedRecipients] = createSignal([]) const [postMessageText, setPostMessageText] = createSignal('') const [loading, setLoading] = createSignal(false) @@ -79,29 +83,21 @@ export const InboxView = () => { } } - const fetchMessages = async (query) => { - const response = await client - .query(query, { - options: { slice: { start: 0, end: 3 } } - }) - .toPromise() - if (response.error) console.debug('getMessages', response.error) - setMessages(response.data.comments.data) - } - let chatWindow - onMount(async () => { + const handleOpenChat = async (chatId) => { setLoading(true) try { - await fetchMessages(messageQuery) + await loadMessages({ chat: chatId }) } catch (error) { setLoading(false) - console.error([fetchMessages], error) + 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[]) @@ -110,12 +106,8 @@ export const InboxView = () => { console.log(error) } - try { - const response = await loadChats() - setChats(response as unknown as Chat[]) - } catch (error) { - console.log(error) - } + await loadChats() + console.log('!!! chats:', chats()) }) const handleSubmit = async () => { @@ -198,7 +190,14 @@ export const InboxView = () => { : chats() } > - {(chat) => } + {(chat) => ( + handleOpenChat(chat.id)} + title={chat.title} + members={chat.members} + ownSlug={currentSlug()} + /> + )}
diff --git a/src/context/inbox.tsx b/src/context/inbox.tsx index e3b41d44..e9969c6f 100644 --- a/src/context/inbox.tsx +++ b/src/context/inbox.tsx @@ -1,13 +1,14 @@ -import type { JSX } from 'solid-js' -import { createContext, useContext } from 'solid-js' -import type { Message } from '../graphql/types.gen' +import type { Accessor, JSX } from 'solid-js' +import { createContext, createSignal, useContext } from 'solid-js' +import type { Chat } from '../graphql/types.gen' import { apiClient } from '../utils/apiClient' import { createStore } from 'solid-js/store' type InboxContextType = { - chatEntities: { [chatId: string]: Message[] } + chats: Accessor actions: { createChat: (members: string[], title: string) => Promise + loadChats: () => Promise } } @@ -18,20 +19,33 @@ export function useInbox() { } export const InboxProvider = (props: { children: JSX.Element }) => { - const [chatEntities, setChatEntities] = createStore({}) + const [chats, setChats] = createSignal([]) + const loadChats = async () => { + try { + const chats = await apiClient.getChats({ limit: 50, offset: 0 }) + setChats( + chats.sort((x, y) => { + return x.updatedAt < y.updatedAt ? 1 : -1 + }) + ) + } catch (error) { + console.log(error) + } + } const createChat = async (members: string[], title: string) => { const chat = await apiClient.createChat({ members, title }) - setChatEntities((s) => { - s[chat.id] = chat + setChats((prevChats) => { + return [chat, ...prevChats] }) return chat } const actions = { - createChat + createChat, + loadChats } - const value: InboxContextType = { chatEntities, actions } + const value: InboxContextType = { chats, actions } return {props.children} } diff --git a/src/graphql/query/chat-messages-load-by.ts b/src/graphql/query/chat-messages-load-by.ts index 48796e30..cfba1e55 100644 --- a/src/graphql/query/chat-messages-load-by.ts +++ b/src/graphql/query/chat-messages-load-by.ts @@ -8,8 +8,8 @@ export default gql` author body createdAt + id updatedAt - seen } } } diff --git a/src/stores/inbox.ts b/src/stores/inbox.ts index 9cc59067..08c764a7 100644 --- a/src/stores/inbox.ts +++ b/src/stores/inbox.ts @@ -1,9 +1,10 @@ import { apiClient } from '../utils/apiClient' +import type { MessagesBy } from '../graphql/types.gen' export const loadRecipients = async (by = {}): Promise => { return await apiClient.getRecipients(by) } -export const loadChats = async (): Promise => { - return await apiClient.getChats({ limit: 50, offset: 0 }) +export const loadMessages = async (by: MessagesBy): Promise => { + return await apiClient.getChatMessages({ by, limit: 50, offset: 0 }) } diff --git a/src/utils/apiClient.ts b/src/utils/apiClient.ts index 243a44f3..eee44d50 100644 --- a/src/utils/apiClient.ts +++ b/src/utils/apiClient.ts @@ -10,7 +10,8 @@ import type { QueryLoadMessagesByArgs, MutationCreateChatArgs, MutationCreateMessageArgs, - QueryLoadRecipientsArgs + QueryLoadRecipientsArgs, + Chat } from '../graphql/types.gen' import { publicGraphQLClient } from '../graphql/publicGraphQLClient' import { getToken, privateGraphQLClient } from '../graphql/privateGraphQLClient' @@ -270,7 +271,7 @@ export const apiClient = { }, // inbox - getChats: async (options: QueryLoadChatsArgs) => { + getChats: async (options: QueryLoadChatsArgs): Promise => { const resp = await privateGraphQLClient.query(myChats, options).toPromise() return resp.data.loadChats.chats }, @@ -287,8 +288,10 @@ export const apiClient = { getChatMessages: async (options: QueryLoadMessagesByArgs) => { const resp = await privateGraphQLClient.query(chatMessagesLoadBy, options).toPromise() - return resp.data.loadChat + console.log('!!! resp:', resp) + // return resp.data.loadChat }, + getRecipients: async (options: QueryLoadRecipientsArgs) => { const resp = await privateGraphQLClient.query(loadRecipients, options).toPromise() return resp.data.loadRecipients.members