Skip to content

Commit 9fab9e0

Browse files
author
Spencer Lepine
authored
customChatWidget: fix widget close/open icon state using ChatEvents (#145)
#144
1 parent ea7b56d commit 9fab9e0

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

customChatWidget/public/amazon-connect-chat-interface.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

customChatWidget/src/components/ChatButton/index.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ SPDX-License-Identifier: MIT-0 */
33

44
import React, { useState, useContext, useEffect } from 'react';
55
import { useAppConfig } from '../../providers/AppConfigProvider';
6-
import { device, closeChatSVGPath, loggerNames} from '../../constants';
6+
import { device, chatWithFormStates, closeChatSVGPath, loggerNames } from '../../constants';
77
import { Button, Svg } from './styled';
88
import { genLogger } from "../../lib/logger";
99

@@ -14,14 +14,31 @@ const { log } = genLogger(name);
1414
const ChatButton = (props) => {
1515
log(">>> Init");
1616
log(props);
17-
const { chatWithoutForm, forceUnmountChatWidget, setForceUnmountChatWidget, setWidgetIsOpen, widgetIsOpen } = props;
17+
const { chatWithoutForm, forceUnmountChatWidget, setForceUnmountChatWidget, setWidgetIsOpen, widgetIsOpen, currentState } = props;
1818
const { primaryColor } = useAppConfig();
1919
const handleChatIconClickEvent = (e) => {
2020
if (chatWithoutForm && forceUnmountChatWidget) setForceUnmountChatWidget(false)
2121

2222
setWidgetIsOpen(prev => !prev);
2323
}
2424

25+
//This useEffect will run only after currentState is changed to widget.
26+
useEffect(() => {
27+
if (currentState === chatWithFormStates.CHAT_WIDGET) {
28+
window.connect.ChatEvents &&
29+
window.connect.ChatEvents.onAgentEndChat(() => {
30+
log("Chat Ended hence toggling back to initial icon(chat)");
31+
handleChatIconClickEvent();
32+
});
33+
34+
window.connect.ChatEvents &&
35+
window.connect.ChatEvents.onChatEnded(() => {
36+
log("Chat Disconnected hence toggling back to initial icon(chat)");
37+
handleChatIconClickEvent();
38+
});
39+
}
40+
}, [currentState]);
41+
2542
return (
2643
<Button primaryColor={ primaryColor } onClick={ handleChatIconClickEvent } device={ device } widgetIsOpen={ widgetIsOpen }>
2744
{

customChatWidget/src/components/ChatIcon/index.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ SPDX-License-Identifier: MIT-0 */
44
import React, { useState, useContext, useEffect } from "react";
55
import anime from 'animejs';
66
import { useAppConfig } from '../../providers/AppConfigProvider';
7-
import { device, closeChatSVGPath, chatSVGPath, loggerNames } from '../../constants';
7+
import { device, closeChatSVGPath, chatSVGPath, loggerNames, chatWithFormStates } from '../../constants';
88
import { Button, Svg } from './styled';
99
import { genLogger } from "../../lib/logger";
1010

@@ -17,7 +17,7 @@ const ChatIcon = (props) =>
1717
log('ChatIcon.displayName: ', ChatIcon.displayName);
1818
log(props);
1919
const { primaryColor } = useAppConfig();
20-
const { chatWithoutForm, forceUnmountChatWidget, setForceUnmountChatWidget, setWidgetIsOpen, widgetIsOpen } = props;
20+
const { chatWithoutForm, forceUnmountChatWidget, setForceUnmountChatWidget, setWidgetIsOpen, widgetIsOpen, currentState } = props;
2121
const handleChatIconClickEvent = (e) => {
2222
if (chatWithoutForm && forceUnmountChatWidget) setForceUnmountChatWidget(false)
2323
const timeline = anime.timeline({
@@ -35,6 +35,7 @@ const ChatIcon = (props) =>
3535
});
3636
setWidgetIsOpen(!widgetIsOpen);
3737
}
38+
3839
// Toggle to initial Icon after the chat is ended by the chat Widget:
3940
const toggleToChatIcon = () => {
4041
const timeline = anime.timeline({
@@ -53,6 +54,22 @@ const ChatIcon = (props) =>
5354
})
5455
}
5556

57+
//This useEffect will run only after currentState is changed to widget.
58+
useEffect(() => {
59+
if (currentState === chatWithFormStates.CHAT_WIDGET) {
60+
window.connect.ChatEvents &&
61+
window.connect.ChatEvents.onAgentEndChat(() => {
62+
log("Chat Ended hence toggling back to initial icon(chat)");
63+
handleChatIconClickEvent();
64+
});
65+
66+
window.connect.ChatEvents &&
67+
window.connect.ChatEvents.onChatEnded(() => {
68+
log("Chat Disconnected hence toggling back to initial icon(chat)");
69+
handleChatIconClickEvent();
70+
});
71+
}
72+
}, [currentState]);
5673

5774
/*! Both chat and carrot SVG's are from Material Design Icons https://github.com/google/material-design-icons
5875
SPDX-License-Identifier: Apache-2.0 */

customChatWidget/src/views/ChatWithForm/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,20 @@ const ChatWithForm = () => {
2121
const [currentState, setCurrentState] = useState(chatWithFormStates.FORM);
2222
const [data, setData] = useState({});
2323
const { initiationIcon } = useAppConfig();
24+
2425
return (
2526
<Main device={device}>
2627
{
2728
initiationIcon.toLowerCase() === chatInitiationIcon.BUTTON ?
2829
<ChatButton
2930
widgetIsOpen={widgetIsOpen}
3031
setWidgetIsOpen={setWidgetIsOpen}
32+
currentState={currentState}
3133
/>
3234
: <ChatIcon
3335
widgetIsOpen={widgetIsOpen}
3436
setWidgetIsOpen={setWidgetIsOpen}
37+
currentState={currentState}
3538
/>
3639
}
3740
<div style={{ display: widgetIsOpen ? null : "none" }}>

customChatWidget/src/views/ChatWithoutForm/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ const ChatWithoutForm = () => {
2727
<ChatButton
2828
widgetIsOpen={widgetIsOpen}
2929
setWidgetIsOpen={setWidgetIsOpen}
30+
currentState={currentState}
3031
forceUnmountChatWidget={forceUnmountChatWidget}
3132
chatWithoutForm={true}
3233
setForceUnmountChatWidget={setForceUnmountChatWidget}
3334
/>
3435
: <ChatIcon
3536
widgetIsOpen={widgetIsOpen}
3637
setWidgetIsOpen={setWidgetIsOpen}
38+
currentState={currentState}
3739
forceUnmountChatWidget={forceUnmountChatWidget}
3840
chatWithoutForm={true}
3941
setForceUnmountChatWidget={setForceUnmountChatWidget}

0 commit comments

Comments
 (0)