Skip to content

Commit

Permalink
microsoft#2794: Fix change-locale sample
Browse files Browse the repository at this point in the history
  • Loading branch information
corinagum committed Jan 5, 2020
1 parent c2f6233 commit 1f25798
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Here is the finished `index.html`:
</head>
<body>
<div id="webchat" role="main"></div>
<script>
<script type="text/babel">
(async function() {
const res = await fetch('https://webchat-mockbot.azurewebsites.net/directline/token', { method: 'POST' });
const { token } = await res.json();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Web Chat: Integrate with React</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!--
For simplicity and code clarity, we are using Babel and React from unpkg.com.
-->
<title>Web Chat: Change locale</title>

<script src="https://unpkg.com/@babel/standalone@7.7.5/babel.min.js"></script>
<script src="https://unpkg.com/react@16.8.6/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.development.js"></script>
<!--
This CDN points to the latest official release of Web Chat. If you need to test against Web Chat's latest bits, please refer to pointing to Web Chat's MyGet feed:
https://github.com/microsoft/BotFramework-WebChat#how-to-test-with-web-chats-latest-bits
-->
<script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<!-- <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script> -->
<script src="/packages/bundle/dist/webchat.js"></script>
<style>
html,
body {
Expand Down Expand Up @@ -42,12 +40,43 @@

const res = await fetch('https://webchat-mockbot.azurewebsites.net/directline/token', { method: 'POST' });
const { token } = await res.json();
const { ReactWebChat } = window.WebChat;

window.ReactDOM.render(
<ReactWebChat directLine={window.WebChat.createDirectLine({ token })} />,
document.getElementById('webchat')
);
const { useMemo, useState } = window.React;
const { createDirectLine, createStore, ReactWebChat } = window.WebChat;

const App = () => {
const [locale, setLocale] = useState(navigator.language);
const directLine = useMemo(() => createDirectLine({ token }), []);
const store = useMemo(
() =>
createStore({}, () => next => action => {
if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
const {
activity: {
from: { role },
text,
type
}
} = action.payload;

if (
role === 'bot' &&
type === 'message' &&
(text === 'en-US' || text === 'ja-JP' || text === 'zh-HK')
) {
setLocale(text);
}
}

return next(action);
}),
[]
);

return <ReactWebChat directLine={directLine} locale={locale} store={store} />;
};

window.ReactDOM.render(<App />, document.getElementById('webchat'));

document.querySelector('#webchat > *').focus();
})().catch(err => console.error(err));
Expand Down

0 comments on commit 1f25798

Please sign in to comment.