From e10681f13b4ca77a2e11be33390513e41f97e6f4 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Thu, 17 Jun 2021 03:27:55 +0100 Subject: [PATCH] Add support messaging page --- src/pages/SupportMessaging/index.tsx | 288 +++++++++++++++++++++++++++ src/pages/SupportMessaging/styles.ts | 7 + 2 files changed, 295 insertions(+) create mode 100644 src/pages/SupportMessaging/index.tsx create mode 100644 src/pages/SupportMessaging/styles.ts diff --git a/src/pages/SupportMessaging/index.tsx b/src/pages/SupportMessaging/index.tsx new file mode 100644 index 0000000..ce2f31c --- /dev/null +++ b/src/pages/SupportMessaging/index.tsx @@ -0,0 +1,288 @@ +import * as Yup from 'yup'; +import { useFormik } from 'formik'; +import { useLocation, useParams, useHistory } from 'react-router-dom'; +import { useReactiveVar, useSubscription } from '@apollo/client'; +import { useState, useEffect } from 'react'; +import { roleVar, userVar } from '../../graphql/state'; +import { Wrapper } from './styles'; +import { + ConnectStreamSubscription, + ConnectStreamSubscriptionVariables, + CreateThreadMutation, + CreateThreadMutationVariables, + GetThreadByIdQuery, + GetThreadByIdQueryVariables, + SendMsgMutation, + SendMsgMutationVariables, + ThreadObject, +} from '../../graphql/types.support'; +import { Box, Button, Input, TextArea, Text, Link } from '../../components'; +import { + Attachment, + Send, + ThreadClient, + ThreadProductOwner, +} from '../../assets'; +import { + CONNECT_STREAM, + CREATE_THREAD, + GET_THREAD_BY_ID, + SEND_MSG, +} from '../../graphql/chat.api.support'; +import { theme } from '../../themes'; +import { clientSupport } from '../..'; + +const SupportMessaging = () => { + const { project, id } = useParams<{ id: string; project: string }>(); + const role = useReactiveVar(roleVar); + const currentUser = useReactiveVar(userVar); + const location = useLocation(); + const history = useHistory(); + const [thread, setThread] = useState(); + + // const { data: liveMessages } = useSubscription< + // ConnectStreamSubscription, + // ConnectStreamSubscriptionVariables + // >(CONNECT_STREAM, { + // variables: { + // mutationType: 'CREATED', + // }, + // }); + + useEffect(() => { + (async () => { + if (id) { + const res = await clientSupport.query< + GetThreadByIdQuery, + GetThreadByIdQueryVariables + >({ + query: GET_THREAD_BY_ID, + variables: { + threadId: id!, + }, + }); + setThread(res?.data?.getThreadById); + } + })(); + + // eslint-disable-next-line + }, [id]); + + const createThreadForm = useFormik({ + initialValues: { + title: '', + description: '', + }, + validationSchema: Yup.object().shape({ + title: Yup.string().required('Title is required'), + description: Yup.string().required('Description is required'), + }), + onSubmit: async ({ title, description }) => { + const createdThread = await clientSupport.mutate< + CreateThreadMutation, + CreateThreadMutationVariables + >({ + mutation: CREATE_THREAD, + variables: { + projectId: project, + title, + threadDescription: description, + }, + }); + history.push(`/support-messaging/${project}/${createdThread}`); + }, + }); + + const sendMsgForm = useFormik({ + initialValues: { + msg: '', + fileName: '', + fileSource: '', + }, + validationSchema: Yup.object().shape({ + msg: Yup.string().required('Message is required'), + }), + onSubmit: async ({ msg, fileName, fileSource }) => { + await clientSupport.mutate({ + mutation: SEND_MSG, + variables: { + threadId: id, + username: `${currentUser?.firstName} ${currentUser?.lastName}`, + msg, + file: { + name: fileName, + src: fileSource, + }, + }, + }); + }, + }); + + return ( + + + {!thread ? ( + <> + + {role === 'client' ? : } + +
+ + +