Skip to content

Commit 71183d2

Browse files
author
Nathan Zylbersztejn
authored
Merge pull request #21 from mrbot-ai/feature/expose-functions
Feature/expose functions
2 parents 4d5e7f8 + 22e5d75 commit 71183d2

File tree

12 files changed

+129
-8
lines changed

12 files changed

+129
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ ratio while filling the element’s entire content box. If the
1616
object's aspect ratio does not match the aspect ratio of its box
1717
then the object will be clipped to fit. The clipping will be taken
1818
from the center.
19+
- `WebChat.toggle()`, `WebChat.open()`, and `WebChat.close` can be used to change the state of the chat box to open or closed.
20+
- `WebChat.show()` and `WebChat.hide()` can be used to show or hide the entire chat widget.
21+
- `WebChat.isOpen()` and `WebChat.isVisible()` can be used to get the open state of the chat box and the visibility state of the entire widget.
22+

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ About images: `width` and `height` define the size in pixels that images in mess
4646
It is recommended to use a particular version (i.e. "webchat-<version>.js") however the file "webchat-latest.js"
4747
is also available and is updated continuously with the latest version.
4848

49+
`WebChat.toggle()`, `WebChat.open()`, and `WebChat.close` can be used to change the state of the chat box to open or closed.
50+
51+
`WebChat.show()` and `WebChat.hide()` can be used to show or hide the entire chat widget.
52+
53+
`WebChat.isOpen()` and `WebChat.isVisible()` can be used to get the open state of the chat box and the visibility state of the entire widget.
54+
4955
#### As a React component
5056

5157
Install the package from GitHub by running:
@@ -64,7 +70,7 @@ function CustomWidget = () => {
6470
interval={2000}
6571
initPayload={"/get_started"}
6672
socketUrl={"http://localhost:5500"}
67-
socketPath={"/socket.io/"}
73+
socketPath={"/socket.io/"}
6874
title={"Title"}
6975
embedded={true}
7076
params={

dev/src/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
}
2525
})
2626
</script>
27-
2827
</body>
2928

3029
</html>

index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3-
import { Widget } from './index_for_react_app';
3+
import { Widget, toggleWidget, openWidget, closeWidget, showWidget, hideWidget, isOpen, isVisible } from './index_for_react_app';
44

