2022-11-10 15:06:02 +00:00
|
|
|
|
import { For, createSignal, Show, onMount, createEffect } from 'solid-js'
|
2022-09-22 09:37:49 +00:00
|
|
|
|
import type { Author } from '../../graphql/types.gen'
|
2022-09-09 11:53:35 +00:00
|
|
|
|
import { AuthorCard } from '../Author/Card'
|
2022-09-22 09:37:49 +00:00
|
|
|
|
import { Icon } from '../Nav/Icon'
|
2022-11-09 12:20:13 +00:00
|
|
|
|
import { Loading } from '../Loading'
|
2022-11-10 15:06:02 +00:00
|
|
|
|
import DialogCard from '../Inbox/DialogCard'
|
|
|
|
|
import Search from '../Inbox/Search'
|
|
|
|
|
import { useAuthorsStore } from '../../stores/zine/authors'
|
|
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
|
import '../../styles/Inbox.scss'
|
2022-11-09 12:20:13 +00:00
|
|
|
|
// Для моков
|
|
|
|
|
import { createClient } from '@urql/core'
|
2022-11-11 04:34:18 +00:00
|
|
|
|
import { findAndLoadGraphQLConfig } from '@graphql-codegen/cli'
|
2022-11-09 12:20:13 +00:00
|
|
|
|
|
|
|
|
|
const OWNER_ID = '501'
|
|
|
|
|
const client = createClient({
|
|
|
|
|
url: 'https://graphqlzero.almansi.me/api'
|
|
|
|
|
})
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
2022-09-22 09:37:49 +00:00
|
|
|
|
// interface InboxProps {
|
|
|
|
|
// chats?: Chat[]
|
|
|
|
|
// messages?: Message[]
|
|
|
|
|
// }
|
2022-11-09 12:20:13 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
|
2022-11-10 15:06:02 +00:00
|
|
|
|
const userSearch = (array: Author[], keyword: string) => {
|
|
|
|
|
const searchTerm = keyword.toLowerCase()
|
|
|
|
|
return array.filter((value) => {
|
|
|
|
|
return value.name.toLowerCase().match(new RegExp(searchTerm, 'g'))
|
|
|
|
|
})
|
2022-11-09 12:20:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 17:09:51 +00:00
|
|
|
|
const postMessage = async (msg: string) => {
|
|
|
|
|
const response = await client.mutation(newMessageQuery, { messageBody: msg }).toPromise()
|
|
|
|
|
return response.data.createComment
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 09:37:49 +00:00
|
|
|
|
export const InboxView = () => {
|
2022-11-10 15:06:02 +00:00
|
|
|
|
const [messages, setMessages] = createSignal([])
|
|
|
|
|
const [authors, setAuthors] = createSignal<Author[]>([])
|
|
|
|
|
const [postMessageText, setPostMessageText] = createSignal('')
|
|
|
|
|
const [loading, setLoading] = createSignal<boolean>(false)
|
|
|
|
|
const { sortedAuthors } = useAuthorsStore()
|
|
|
|
|
|
|
|
|
|
createEffect(() => {
|
|
|
|
|
setAuthors(sortedAuthors())
|
|
|
|
|
})
|
|
|
|
|
|
2022-11-11 04:34:18 +00:00
|
|
|
|
// Поиск по диалогам
|
2022-11-10 15:06:02 +00:00
|
|
|
|
const getQuery = (query) => {
|
2022-11-10 15:58:43 +00:00
|
|
|
|
if (query().length >= 2) {
|
2022-11-10 15:06:02 +00:00
|
|
|
|
const match = userSearch(authors(), query())
|
2022-11-11 04:34:18 +00:00
|
|
|
|
console.log('!!! match:', match)
|
2022-11-10 15:06:02 +00:00
|
|
|
|
setAuthors(match)
|
|
|
|
|
} else {
|
|
|
|
|
setAuthors(sortedAuthors())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
2022-11-11 04:34:18 +00:00
|
|
|
|
|
|
|
|
|
let chatWindow
|
2022-11-10 15:06:02 +00:00
|
|
|
|
onMount(async () => {
|
|
|
|
|
setLoading(true)
|
|
|
|
|
await fetchMessages(messageQuery)
|
|
|
|
|
.then(() => {
|
|
|
|
|
setLoading(false)
|
|
|
|
|
chatWindow.scrollTop = chatWindow.scrollHeight
|
|
|
|
|
})
|
|
|
|
|
.catch(() => setLoading(false))
|
|
|
|
|
})
|
2022-11-11 04:34:18 +00:00
|
|
|
|
|
2022-11-09 12:20:13 +00:00
|
|
|
|
const handleSubmit = async () => {
|
|
|
|
|
postMessage(postMessageText())
|
|
|
|
|
.then((result) => {
|
|
|
|
|
setMessages((prev) => [...prev, result])
|
|
|
|
|
})
|
|
|
|
|
.then(() => {
|
|
|
|
|
setPostMessageText('')
|
|
|
|
|
chatWindow.scrollTop = chatWindow.scrollHeight
|
|
|
|
|
})
|
2022-11-14 17:09:51 +00:00
|
|
|
|
.catch(console.error)
|
2022-11-09 12:20:13 +00:00
|
|
|
|
}
|
|
|
|
|
const handleChangeMessage = (event) => {
|
|
|
|
|
setPostMessageText(event.target.value)
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
|
// TODO: get user session
|
|
|
|
|
return (
|
|
|
|
|
<div class="messages container">
|
|
|
|
|
<div class="row">
|
|
|
|
|
<div class="chat-list col-md-4">
|
2022-11-10 15:06:02 +00:00
|
|
|
|
<Search placeholder="Поиск" onChange={getQuery} />
|
2022-09-09 11:53:35 +00:00
|
|
|
|
<div class="chat-list__types">
|
|
|
|
|
<ul>
|
|
|
|
|
<li>
|
2022-11-11 04:34:18 +00:00
|
|
|
|
<strong>Все</strong>
|
2022-09-09 11:53:35 +00:00
|
|
|
|
</li>
|
2022-11-11 04:34:18 +00:00
|
|
|
|
<li>Переписки</li>
|
|
|
|
|
<li>Группы</li>
|
2022-09-09 11:53:35 +00:00
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
2022-11-11 04:34:18 +00:00
|
|
|
|
<div class="holder">
|
|
|
|
|
<div class="dialogs">
|
2022-11-14 17:09:51 +00:00
|
|
|
|
<For each={authors()}>{(author: Author) => <DialogCard author={author} online={true} />}</For>
|
2022-11-11 04:34:18 +00:00
|
|
|
|
</div>
|
2022-09-09 11:53:35 +00:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="col-md-8 conversation">
|
|
|
|
|
<div class="interlocutor user--online">
|
|
|
|
|
<AuthorCard author={{} as Author} hideFollow={true} />
|
|
|
|
|
<div class="user-status">Online</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="conversation__messages">
|
2022-11-09 12:20:13 +00:00
|
|
|
|
<div class="conversation__messages-container" ref={chatWindow}>
|
|
|
|
|
<Show when={loading()}>
|
|
|
|
|
<Loading />
|
|
|
|
|
</Show>
|
|
|
|
|
<For each={messages()}>
|
|
|
|
|
{(comment: { body: string; id: string; email: string }) => (
|
|
|
|
|
<div
|
|
|
|
|
class={`conversation__message-container
|
|
|
|
|
${
|
|
|
|
|
OWNER_ID === comment.id
|
|
|
|
|
? 'conversation__message-container--own'
|
|
|
|
|
: 'conversation__message-container--other'
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<div class="conversation__message">
|
|
|
|
|
{comment.body}
|
|
|
|
|
<div class="conversation__message-details">
|
|
|
|
|
<time>14:26</time>
|
|
|
|
|
{comment.email} id: {comment.id}
|
|
|
|
|
</div>
|
|
|
|
|
<button class="conversation__context-popup-control">
|
|
|
|
|
<Icon name="ellipsis" />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2022-09-09 11:53:35 +00:00
|
|
|
|
</div>
|
2022-11-09 12:20:13 +00:00
|
|
|
|
)}
|
|
|
|
|
</For>
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
2022-11-09 12:20:13 +00:00
|
|
|
|
{/*<div class="conversation__date">*/}
|
|
|
|
|
{/* <time>12 сентября</time>*/}
|
|
|
|
|
{/*</div>*/}
|
2022-09-09 11:53:35 +00:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2022-11-09 12:20:13 +00:00
|
|
|
|
<div class="conversation__message-form">
|
|
|
|
|
<textarea
|
|
|
|
|
value={postMessageText()}
|
|
|
|
|
onInput={(event) => handleChangeMessage(event)}
|
|
|
|
|
placeholder="Написать сообщение"
|
|
|
|
|
/>
|
|
|
|
|
<button type="submit" disabled={postMessageText().length === 0} onClick={handleSubmit}>
|
2022-09-09 11:53:35 +00:00
|
|
|
|
<Icon name="send-message" />
|
|
|
|
|
</button>
|
2022-11-09 12:20:13 +00:00
|
|
|
|
</div>
|
2022-09-09 11:53:35 +00:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|