Sort dialog list by last update time

This commit is contained in:
ilya-bkv 2022-12-15 12:08:03 +03:00
parent f8d1826ae1
commit 7f6d2b99f2
5 changed files with 28 additions and 26 deletions

View File

@ -58,7 +58,7 @@ const DialogCard = (props: DialogProps) => {
<Show when={!props.isChatHeader}> <Show when={!props.isChatHeader}>
<div class={styles.activity}> <div class={styles.activity}>
<Show when={props.lastUpdate}> <Show when={props.lastUpdate}>
<div class={styles.time}>{formattedTime(props.lastUpdate)}</div> <div class={styles.time}>{formattedTime(props.lastUpdate * 1000)}</div>
</Show> </Show>
<Show when={props.counter > 0}> <Show when={props.counter > 0}>
<div class={styles.counter}> <div class={styles.counter}>

View File

@ -18,6 +18,10 @@ import { useRouter } from '../../stores/router'
import { clsx } from 'clsx' import { clsx } from 'clsx'
import styles from '../../styles/Inbox.module.scss' import styles from '../../styles/Inbox.module.scss'
type InboxSearchParams = {
initChat: string
chat: string
}
const userSearch = (array: Author[], keyword: string) => { const userSearch = (array: Author[], keyword: string) => {
const searchTerm = keyword.toLowerCase() const searchTerm = keyword.toLowerCase()
return array.filter((value) => { return array.filter((value) => {
@ -29,10 +33,9 @@ export const InboxView = () => {
const { const {
chats, chats,
messages, messages,
actions: { loadChats, getMessages, sendMessage } actions: { loadChats, getMessages, sendMessage, createChat }
} = useInbox() } = useInbox()
// const [messages, setMessages] = createSignal<MessageType[]>([])
const [recipients, setRecipients] = createSignal<Author[]>([]) const [recipients, setRecipients] = createSignal<Author[]>([])
const [postMessageText, setPostMessageText] = createSignal('') const [postMessageText, setPostMessageText] = createSignal('')
const [sortByGroup, setSortByGroup] = createSignal<boolean>(false) const [sortByGroup, setSortByGroup] = createSignal<boolean>(false)
@ -65,10 +68,6 @@ export const InboxView = () => {
} }
} }
createEffect(() => {
console.log('!!! messages:', messages())
})
onMount(async () => { onMount(async () => {
try { try {
const response = await loadRecipients({ days: 365 }) const response = await loadRecipients({ days: 365 })
@ -95,22 +94,24 @@ export const InboxView = () => {
setPostMessageText(event.target.value) setPostMessageText(event.target.value)
} }
const { actions } = useInbox() const { changeSearchParam, searchParams } = useRouter<InboxSearchParams>()
const urlParams = new URLSearchParams(window.location.search)
const params = Object.fromEntries(urlParams)
createEffect(async () => { createEffect(async () => {
if (textareaParent) { if (textareaParent) {
textareaParent.dataset.replicatedValue = postMessageText() textareaParent.dataset.replicatedValue = postMessageText()
} }
if (params['chat']) { if (searchParams().chat) {
if (chats().length === 0) return const chatToOpen = chats()?.find((chat) => chat.id === searchParams().chat)
const chatToOpen = chats()?.find((chat) => chat.id === params['chat']) if (!chatToOpen) return
await handleOpenChat(chatToOpen) await handleOpenChat(chatToOpen)
return
} }
if (params['initChat']) { if (searchParams().initChat) {
try { try {
const newChat = await actions.createChat([Number(params['initChat'])], '') const newChat = await createChat([Number(searchParams().initChat)], '')
await loadChats() await loadChats()
changeSearchParam('initChat', null)
changeSearchParam('chat', newChat.chat.id)
const chatToOpen = chats().find((chat) => chat.id === newChat.chat.id) const chatToOpen = chats().find((chat) => chat.id === newChat.chat.id)
await handleOpenChat(chatToOpen) await handleOpenChat(chatToOpen)
} catch (error) { } catch (error) {
@ -125,7 +126,7 @@ export const InboxView = () => {
const chatsToShow = () => { const chatsToShow = () => {
const sorted = chats().sort((a, b) => { const sorted = chats().sort((a, b) => {
return a.updatedAt - b.updatedAt return b.updatedAt - a.updatedAt
}) })
if (sortByPerToPer()) { if (sortByPerToPer()) {
return sorted.filter((chat) => chat.title.trim().length === 0) return sorted.filter((chat) => chat.title.trim().length === 0)

View File

@ -33,11 +33,7 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
const loadChats = async () => { const loadChats = async () => {
try { try {
const newChats = await apiClient.getChats({ limit: 50, offset: 0 }) const newChats = await apiClient.getChats({ limit: 50, offset: 0 })
setChats( setChats(newChats)
newChats.sort((x, y) => {
return x.updatedAt < y.updatedAt ? 1 : -1
})
)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
} }
@ -49,14 +45,19 @@ export const InboxProvider = (props: { children: JSX.Element }) => {
const response = await loadMessages({ chat: chatId }) const response = await loadMessages({ chat: chatId })
setMessages(response as unknown as Message[]) setMessages(response as unknown as Message[])
} catch (error) { } catch (error) {
console.log('[loadMessages]', error) console.error('[loadMessages]', error)
} }
} }
const sendMessage = async (args) => { const sendMessage = async (args) => {
try { try {
const post = await apiClient.createMessage(args) const message = await apiClient.createMessage(args)
setMessages((prev) => [...prev, post.message]) setMessages((prev) => [...prev, message])
const chat = chats().find((chat) => chat.id === args.chat)
setChats((prev) => [
...prev.filter((c) => c.id !== chat.id),
{ ...chat, updatedAt: message.createdAt }
])
} catch (error) { } catch (error) {
console.error('[post message error]:', error) console.error('[post message error]:', error)
} }

View File

@ -288,7 +288,7 @@ 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()
return resp.data.createMessage return resp.data.createMessage.message
}, },
getChatMessages: async (options: QueryLoadMessagesByArgs) => { getChatMessages: async (options: QueryLoadMessagesByArgs) => {

View File

@ -4,7 +4,7 @@ import { locale } from '../stores/ui'
// unix timestamp in seconds // unix timestamp in seconds
const formattedTime = (time: number) => const formattedTime = (time: number) =>
createMemo<string>(() => { createMemo<string>(() => {
return new Date(time * 1000).toLocaleTimeString(locale(), { return new Date(time).toLocaleTimeString(locale(), {
hour: 'numeric', hour: 'numeric',
minute: 'numeric' minute: 'numeric'
}) })