From f8bb3307a97e318ec5a0163ea1db41b1bffdf12f Mon Sep 17 00:00:00 2001 From: knrt10 Date: Wed, 14 Aug 2019 11:31:59 +0530 Subject: [PATCH] Rebased code and fixed API --- src/components/App/index.js | 7 +- src/lib/main.js | 23 -- src/routes/Chat/container.js | 359 +-------------------------- src/routes/ChatFinished/component.js | 2 +- src/routes/Register/container.js | 2 +- 5 files changed, 7 insertions(+), 386 deletions(-) diff --git a/src/components/App/index.js b/src/components/App/index.js index 36d1d4bfa..0b3542103 100644 --- a/src/components/App/index.js +++ b/src/components/App/index.js @@ -2,7 +2,6 @@ import { Component } from 'preact'; import { Router, route } from 'preact-router'; import queryString from 'query-string'; -import { Livechat } from '../../api'; import { locationUpdate, userSessionPresence } from '../../lib/location'; import history from '../../history'; import Chat from '../../routes/Chat'; @@ -137,7 +136,8 @@ export class App extends Component { async initialize() { // TODO: split these behaviors into composable components - await Connection.init(); + await Connection.init(); + locationUpdate(); this.handleTriggers(); CustomFields.init(); Hooks.init(); @@ -150,7 +150,8 @@ export class App extends Component { async finalize() { CustomFields.reset(); - userPresence.reset(); + userPresence.reset(); + userSessionPresence.reset(); visibility.removeListener(this.handleVisibilityChange); I18n.off('change', this.handleLanguageChange); } diff --git a/src/lib/main.js b/src/lib/main.js index 0ae2858d9..0cb843a75 100644 --- a/src/lib/main.js +++ b/src/lib/main.js @@ -12,7 +12,6 @@ export const loadConfig = async () => { Livechat.credentials.token = token; -<<<<<<< HEAD const { agent, room, @@ -35,28 +34,6 @@ export const loadConfig = async () => { visible: true, unread: null, }); -======= - const { - agent, - room, - guest: user, - resources: { sound: src = null } = {}, - ...config - } = await Livechat.config({ token }); - - await store.setState({ - config, - agent, - room, - user, - sound: { src, enabled: true, play: false }, - messages: [], - typing: [], - noMoreMessages: false, - visible: true, - unread: null, - }); ->>>>>>> Rebased code }; export const getToken = () => { diff --git a/src/routes/Chat/container.js b/src/routes/Chat/container.js index a21428624..51524786f 100644 --- a/src/routes/Chat/container.js +++ b/src/routes/Chat/container.js @@ -12,7 +12,6 @@ import { initRoom, closeChat, loadMessages, loadMoreMessages, defaultRoomParams import { normalizeQueueAlert } from '../../lib/api'; export class ChatContainer extends Component { -<<<<<<< HEAD state = { connectingAgent: false, queueSpot: 0, @@ -105,10 +104,9 @@ export class ChatContainer extends Component { const { alerts, dispatch, token, user } = this.props; try { this.stopTypingDebounced.stop(); - store.setState({ userState: 'chatting' }); await Promise.all([ this.stopTyping({ rid, username: user.username }), - Livechat.changeUserState('chatting'), + Livechat.updateSessionStatus('online', token), Livechat.sendMessage({ msg, token, rid }), ]); } catch (error) { @@ -180,8 +178,6 @@ export class ChatContainer extends Component { await dispatch({ alerts: (alerts.push(alert), alerts) }); } finally { await dispatch({ loading: false }); - await store.setState({ userState: 'offline' }); - await Livechat.changeUserState('offline'); await closeChat(); } } @@ -316,276 +312,10 @@ export class ChatContainer extends Component { onSoundStop={this.handleSoundStop} /> ) -======= - state = { - connectingAgent: { value: false }, - } - - checkConnecting = () => { - const { connecting } = this.props; - if (connecting !== this.state.connectingAgent) { - this.state.connectingAgent = connecting; - this.handleConnectingAgentAlert(connecting); - } - } - - grantUser = async () => { - const { token, user, guest } = this.props; - - if (user) { - return user; - } - - const visitor = { token, ...guest }; - await Livechat.grantVisitor({ visitor }); - await loadConfig(); - } - - getRoom = async () => { - const { alerts, dispatch, room } = this.props; - - if (room) { - return room; - } - - await dispatch({ loading: true }); - try { - const params = defaultRoomParams(); - const newRoom = await Livechat.room(params); - await dispatch({ room: newRoom, messages: [], noMoreMessages: false }); - await initRoom(); - return newRoom; - } catch (error) { - const { data: { error: reason } } = error; - const alert = { id: createToken(), children: I18n.t('Error starting a new conversation: %{reason}', { reason }), error: true, timeout: 10000 }; - await dispatch({ loading: false, alerts: (alerts.push(alert), alerts) }); - throw error; - } finally { - await dispatch({ loading: false }); - } - } - - handleTop = () => { - loadMoreMessages(); - } - - startTyping = throttle(async ({ rid, username }) => { - await Livechat.notifyVisitorTyping(rid, username, true); - this.stopTypingDebounced({ rid, username }); - }, 4500) - - stopTyping = ({ rid, username }) => Livechat.notifyVisitorTyping(rid, username, false) - - stopTypingDebounced = debounce(this.stopTyping, 5000) - - handleChangeText = async () => { - const { user, room } = this.props; - if (!(user && user.username && room && room._id)) { - return; - } - - this.startTyping({ rid: room._id, username: user.username }); - } - - handleSubmit = async (msg) => { - if (msg.trim() === '') { - return; - } - - await this.grantUser(); - const { _id: rid } = await this.getRoom(); - const { alerts, dispatch, token, user } = this.props; - try { - this.stopTypingDebounced.stop(); - await Promise.all([ - this.stopTyping({ rid, username: user.username }), - Livechat.updateSessionStatus('online', token), - Livechat.sendMessage({ msg, token, rid }), - ]); - } catch (error) { - const { data: { error: reason } } = error; - const alert = { id: createToken(), children: reason, error: true, timeout: 5000 }; - await dispatch({ alerts: (alerts.push(alert), alerts) }); - } - await Livechat.notifyVisitorTyping(rid, user.username, false); - } - - doFileUpload = async (rid, file) => { - const { alerts, dispatch } = this.props; - - try { - await Livechat.uploadFile({ rid, file }); - } catch (error) { - const { data: { reason, sizeAllowed } } = error; - - let message = I18n.t('FileUpload Error'); - switch (reason) { - case 'error-type-not-allowed': - message = I18n.t('Media Types Not Accepted.'); - break; - case 'error-size-not-allowed': - message = I18n.t('File exceeds allowed size of %{size}.', { size: sizeAllowed }); - } - - const alert = { id: createToken(), children: message, error: true, timeout: 5000 }; - await dispatch({ alerts: (alerts.push(alert), alerts) }); - } - }; - - handleUpload = async (files) => { - await this.grantUser(); - const { _id: rid } = await this.getRoom(); - - files.forEach((file) => this.doFileUpload(rid, file)); - } - - handleSoundStop = async () => { - const { dispatch, sound = {} } = this.props; - await dispatch({ sound: { ...sound, play: false } }); - } - - onChangeDepartment = () => { - route('/switch-department'); - } - - onFinishChat = async () => { - const { success } = await ModalManager.confirm({ - text: I18n.t('Are you sure you want to finish this chat?'), - }); - - if (!success) { - return; - } - - const { alerts, dispatch, room: { _id: rid } = {} } = this.props; - - await dispatch({ loading: true }); - try { - if (rid) { - await Livechat.closeChat({ rid }); - } - } catch (error) { - console.error(error); - const alert = { id: createToken(), children: I18n.t('Error closing chat.'), error: true, timeout: 0 }; - await dispatch({ alerts: (alerts.push(alert), alerts) }); - } finally { - await dispatch({ loading: false }); - await closeChat(); - } - } - - onRemoveUserData = async () => { - const { success } = await ModalManager.confirm({ - text: I18n.t('Are you sure you want to remove all of your personal data?'), - }); - - if (!success) { - return; - } - - const { alerts, dispatch } = this.props; - - await dispatch({ loading: true }); - try { - await Livechat.deleteVisitor(); - } catch (error) { - console.error(error); - const alert = { id: createToken(), children: I18n.t('Error removing user data.'), error: true, timeout: 0 }; - await dispatch({ alerts: (alerts.push(alert), alerts) }); - } finally { - await loadConfig(); - await dispatch({ loading: false }); - route('/chat-finished'); - } - } - - canSwitchDepartment = () => { - const { allowSwitchingDepartments, departments = {} } = this.props; - return allowSwitchingDepartments && departments.filter((dept) => dept.showOnRegistration).length > 1; - } - - canFinishChat = () => { - const { room, connecting } = this.props; - return (room !== undefined) || connecting; - } - - canRemoveUserData = () => { - const { allowRemoveUserData } = this.props; - return allowRemoveUserData; - } - - showOptionsMenu = () => - this.canSwitchDepartment() || this.canFinishChat() || this.canRemoveUserData() - - - async handleConnectingAgentAlert(connecting) { - const { alerts: oldAlerts, dispatch } = this.props; - - const alerts = oldAlerts.filter((item) => item.id !== constants.connectingAgentAlertId); - if (connecting) { - alerts.push({ - id: constants.connectingAgentAlertId, - children: I18n.t('Please, wait for the next available agent..'), - warning: true, - hideCloseButton: true, - timeout: 0, - }); - } - - await dispatch({ alerts }); - } - - componentDidMount() { - this.checkConnecting(); - loadMessages(); - } - - async componentWillReceiveProps({ messages: nextMessages, visible: nextVisible, minimized: nextMinimized }) { - const { messages, alerts, dispatch } = this.props; - - if (nextMessages && messages && nextMessages.length !== messages.length && nextVisible && !nextMinimized) { - const nextLastMessage = nextMessages[nextMessages.length - 1]; - const lastMessage = messages[messages.length - 1]; - if ( - (nextLastMessage && lastMessage && nextLastMessage._id !== lastMessage._id) - || (nextMessages.length === 1 && messages.length === 0) - ) { - const newAlerts = alerts.filter((item) => item.id !== constants.unreadMessagesAlertId); - await dispatch({ alerts: newAlerts, unread: null, lastReadMessageId: nextLastMessage._id }); - } - } - } - - componentDidUpdate() { - this.checkConnecting(); - } - - componentWillUnmount() { - this.handleConnectingAgentAlert(false); - } - - render = ({ user, ...props }) => ( - - ) ->>>>>>> Rebased code } export const ChatConnector = ({ ref, ...props }) => ( -<<<<<<< HEAD {({ config: { @@ -678,93 +408,6 @@ export const ChatConnector = ({ ref, ...props }) => ( /> )} -======= - - {({ - config: { - settings: { - fileUpload: uploads, - allowSwitchingDepartments, - forceAcceptDataProcessingConsent: allowRemoveUserData, - showConnecting, - } = {}, - messages: { - conversationFinishedMessage, - } = {}, - theme: { - color, - title, - } = {}, - departments = {}, - }, - iframe: { - theme: { - color: customColor, - fontColor: customFontColor, - iconColor: customIconColor, - } = {}, - guest, - } = {}, - token, - agent, - sound, - user, - room, - messages, - noMoreMessages, - typing, - loading, - dispatch, - alerts, - visible, - unread, - lastReadMessageId, - triggerAgent, - }) => ( - canRenderMessage(message))} - noMoreMessages={noMoreMessages} - emoji={false} - uploads={uploads} - typingUsernames={Array.isArray(typing) ? typing : []} - loading={loading} - showConnecting={showConnecting} // setting from server that tells if app needs to show "connecting" sometimes - connecting={!!(room && !agent && showConnecting)} - dispatch={dispatch} - departments={departments} - allowSwitchingDepartments={allowSwitchingDepartments} - conversationFinishedMessage={conversationFinishedMessage || I18n.t('Conversation finished')} - allowRemoveUserData={allowRemoveUserData} - alerts={alerts} - visible={visible} - unread={unread} - lastReadMessageId={lastReadMessageId} - guest={guest} - triggerAgent={triggerAgent} - /> - )} - ->>>>>>> Rebased code ); diff --git a/src/routes/ChatFinished/component.js b/src/routes/ChatFinished/component.js index 4b4d9cd66..c1455a816 100644 --- a/src/routes/ChatFinished/component.js +++ b/src/routes/ChatFinished/component.js @@ -9,7 +9,7 @@ const defaultGreeting = I18n.t('Thanks for talking with us'); const defaultMessage = I18n.t('If you have any other questions, just press the button below to start a new chat.'); export default class ChatFinished extends Component { - handleClick = async() => { + handleClick = () => { const { onRedirectChat } = this.props; onRedirectChat && onRedirectChat(); } diff --git a/src/routes/Register/container.js b/src/routes/Register/container.js index 23c093cb8..21d1049cf 100644 --- a/src/routes/Register/container.js +++ b/src/routes/Register/container.js @@ -4,7 +4,7 @@ import { route } from 'preact-router'; import { Livechat } from '../../api'; import { parentCall } from '../../lib/parentCall'; import { loadConfig } from '../../lib/main'; -import { Consumer, store } from '../../store'; +import { Consumer } from '../../store'; import { ModalManager } from '../../components/Modal'; import Register from './component';