Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import {ChatWidget, ChatWindow, Papercups} from '@papercups-io/chat-widget';

// NB: during development, replace this with a valid account ID from your dev db
const TEST_ACCOUNT_ID = '2ebbad4c-b162-4ed2-aff5-eaf9ebf469a5';
const TEST_ACCOUNT_ID = 'b6d3ac2d-fe0b-425b-93f6-327fd67848df';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it is to be merged, I'll revert this


type Props = {disco?: boolean; displayChatWindow?: boolean};

Expand Down Expand Up @@ -162,6 +162,11 @@ const App = ({disco, displayChatWindow}: Props) => {
<button onClick={Papercups.open}>Open</button>
<button onClick={Papercups.close}>Close</button>
<button onClick={Papercups.toggle}>Toggle</button>
<button
onClick={() => Papercups.sendBotMessage({message: 'a', signature: 'b'})}
>
sendMsg
</button>
</>
);
};
Expand Down
5 changes: 4 additions & 1 deletion example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));
ReactDOM.render(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it is to be merged, I'll revert this

<App displayChatWindow={true} />,
document.getElementById('root')
);
8 changes: 8 additions & 0 deletions src/components/ChatWidgetContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class ChatWidgetContainer extends React.Component<Props, State> {
logger: Logger;

EVENTS = [
'papercups:send_bot_message',
'papercups:open',
'papercups:close',
'papercups:toggle',
Expand Down Expand Up @@ -383,6 +384,8 @@ class ChatWidgetContainer extends React.Component<Props, State> {
const {type, detail} = event;

switch (type) {
case 'papercups:send_bot_message':
return this.handleSendBotMessage(detail);
case 'papercups:open':
return this.handleOpenWidget();
case 'papercups:close':
Expand All @@ -396,6 +399,11 @@ class ChatWidgetContainer extends React.Component<Props, State> {
}
};

handleSendBotMessage = (detail: any) => {
this.handleOpenWidget();
this.send('papercups:send_bot_message', detail);
};

postMessageHandlers = (msg: any) => {
this.logger.debug('Handling in parent:', msg.data);
const iframeUrl = this.getIframeUrl();
Expand Down
14 changes: 14 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,24 @@ export const identify = () => {
console.warn('`Papercups.identify` has not been implemented yet!');
};

export const sendBotMessage = ({
message,
signature,
}: {
message: string;
signature: string;
}) =>
window.dispatchEvent(
new CustomEvent('papercups:send_bot_message', {
detail: {message, signature},
})
);

export const Papercups = {
open,
close,
toggle,
sendBotMessage,
};

export {ChatWidget, ChatWindow};
Expand Down