Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chatbot get now activation implemented #54

Merged
merged 2 commits into from
Oct 26, 2023
Merged
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
44 changes: 44 additions & 0 deletions __tests__/unit/chat-ui.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { loadingDots as loadingDotsObj } from '../../src/lib/utils';
import { customEventTypes } from '../../src/lib/custom/tracking-events';
import { actionService } from '../../src/lib/action-service';
import { getStringInAngleBrackets } from '../../src/lib/helpers';
import { experimentsPrompt } from '../../src/lib/config/prompts-affected';

const chatWidgets = require('../../src/lib/chat-widgets');

Expand Down Expand Up @@ -312,6 +313,49 @@ describe('ChatUi', () => {
expect(sut.track).toBeCalledWith(customEventTypes.linkProvided);
});

test('should call set cta button to close', () => {
// Arrange
Object.defineProperty(window, 'location', {
value: {
search: `?utm_chat=${experimentsPrompt.finalPage}`,
},
writable: true,
});
const link = 'https://example.com';

// Act
sut.init({ containerId: 'chatbot-container', translations });
sut.track = jest.fn();
sut.setCtaButtonToClose = jest.fn();

sut.link = link;
sut.setCtaButton();

// Assert
// expect(sut.elements.ctaButton.getAttribute('href')).toBe('javascript:void(0)');
expect(sut.elements.ctaButton.classList.contains('hidden')).toBe(false);

expect(sut.elements.promptContainer.classList.contains('hidden')).toBe(true);

expect(sut.setCtaButtonToClose).toBeCalledTimes(1);
expect(sut.track).toBeCalledTimes(1);
expect(sut.track).toBeCalledWith(customEventTypes.linkProvided);
});

test('should close the chat via cta button click', () => {
// Act
sut.init({ containerId: 'chatbot-container', translations });
sut.closeWidget = jest.fn();

sut.setCtaButtonToClose();

sut.elements.ctaButton.click();

// Assert
expect(sut.elements.ctaButton.getAttribute('href')).toBe('javascript:void(0)');
expect(sut.closeWidget).toBeCalledTimes(1);
});

test('shouldHideChat returns true when user has seen the chat and the history has not expired (24hrs)', () => {
// Arrange
localStorage.setItem(
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "1ff-chat-ui",
"version": "1.0.41",
"version": "1.0.42",
"description": "chatbot to communicate with taught ai",
"main": "src/lib/chat-ui.js",
"scripts": {
Expand Down
27 changes: 24 additions & 3 deletions src/lib/chat-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
} from './socket-services';
import { actionService } from './action-service';
import { customEventTypes, standardEventTypes } from './custom/tracking-events';
import { experimentsPrompt } from '../../src/lib/config/prompts-affected';

const nodeEvents = require('events');

Expand Down Expand Up @@ -202,8 +203,10 @@ const ChatUi = {

if (this.link) {
this.setCtaButton();
if(getTerm() === experimentsPrompt.finalPage){
this.link.remove();
}
}

if (content.includes(intentionType.email)) {
this.setEmailVisibility();
}
Expand Down Expand Up @@ -450,13 +453,27 @@ const ChatUi = {
*/
setCtaButton() {
this.elements.ctaButton.classList.remove('hidden');
this.elements.ctaButton.setAttribute('href', this.link);
if (getTerm() === experimentsPrompt.finalPage) {
this.setCtaButtonToClose();
} else {
this.elements.ctaButton.setAttribute('href', this.link);
}
input.hide(this);
this.track(customEventTypes.linkProvided);
this.elements.ctaButton.addEventListener('click', () => {
this.track(customEventTypes.linkClicked);
});
},

setCtaButtonToClose(){
this.elements.ctaButton.setAttribute('href', 'javascript:void(0)');

this.elements.ctaButton.addEventListener('click', (e) => {
e.preventDefault();
this.closeWidget();
});
},

/**
* Sets custom variables and applies them to the main container element and font family.
* Theme-specific properties are handled separately.
Expand Down Expand Up @@ -725,13 +742,17 @@ const ChatUi = {
this.closeSocket();
localStorage.setItem(CHAT_SEEN_KEY, true);
},
goBack() {
this.closeSocket();
history.back();
},
/**
* Attaches event listeners to the chat widget elements.
*
* @returns {void}
*/
attachListeners() {
this.elements.closeButton?.addEventListener('click', this.closeWidget.bind(this));
this.elements.closeButton?.addEventListener('click', this.goBack.bind(this));
this.elements.sendButton.addEventListener('click', this.addNewMessage.bind(this));
this.elements.ctaButton.addEventListener('click', this.closeWidget.bind(this));
this.elements.messageInput.addEventListener('keydown', this.onKeyDown.bind(this));
Expand Down
3 changes: 3 additions & 0 deletions src/lib/chat-widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
replaceLinksWithAnchors,
replaceStringInCurlyBracketsWithStrong,
removeStringInAngleBrackets,
getTerm,
} from './helpers';
import { actionService } from './action-service';
import { translations } from './config/translations';
import { experimentsPrompt } from '../../src/lib/config/prompts-affected';

export const chatMarkup = (config) => `<div class="chat-widget">
<div class="chat-widget__head">
Expand All @@ -19,6 +21,7 @@ export const chatMarkup = (config) => `<div class="chat-widget">
<span class="widget-role">${config.assistant.role}</span>
</span>
</div>
${getTerm() === experimentsPrompt.finalPage ? closeButton : ''}
</div>
<div class="chat-widget__messages" id="scroll-incrementor">
<div class="chat-widget__messages-container" id="message-incrementor"></div>
Expand Down
3 changes: 3 additions & 0 deletions src/lib/config/prompts-affected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const experimentsPrompt = {
finalPage: 'md-final-getdiet-usamdr',
};
4 changes: 4 additions & 0 deletions src/lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export function getRandomInteger(min, max) {
return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
}

export function removeUrl(text){
return text.replace(REGEX_URL);
}

/**
* Formats a date string according to the locale, including the date and time.
*
Expand Down
11 changes: 9 additions & 2 deletions src/lib/socket-services.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { UNSENT_MESSAGES_KEY } from './chat-ui';
import { rolesHTML } from './chat-widgets';
import { constructLink, replaceLinksWithAnchors, clearCarets } from './helpers';
import { constructLink, replaceLinksWithAnchors, clearCarets, getTerm, removeUrl } from './helpers';
import { errorMessage, input, loadingDots, messages, resendButton } from './utils';
import { experimentsPrompt } from '../../src/lib/config/prompts-affected';

/**
* Handles the start of the stream.
* Hides loading dots, appends the current timestamp, and adds the assistant role element.
Expand Down Expand Up @@ -129,7 +131,12 @@ export function onStreamEnd() {
lastMessageTextContainer.classList.remove('cursor');
this.hasAnswers = lastMessageElement.querySelector('.answers-container');
this.link = constructLink(lastMessageTextContainer.innerHTML);
lastMessageTextContainer.innerHTML = replaceLinksWithAnchors(lastMessageTextContainer.innerHTML);

if (getTerm() === experimentsPrompt.finalPage) {
lastMessageTextContainer.innerHTML = removeUrl(lastMessageTextContainer.innerHTML);
} else {
lastMessageTextContainer.innerHTML = replaceLinksWithAnchors(lastMessageTextContainer.innerHTML);
}

if (this.link) {
this.setCtaButton();
Expand Down