From f3b8fbeba29705d03347f1a79de8ddca0309afc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Felchar?= <11652381+cauefcr@users.noreply.github.com> Date: Fri, 25 Mar 2022 16:40:16 -0300 Subject: [PATCH] [FIX] Fixes broken triggers. (#695) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * updated beginning of the file. * 1 fixed, 1 working, 1 to go * all working 🎉 --- src/components/App/index.js | 5 +++-- src/lib/triggers.js | 28 ++++++++++++++++------------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/components/App/index.js b/src/components/App/index.js index ac31d4c53..ed4cec757 100644 --- a/src/components/App/index.js +++ b/src/components/App/index.js @@ -65,6 +65,7 @@ export class App extends Component { return route('/leave-message'); } + const showDepartment = departments.filter((dept) => dept.showOnRegistration).length > 0; const showRegistrationForm = ( @@ -79,7 +80,7 @@ export class App extends Component { }, 100); } - handleTriggers() { + handleTriggers = () => { const { config: { online, enabled } } = this.props; Triggers.enabled = online && enabled; @@ -87,6 +88,7 @@ export class App extends Component { if (online && enabled) { Triggers.init(); } + Triggers.processTriggers(); } handleEnableNotifications = () => { @@ -121,7 +123,6 @@ export class App extends Component { } handleOpenWindow = () => { - parentCall('openPopout'); const { dispatch } = this.props; dispatch({ undocked: true, minimized: false }); } diff --git a/src/lib/triggers.js b/src/lib/triggers.js index c7a6e6b8d..26b668cdf 100644 --- a/src/lib/triggers.js +++ b/src/lib/triggers.js @@ -86,17 +86,14 @@ class Triggers { } }); }); - - this.processTriggers(); } async fire(trigger) { const { token, user, firedTriggers = [] } = store.state; - if (!this._enabled || user) { + if (!this._enabled || !user) { return; } const { actions } = trigger; - await asyncForEach(actions, (action) => { if (action.name === 'send-message') { trigger.skip = true; @@ -152,17 +149,13 @@ class Triggers { return; } - const self = this; trigger.conditions.forEach((condition) => { switch (condition.name) { case 'page-url': - this._requests.forEach((request) => { - const hrefRegExp = new RegExp(condition.value, 'g'); - if (request.location.href.match(hrefRegExp)) { - self.fire(trigger); - } - }); - this._requests = []; + const hrefRegExp = new RegExp(condition.value, 'g'); + if (hrefRegExp.test(window.location.href)) { + this.fire(trigger); + } break; case 'time-on-site': if (trigger.timeout) { @@ -172,9 +165,20 @@ class Triggers { this.fire(trigger); }, parseInt(condition.value, 10) * 1000); break; + case 'chat-opened-by-visitor': + const openFunc = () => { + const { user } = store.state; + if (user) { + if (trigger.runOnce) { store.off('change', openFunc); } + this.fire(trigger); + } + }; + store.on('change', openFunc); + break; } }); }); + this._requests = []; } set triggers(newTriggers) {