Skip to content

Commit aa28a03

Browse files
committed
add optional isAnonymous parameter, move mock logged in to earlier in index.html example
1 parent fe6db9e commit aa28a03

File tree

9 files changed

+75
-38
lines changed

9 files changed

+75
-38
lines changed

build/static/js/453.chunk.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/static/js/453.chunk.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/static/js/main.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/static/js/main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>ACCESS Q&A Bot Standalone Demo</title>
77
<link rel="stylesheet" href="./build/static/css/main.css">
8+
<!-- Load scripts in head for earlier initialization -->
9+
<script>
10+
// Determine if user is logged in by checking for a class on the body
11+
document.addEventListener('DOMContentLoaded', function() {
12+
window.isAnonymous = !document.querySelector('body').classList.contains('user-logged-in');
13+
});
14+
</script>
15+
<!-- Preload JavaScript bundles -->
16+
<script src="./build/static/js/main.js"></script>
17+
<script src="./build/static/js/453.chunk.js"></script>
818
<style>
919
body {
1020
font-family: Arial, sans-serif;
@@ -61,43 +71,62 @@ <h2>Method 3: Explicitly Call JavaScript Function</h2>
6171
</div>
6272

6373
<script>
64-
// Determine if user is logged in by checking for a class on the body
65-
function isAnonymous() {
66-
return !document.querySelector('body').classList.contains('user-logged-in');
67-
}
68-
window.isAnonymous = isAnonymous();
69-
</script>
74+
// Initialize all methods once DOM is fully loaded and scripts are available
75+
document.addEventListener('DOMContentLoaded', function() {
76+
// Ensure window.qAndATool is available before proceeding
77+
function initializeQABots() {
78+
if (window.qAndATool) {
79+
// Method 1: Initialize the root element if not already done
80+
const qaBot = document.getElementById('qa-bot');
81+
if (qaBot && !qaBot.hasChildNodes()) {
82+
console.log("Initializing Method 1 - Root QA Bot");
83+
window.qAndATool({
84+
target: qaBot,
85+
isLoggedIn: !window.isAnonymous,
86+
isAnonymous: window.isAnonymous
87+
});
88+
}
7089

71-
<!--
72-
Load the main JavaScript bundle which includes:
73-
1. The React components that power the Q&A tool
74-
2. The globally exposed qAndATool function (from src/index.js)
90+
// Method 2: Initialize embedded-qa-bot elements
91+
const embeddedBots = document.querySelectorAll('.embedded-qa-bot');
92+
embeddedBots.forEach(function(bot) {
93+
if (!bot.hasChildNodes()) {
94+
console.log("Initializing Method 2 - Embedded QA Bot");
95+
window.qAndATool({
96+
target: bot,
97+
embedded: true,
98+
isOpen: true,
99+
welcome: bot.getAttribute('data-welcome') || "Hello!",
100+
prompt: bot.getAttribute('data-prompt') || "Ask me a question...",
101+
isLoggedIn: !window.isAnonymous,
102+
isAnonymous: window.isAnonymous
103+
});
104+
}
105+
});
75106

76-
Note: All three integration methods depend on window.qAndATool
77-
being exposed by the JavaScript bundle
78-
-->
79-
<script src="./build/static/js/main.js"></script>
80-
<script src="./build/static/js/453.chunk.js"></script>
107+
// Method 3: Initialize the custom-qa-bot element
108+
console.log("Initializing Method 3 - Custom QA Bot");
109+
window.qAndATool({
110+
target: document.getElementById('custom-qa-bot'),
111+
embedded: true,
112+
welcome: "This is configured using JavaScript!",
113+
prompt: "Try asking a question about ACCESS...",
114+
isLoggedIn: !window.isAnonymous,
115+
isAnonymous: window.isAnonymous,
116+
disabled: window.isAnonymous,
117+
isOpen: true
118+
});
119+
} else {
120+
console.error("qAndATool function not found. Make sure the JS files are loaded properly.");
121+
}
122+
}
81123

82-
<script>
83-
// Wait for window.qAndATool to be available
84-
window.addEventListener('load', function() {
124+
// Try to initialize immediately
85125
if (window.qAndATool) {
86-
// JavaScript function example
87-
window.qAndATool({
88-
target: document.getElementById('custom-qa-bot'),
89-
embedded: true,
90-
welcome: "This is configured using JavaScript!",
91-
prompt: "Try asking a question about ACCESS...",
92-
isLoggedIn: !window.isAnonymous,
93-
disabled: window.isAnonymous,
94-
isOpen: true
95-
});
96-
97-
// Note: Method 1 uses the div with id="qa-bot"
98-
// and Method 2 is handled by the class-based selector
126+
initializeQABots();
99127
} else {
100-
console.error("qAndATool function not found. Make sure the JS files are loaded properly.");
128+
// Or wait a bit for scripts to load if needed
129+
setTimeout(initializeQABots, 500);
101130
}
102131
});
103132
</script>

src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import QABot from './components/QABot';
1010
* - welcome: string
1111
* - prompt: string
1212
* - isLoggedIn: boolean
13+
* - isAnonymous: boolean
1314
* - disabled: boolean
1415
* - isOpen: boolean
1516
* - onClose: function
@@ -21,6 +22,7 @@ function App(props) {
2122
welcome={props.welcome}
2223
prompt={props.prompt}
2324
isLoggedIn={props.isLoggedIn}
25+
isAnonymous={props.isAnonymous}
2426
disabled={props.disabled}
2527
isOpen={props.isOpen}
2628
onClose={props.onClose}

src/components/QABot.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ const QABot = (props) => {
1111
const containerRef = useRef(null);
1212

1313
const isLoggedIn = props.isLoggedIn !== undefined ? props.isLoggedIn : false;
14+
const isAnonymous = props.isAnonymous !== undefined ? props.isAnonymous : !isLoggedIn;
1415
// Derive disabled state, respecting explicit disabled prop if provided
15-
const disabled = props.disabled !== undefined ? props.disabled : !isLoggedIn;
16+
const disabled = props.disabled !== undefined ? props.disabled : isAnonymous;
1617

1718
// Use isOpen prop with default to false if not provided
1819
const isOpen = props.isOpen !== undefined ? props.isOpen : false;

src/lib.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export { WebComponentQABot };
1515
// Function-based API that prioritizes web component usage
1616
// and then attempts a react render (since some people may use the function within a react context)
1717
export function qAndATool(config) {
18-
const { target, version, isLoggedIn, isOpen, ...otherProps } = config;
18+
const { target, version, isLoggedIn, isAnonymous, isOpen, ...otherProps } = config;
1919

2020
if (!target || !(target instanceof HTMLElement)) {
2121
console.error('QA Bot: A valid target DOM element is required');
@@ -28,6 +28,7 @@ export function qAndATool(config) {
2828
return webComponentQAndATool({
2929
target,
3030
isLoggedIn,
31+
isAnonymous,
3132
isOpen,
3233
...otherProps
3334
});
@@ -38,6 +39,7 @@ export function qAndATool(config) {
3839
<React.StrictMode>
3940
<App
4041
isLoggedIn={isLoggedIn}
42+
isAnonymous={isAnonymous}
4143
isOpen={isOpen}
4244
{...otherProps}
4345
/>

src/web-component.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class AccessQABot extends HTMLElement {
122122
'prompt',
123123
'embedded',
124124
'is-logged-in',
125+
'is-anonymous',
125126
'disabled',
126127
'is-open',
127128
'api-key'
@@ -142,6 +143,7 @@ class AccessQABot extends HTMLElement {
142143
prompt: this.getAttribute('prompt'),
143144
embedded: this.hasAttribute('embedded'),
144145
isLoggedIn: this.hasAttribute('is-logged-in'),
146+
isAnonymous: this.hasAttribute('is-anonymous'),
145147
disabled: this.hasAttribute('disabled'),
146148
isOpen: this.hasAttribute('is-open'),
147149
// Always provide an apiKey to prevent process.env reference errors
@@ -232,6 +234,7 @@ export function webComponentQAndATool(config) {
232234
if (props.prompt) qaBot.setAttribute('prompt', props.prompt);
233235
if (props.embedded) qaBot.setAttribute('embedded', '');
234236
if (props.isLoggedIn) qaBot.setAttribute('is-logged-in', '');
237+
if (props.isAnonymous) qaBot.setAttribute('is-anonymous', '');
235238
if (props.disabled) qaBot.setAttribute('disabled', '');
236239
if (props.isOpen) qaBot.setAttribute('is-open', '');
237240
if (props.apiKey) qaBot.setAttribute('api-key', props.apiKey);

0 commit comments

Comments
 (0)