Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
[FIX] Fixes broken triggers. (#695)
Browse files Browse the repository at this point in the history
* updated beginning of the file.

* 1 fixed, 1 working, 1 to go

* all working 🎉
  • Loading branch information
cauefcr authored Mar 25, 2022
1 parent fb1b2ee commit f3b8fbe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/components/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class App extends Component {
return route('/leave-message');
}


const showDepartment = departments.filter((dept) => dept.showOnRegistration).length > 0;

const showRegistrationForm = (
Expand All @@ -79,14 +80,15 @@ export class App extends Component {
}, 100);
}

handleTriggers() {
handleTriggers = () => {
const { config: { online, enabled } } = this.props;

Triggers.enabled = online && enabled;

if (online && enabled) {
Triggers.init();
}
Triggers.processTriggers();
}

handleEnableNotifications = () => {
Expand Down Expand Up @@ -121,7 +123,6 @@ export class App extends Component {
}

handleOpenWindow = () => {
parentCall('openPopout');
const { dispatch } = this.props;
dispatch({ undocked: true, minimized: false });
}
Expand Down
28 changes: 16 additions & 12 deletions src/lib/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit f3b8fbe

Please sign in to comment.