mirror of
https://github.com/hazemKrimi/crimson-quirks-ui.git
synced 2026-05-01 18:20:28 +00:00
Complete support chat logic
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useHistory, useLocation } from 'react-router';
|
import { useHistory, useLocation } from 'react-router';
|
||||||
import { useLazyQuery, useReactiveVar } from '@apollo/client';
|
import { useReactiveVar } from '@apollo/client';
|
||||||
import { roleVar } from '../../graphql/state';
|
import { roleVar } from '../../graphql/state';
|
||||||
import { Box, Button, Text } from '..';
|
import { Box, Button, Text } from '..';
|
||||||
import { Wrapper } from './styles';
|
import { Wrapper } from './styles';
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import * as Yup from 'yup';
|
import * as Yup from 'yup';
|
||||||
import { useFormik } from 'formik';
|
import { useFormik } from 'formik';
|
||||||
import { useLocation, useParams, useHistory } from 'react-router-dom';
|
import { useParams, useHistory } from 'react-router-dom';
|
||||||
import { useReactiveVar, useSubscription } from '@apollo/client';
|
import { useReactiveVar } from '@apollo/client';
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import { roleVar, userVar } from '../../graphql/state';
|
import { roleVar, userVar } from '../../graphql/state';
|
||||||
import { Wrapper } from './styles';
|
import { Wrapper } from './styles';
|
||||||
@@ -12,21 +12,20 @@ import {
|
|||||||
CreateThreadMutationVariables,
|
CreateThreadMutationVariables,
|
||||||
GetThreadByIdQuery,
|
GetThreadByIdQuery,
|
||||||
GetThreadByIdQueryVariables,
|
GetThreadByIdQueryVariables,
|
||||||
|
MessagesQuery,
|
||||||
|
MessagesQueryVariables,
|
||||||
SendMsgMutation,
|
SendMsgMutation,
|
||||||
SendMsgMutationVariables,
|
SendMsgMutationVariables,
|
||||||
ThreadObject,
|
ThreadObject,
|
||||||
|
UserMessageObject,
|
||||||
} from '../../graphql/types.support';
|
} from '../../graphql/types.support';
|
||||||
import { Box, Button, Input, TextArea, Text, Link } from '../../components';
|
import { Box, Button, Input, TextArea, Text } from '../../components';
|
||||||
import {
|
import { Send, ThreadClient, ThreadProductOwner } from '../../assets';
|
||||||
Attachment,
|
|
||||||
Send,
|
|
||||||
ThreadClient,
|
|
||||||
ThreadProductOwner,
|
|
||||||
} from '../../assets';
|
|
||||||
import {
|
import {
|
||||||
CONNECT_STREAM,
|
CONNECT_STREAM,
|
||||||
CREATE_THREAD,
|
CREATE_THREAD,
|
||||||
GET_THREAD_BY_ID,
|
GET_THREAD_BY_ID,
|
||||||
|
MESSAGES,
|
||||||
SEND_MSG,
|
SEND_MSG,
|
||||||
} from '../../graphql/chat.api.support';
|
} from '../../graphql/chat.api.support';
|
||||||
import { theme } from '../../themes';
|
import { theme } from '../../themes';
|
||||||
@@ -36,23 +35,17 @@ const SupportMessaging = () => {
|
|||||||
const { project, id } = useParams<{ id: string; project: string }>();
|
const { project, id } = useParams<{ id: string; project: string }>();
|
||||||
const role = useReactiveVar(roleVar);
|
const role = useReactiveVar(roleVar);
|
||||||
const currentUser = useReactiveVar(userVar);
|
const currentUser = useReactiveVar(userVar);
|
||||||
const location = useLocation();
|
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const [thread, setThread] = useState<ThreadObject>();
|
const [thread, setThread] = useState<ThreadObject>();
|
||||||
|
const [messages, setMessages] = useState<Array<UserMessageObject>>([]);
|
||||||
// const { data: liveMessages } = useSubscription<
|
const [addedMessages, setAddedMessages] = useState<Array<UserMessageObject>>(
|
||||||
// ConnectStreamSubscription,
|
[]
|
||||||
// ConnectStreamSubscriptionVariables
|
);
|
||||||
// >(CONNECT_STREAM, {
|
|
||||||
// variables: {
|
|
||||||
// mutationType: 'CREATED',
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async () => {
|
(async () => {
|
||||||
if (id) {
|
if (id) {
|
||||||
const res = await clientSupport.query<
|
const threadResult = await clientSupport.query<
|
||||||
GetThreadByIdQuery,
|
GetThreadByIdQuery,
|
||||||
GetThreadByIdQueryVariables
|
GetThreadByIdQueryVariables
|
||||||
>({
|
>({
|
||||||
@@ -61,7 +54,46 @@ const SupportMessaging = () => {
|
|||||||
threadId: id!,
|
threadId: id!,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
setThread(res?.data?.getThreadById);
|
|
||||||
|
setThread(threadResult?.data?.getThreadById);
|
||||||
|
|
||||||
|
const messagesResult = await clientSupport.query<
|
||||||
|
MessagesQuery,
|
||||||
|
MessagesQueryVariables
|
||||||
|
>({
|
||||||
|
query: MESSAGES,
|
||||||
|
variables: {
|
||||||
|
threadId: id!,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
setMessages(
|
||||||
|
Array.from(messagesResult?.data?.messages).map((message, index) => ({
|
||||||
|
text: message.text,
|
||||||
|
username: message.username,
|
||||||
|
id: index.toString(),
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
|
||||||
|
const messageSubscriber = clientSupport.subscribe<
|
||||||
|
ConnectStreamSubscription,
|
||||||
|
ConnectStreamSubscriptionVariables
|
||||||
|
>({
|
||||||
|
query: CONNECT_STREAM,
|
||||||
|
variables: {
|
||||||
|
mutationType: 'CREATED',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
messageSubscriber.subscribe(({ data }) => {
|
||||||
|
setAddedMessages((prevMessages) => [
|
||||||
|
...prevMessages,
|
||||||
|
{
|
||||||
|
id: messages.length.toString(),
|
||||||
|
username: data?.connectStream?.userMessage?.username!,
|
||||||
|
text: data?.connectStream?.userMessage?.text!,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
@@ -96,25 +128,20 @@ const SupportMessaging = () => {
|
|||||||
const sendMsgForm = useFormik({
|
const sendMsgForm = useFormik({
|
||||||
initialValues: {
|
initialValues: {
|
||||||
msg: '',
|
msg: '',
|
||||||
fileName: '',
|
|
||||||
fileSource: '',
|
|
||||||
},
|
},
|
||||||
validationSchema: Yup.object().shape({
|
validationSchema: Yup.object().shape({
|
||||||
msg: Yup.string().required('Message is required'),
|
msg: Yup.string().required('Message is required'),
|
||||||
}),
|
}),
|
||||||
onSubmit: async ({ msg, fileName, fileSource }) => {
|
onSubmit: async ({ msg }, { resetForm }) => {
|
||||||
await clientSupport.mutate<SendMsgMutation, SendMsgMutationVariables>({
|
await clientSupport.mutate<SendMsgMutation, SendMsgMutationVariables>({
|
||||||
mutation: SEND_MSG,
|
mutation: SEND_MSG,
|
||||||
variables: {
|
variables: {
|
||||||
threadId: id,
|
threadId: id,
|
||||||
username: `${currentUser?.firstName} ${currentUser?.lastName}`,
|
username: `${currentUser?.firstName} ${currentUser?.lastName}`,
|
||||||
msg,
|
msg,
|
||||||
file: {
|
|
||||||
name: fileName,
|
|
||||||
src: fileSource,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
resetForm();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -208,9 +235,11 @@ const SupportMessaging = () => {
|
|||||||
flexDirection='column'
|
flexDirection='column'
|
||||||
marginBottom='20px'
|
marginBottom='20px'
|
||||||
>
|
>
|
||||||
{thread.userMessages.map((msg) => (
|
{messages.map((msg) => (
|
||||||
<Box
|
<Box
|
||||||
borderRadius='10px'
|
borderRadius='10px'
|
||||||
|
boxShadow='1px 1px 10px rgba(50, 59, 105, 0.25)'
|
||||||
|
marginBottom='15px'
|
||||||
padding='10px'
|
padding='10px'
|
||||||
color={
|
color={
|
||||||
msg.username ===
|
msg.username ===
|
||||||
@@ -235,16 +264,37 @@ const SupportMessaging = () => {
|
|||||||
<Box marginBottom='5px'>
|
<Box marginBottom='5px'>
|
||||||
<Text variant='body'>{msg.text}</Text>
|
<Text variant='body'>{msg.text}</Text>
|
||||||
</Box>
|
</Box>
|
||||||
{msg.attachment.src && (
|
|
||||||
<Box display='flex' flexDirection='row' alignItems='center'>
|
|
||||||
<Attachment fill='white' />
|
|
||||||
<Box>
|
|
||||||
<Link url href={msg.attachment.src} target='_blank'>
|
|
||||||
{msg.attachment.name}
|
|
||||||
</Link>
|
|
||||||
</Box>
|
</Box>
|
||||||
|
))}
|
||||||
|
{addedMessages.map((msg) => (
|
||||||
|
<Box
|
||||||
|
borderRadius='10px'
|
||||||
|
boxShadow='1px 1px 10px rgba(50, 59, 105, 0.25)'
|
||||||
|
marginBottom='15px'
|
||||||
|
padding='10px'
|
||||||
|
color={
|
||||||
|
msg.username ===
|
||||||
|
`${currentUser?.firstName} ${currentUser?.lastName}`
|
||||||
|
? 'white'
|
||||||
|
: 'initial'
|
||||||
|
}
|
||||||
|
key={msg.id}
|
||||||
|
background={
|
||||||
|
msg.username ===
|
||||||
|
`${currentUser?.firstName} ${currentUser?.lastName}`
|
||||||
|
? theme.colors[role || 'client'].main
|
||||||
|
: 'white'
|
||||||
|
}
|
||||||
|
alignSelf={
|
||||||
|
msg.username ===
|
||||||
|
`${currentUser?.firstName} ${currentUser?.lastName}`
|
||||||
|
? 'flex-end'
|
||||||
|
: 'flex-start'
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Box marginBottom='5px'>
|
||||||
|
<Text variant='body'>{msg.text}</Text>
|
||||||
</Box>
|
</Box>
|
||||||
)}
|
|
||||||
</Box>
|
</Box>
|
||||||
))}
|
))}
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
Reference in New Issue
Block a user