55
const plugin = {
66
init: (args) => {
7+
78
ReactDOM.render(
89
<Widget
910
socketUrl={args.socketUrl}
@@ -20,13 +21,19 @@ const plugin = {
2021
badge={args.badge}
2122
params={args.params}
2223
/>, document.querySelector(args.selector)
23-
2424
);
2525
}
2626
};
2727

2828
export {
2929
plugin as default,
30-
Widget
30+
Widget,
31+
toggleWidget as toggle,
32+
openWidget as open,
33+
closeWidget as close,
34+
showWidget as show,
35+
hideWidget as hide,
36+
isOpen,
37+
isVisible
3138
};
3239

index_for_react_app.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import {
77
addImageSnippet,
88
addQuickReply,
99
renderCustomComponent,
10+
isOpen,
11+
isVisible,
12+
showWidget,
13+
hideWidget,
1014
toggleWidget,
15+
openWidget,
16+
closeWidget,
1117
toggleInputDisabled,
1218
dropMessages
1319
} from './src/store/actions/dispatcher';
@@ -21,7 +27,13 @@ export {
2127
addImageSnippet,
2228
addQuickReply,
2329
renderCustomComponent,
30+
isOpen,
31+
isVisible,
32+
showWidget,
33+
hideWidget,
2434
toggleWidget,
35+
openWidget,
36+
closeWidget,
2537
toggleInputDisabled,
2638
dropMessages
2739
};

src/components/Widget/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class Widget extends Component {
103103
profileAvatar={this.props.profileAvatar}
104104
showCloseButton={this.props.showCloseButton}
105105
fullScreenMode={this.props.fullScreenMode}
106+
showWidget={this.props.showWidget}
106107
showChat={this.props.showChat}
107108
badge={this.props.badge}
108109
embedded={this.props.embedded}
@@ -126,6 +127,7 @@ Widget.propTypes = {
126127
profileAvatar: PropTypes.string,
127128
showCloseButton: PropTypes.bool,
128129
fullScreenMode: PropTypes.bool,
130+
showWidget: PropTypes.bool,
129131
showChat: PropTypes.bool,
130132
badge: PropTypes.number,
131133
socket: PropTypes.shape({}),

src/components/Widget/layout.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const WidgetLayout = (props) => {
1616
);
1717

1818
return (
19+
props.showWidget ?
1920
<div className={classes.join(' ')}>
2021
{
2122
(props.fullScreenMode || props.showChat || props.embedded) &&
@@ -39,6 +40,7 @@ const WidgetLayout = (props) => {
3940
/>
4041
}
4142
</div>
43+
: null
4244
);
4345
};
4446

@@ -58,6 +60,7 @@ WidgetLayout.propTypes = {
5860
};
5961

6062
export default connect(store => ({
63+
showWidget: store.behavior.get('showWidget'),
6164
showChat: store.behavior.get('showChat'),
6265
disabledInput: store.behavior.get('disabledInput')
6366
}))(WidgetLayout);

src/store/actions/actionTypes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
export const INITIALIZE = 'INITIALIZE';
2+
export const GET_OPEN_STATE = 'GET_OPEN_STATE';
3+
export const GET_VISIBLE_STATE = 'GET_VISIBLE_STATE';
4+
export const SHOW_WIDGET = 'SHOW_WIDGET';
5+
export const HIDE_WIDGET = 'HIDE_WIDGET';
26
export const TOGGLE_CHAT = 'TOGGLE_CHAT';
7+
export const OPEN_CHAT = 'OPEN_CHAT';
8+
export const CLOSE_CHAT = 'CLOSE_CHAT';
39
export const TOGGLE_INPUT_DISABLED = 'TOGGLE_INPUT_DISABLED';
410
export const CHANGE_INPUT_FIELD_HINT = 'CHANGE_INOUT_FIELD_HINT';
511
export const ADD_NEW_USER_MESSAGE = 'ADD_NEW_USER_MESSAGE';

src/store/actions/dispatcher.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { store } from '../store';
22
import * as actions from './index';
33

4+
export function isOpen() {
5+
return store.dispatch(actions.getOpenState());
6+
}
7+
8+
export function isVisible() {
9+
return store.dispatch(actions.getVisibleState());
10+
}
11+
412
export function initialize() {
513
store.dispatch(actions.initialize());
614
}
@@ -45,10 +53,26 @@ export function renderCustomComponent(component, props, showAvatar = false) {
4553
store.dispatch(actions.renderCustomComponent(component, props, showAvatar));
4654
}
4755

56+
export function showWidget() {
57+
store.dispatch(actions.showWidget());
58+
}
59+
60+
export function hideWidget() {
61+
store.dispatch(actions.hideWidget());
62+
}
63+
4864
export function toggleWidget() {
4965
store.dispatch(actions.toggleChat());
5066
}
5167

68+
export function openWidget() {
69+
store.dispatch(actions.openChat());
70+
}
71+
72+
export function closeWidget() {
73+
store.dispatch(actions.closeChat());
74+
}
75+
5276
export function toggleInputDisabled() {
5377
store.dispatch(actions.toggleInputDisabled());
5478
}

src/store/actions/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,48 @@ export function initialize() {
66
};
77
}
88

9+
export function getOpenState() {
10+
return {
11+
type: actions.GET_OPEN_STATE
12+
};
13+
}
14+
15+
export function getVisibleState() {
16+
return {
17+
type: actions.GET_VISIBLE_STATE
18+
};
19+
}
20+
21+
export function showWidget() {
22+
return {
23+
type: actions.SHOW_WIDGET
24+
};
25+
}
26+
27+
export function hideWidget() {
28+
return {
29+
type: actions.HIDE_WIDGET
30+
};
31+
}
32+
933
export function toggleChat() {
1034
return {
1135
type: actions.TOGGLE_CHAT
1236
};
1337
}
1438

39+
export function openChat() {
40+
return {
41+
type: actions.OPEN_CHAT
42+
};
43+
}
44+
45+
export function closeChat() {
46+
return {
47+
type: actions.CLOSE_CHAT
48+
};
49+
}
50+
1551
export function toggleInputDisabled() {
1652
return {
1753
type: actions.TOGGLE_INPUT_DISABLED

0 commit comments

Comments
 (0)