Skip to content

Commit

Permalink
customChatWidget: fix widget close/open icon state using ChatEvents (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Spencer Lepine authored Oct 6, 2023
1 parent ea7b56d commit 9fab9e0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion customChatWidget/public/amazon-connect-chat-interface.js

Large diffs are not rendered by default.

21 changes: 19 additions & 2 deletions customChatWidget/src/components/ChatButton/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SPDX-License-Identifier: MIT-0 */

import React, { useState, useContext, useEffect } from 'react';
import { useAppConfig } from '../../providers/AppConfigProvider';
import { device, closeChatSVGPath, loggerNames} from '../../constants';
import { device, chatWithFormStates, closeChatSVGPath, loggerNames } from '../../constants';
import { Button, Svg } from './styled';
import { genLogger } from "../../lib/logger";

Expand All @@ -14,14 +14,31 @@ const { log } = genLogger(name);
const ChatButton = (props) => {
log(">>> Init");
log(props);
const { chatWithoutForm, forceUnmountChatWidget, setForceUnmountChatWidget, setWidgetIsOpen, widgetIsOpen } = props;
const { chatWithoutForm, forceUnmountChatWidget, setForceUnmountChatWidget, setWidgetIsOpen, widgetIsOpen, currentState } = props;
const { primaryColor } = useAppConfig();
const handleChatIconClickEvent = (e) => {
if (chatWithoutForm && forceUnmountChatWidget) setForceUnmountChatWidget(false)

setWidgetIsOpen(prev => !prev);
}

//This useEffect will run only after currentState is changed to widget.
useEffect(() => {
if (currentState === chatWithFormStates.CHAT_WIDGET) {
window.connect.ChatEvents &&
window.connect.ChatEvents.onAgentEndChat(() => {
log("Chat Ended hence toggling back to initial icon(chat)");
handleChatIconClickEvent();
});

window.connect.ChatEvents &&
window.connect.ChatEvents.onChatEnded(() => {
log("Chat Disconnected hence toggling back to initial icon(chat)");
handleChatIconClickEvent();
});
}
}, [currentState]);

return (
<Button primaryColor={ primaryColor } onClick={ handleChatIconClickEvent } device={ device } widgetIsOpen={ widgetIsOpen }>
{
Expand Down
21 changes: 19 additions & 2 deletions customChatWidget/src/components/ChatIcon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SPDX-License-Identifier: MIT-0 */
import React, { useState, useContext, useEffect } from "react";
import anime from 'animejs';
import { useAppConfig } from '../../providers/AppConfigProvider';
import { device, closeChatSVGPath, chatSVGPath, loggerNames } from '../../constants';
import { device, closeChatSVGPath, chatSVGPath, loggerNames, chatWithFormStates } from '../../constants';
import { Button, Svg } from './styled';
import { genLogger } from "../../lib/logger";

Expand All @@ -17,7 +17,7 @@ const ChatIcon = (props) =>
log('ChatIcon.displayName: ', ChatIcon.displayName);
log(props);
const { primaryColor } = useAppConfig();
const { chatWithoutForm, forceUnmountChatWidget, setForceUnmountChatWidget, setWidgetIsOpen, widgetIsOpen } = props;
const { chatWithoutForm, forceUnmountChatWidget, setForceUnmountChatWidget, setWidgetIsOpen, widgetIsOpen, currentState } = props;
const handleChatIconClickEvent = (e) => {
if (chatWithoutForm && forceUnmountChatWidget) setForceUnmountChatWidget(false)
const timeline = anime.timeline({
Expand All @@ -35,6 +35,7 @@ const ChatIcon = (props) =>
});
setWidgetIsOpen(!widgetIsOpen);
}

// Toggle to initial Icon after the chat is ended by the chat Widget:
const toggleToChatIcon = () => {
const timeline = anime.timeline({
Expand All @@ -53,6 +54,22 @@ const ChatIcon = (props) =>
})
}

//This useEffect will run only after currentState is changed to widget.
useEffect(() => {
if (currentState === chatWithFormStates.CHAT_WIDGET) {
window.connect.ChatEvents &&
window.connect.ChatEvents.onAgentEndChat(() => {
log("Chat Ended hence toggling back to initial icon(chat)");
handleChatIconClickEvent();
});

window.connect.ChatEvents &&
window.connect.ChatEvents.onChatEnded(() => {
log("Chat Disconnected hence toggling back to initial icon(chat)");
handleChatIconClickEvent();
});
}
}, [currentState]);

/*! Both chat and carrot SVG's are from Material Design Icons https://github.com/google/material-design-icons
SPDX-License-Identifier: Apache-2.0 */
Expand Down
3 changes: 3 additions & 0 deletions customChatWidget/src/views/ChatWithForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ const ChatWithForm = () => {
const [currentState, setCurrentState] = useState(chatWithFormStates.FORM);
const [data, setData] = useState({});
const { initiationIcon } = useAppConfig();

return (
<Main device={device}>
{
initiationIcon.toLowerCase() === chatInitiationIcon.BUTTON ?
<ChatButton
widgetIsOpen={widgetIsOpen}
setWidgetIsOpen={setWidgetIsOpen}
currentState={currentState}
/>
: <ChatIcon
widgetIsOpen={widgetIsOpen}
setWidgetIsOpen={setWidgetIsOpen}
currentState={currentState}
/>
}
<div style={{ display: widgetIsOpen ? null : "none" }}>
Expand Down
2 changes: 2 additions & 0 deletions customChatWidget/src/views/ChatWithoutForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ const ChatWithoutForm = () => {
<ChatButton
widgetIsOpen={widgetIsOpen}
setWidgetIsOpen={setWidgetIsOpen}
currentState={currentState}
forceUnmountChatWidget={forceUnmountChatWidget}
chatWithoutForm={true}
setForceUnmountChatWidget={setForceUnmountChatWidget}
/>
: <ChatIcon
widgetIsOpen={widgetIsOpen}
setWidgetIsOpen={setWidgetIsOpen}
currentState={currentState}
forceUnmountChatWidget={forceUnmountChatWidget}
chatWithoutForm={true}
setForceUnmountChatWidget={setForceUnmountChatWidget}
Expand Down

0 comments on commit 9fab9e0

Please sign in to comment.