2021-06-20 23:10:27 -04:00
|
|
|
import createFlash from './flash';
|
2018-12-06 08:41:20 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { parseBoolean } from './lib/utils/common_utils';
|
2018-12-06 08:41:20 -05:00
|
|
|
import { __ } from './locale';
|
|
|
|
|
2019-08-05 05:00:34 -04:00
|
|
|
const DEFERRED_LINK_CLASS = 'deferred-link';
|
|
|
|
|
2018-12-06 08:41:20 -05:00
|
|
|
export default class PersistentUserCallout {
|
2019-12-02 22:06:35 -05:00
|
|
|
constructor(container, options = container.dataset) {
|
2022-02-04 13:17:50 -05:00
|
|
|
const { dismissEndpoint, featureId, groupId, deferLinks } = options;
|
2018-12-06 08:41:20 -05:00
|
|
|
this.container = container;
|
|
|
|
this.dismissEndpoint = dismissEndpoint;
|
|
|
|
this.featureId = featureId;
|
2022-02-04 13:17:50 -05:00
|
|
|
this.groupId = groupId;
|
2019-08-05 05:00:34 -04:00
|
|
|
this.deferLinks = parseBoolean(deferLinks);
|
2022-03-07 13:19:30 -05:00
|
|
|
this.closeButtons = this.container.querySelectorAll('.js-close');
|
2018-12-06 08:41:20 -05:00
|
|
|
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2020-06-26 08:08:51 -04:00
|
|
|
const followLink = this.container.querySelector('.js-follow-link');
|
2020-05-18 08:08:08 -04:00
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
if (this.closeButtons.length) {
|
|
|
|
this.handleCloseButtonCallout();
|
2020-06-26 08:08:51 -04:00
|
|
|
} else if (followLink) {
|
|
|
|
this.handleFollowLinkCallout(followLink);
|
2020-05-18 08:08:08 -04:00
|
|
|
}
|
2020-06-26 08:08:51 -04:00
|
|
|
}
|
2020-05-18 08:08:08 -04:00
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
handleCloseButtonCallout() {
|
|
|
|
this.closeButtons.forEach((closeButton) => {
|
|
|
|
closeButton.addEventListener('click', this.dismiss);
|
|
|
|
});
|
2019-08-05 05:00:34 -04:00
|
|
|
|
|
|
|
if (this.deferLinks) {
|
2020-12-23 19:10:25 -05:00
|
|
|
this.container.addEventListener('click', (event) => {
|
2019-08-05 05:00:34 -04:00
|
|
|
const isDeferredLink = event.target.classList.contains(DEFERRED_LINK_CLASS);
|
|
|
|
if (isDeferredLink) {
|
|
|
|
const { href, target } = event.target;
|
|
|
|
|
|
|
|
this.dismiss(event, { href, target });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-12-06 08:41:20 -05:00
|
|
|
}
|
|
|
|
|
2020-06-26 08:08:51 -04:00
|
|
|
handleFollowLinkCallout(followLink) {
|
2020-12-23 19:10:25 -05:00
|
|
|
followLink.addEventListener('click', (event) => this.registerCalloutWithLink(event));
|
2020-06-26 08:08:51 -04:00
|
|
|
}
|
|
|
|
|
2022-03-07 13:19:30 -05:00
|
|
|
dismiss = (event, deferredLinkOptions = null) => {
|
2018-12-06 08:41:20 -05:00
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
axios
|
|
|
|
.post(this.dismissEndpoint, {
|
|
|
|
feature_name: this.featureId,
|
2022-02-04 13:17:50 -05:00
|
|
|
group_id: this.groupId,
|
2018-12-06 08:41:20 -05:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.container.remove();
|
2022-03-07 13:19:30 -05:00
|
|
|
this.closeButtons.forEach((closeButton) => {
|
|
|
|
closeButton.removeEventListener('click', this.dismiss);
|
|
|
|
});
|
2019-08-05 05:00:34 -04:00
|
|
|
|
|
|
|
if (deferredLinkOptions) {
|
|
|
|
const { href, target } = deferredLinkOptions;
|
|
|
|
window.open(href, target);
|
|
|
|
}
|
2018-12-06 08:41:20 -05:00
|
|
|
})
|
|
|
|
.catch(() => {
|
2021-06-20 23:10:27 -04:00
|
|
|
createFlash({
|
|
|
|
message: __(
|
|
|
|
'An error occurred while dismissing the alert. Refresh the page and try again.',
|
|
|
|
),
|
|
|
|
});
|
2018-12-06 08:41:20 -05:00
|
|
|
});
|
2022-03-07 13:19:30 -05:00
|
|
|
};
|
2019-02-19 16:26:07 -05:00
|
|
|
|
2020-06-26 08:08:51 -04:00
|
|
|
registerCalloutWithLink(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const { href } = event.currentTarget;
|
|
|
|
|
|
|
|
axios
|
|
|
|
.post(this.dismissEndpoint, {
|
|
|
|
feature_name: this.featureId,
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
window.location.assign(href);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
2021-06-20 23:10:27 -04:00
|
|
|
createFlash({
|
|
|
|
message: __(
|
2020-06-26 08:08:51 -04:00
|
|
|
'An error occurred while acknowledging the notification. Refresh the page and try again.',
|
|
|
|
),
|
2021-06-20 23:10:27 -04:00
|
|
|
});
|
2020-06-26 08:08:51 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-02 22:06:35 -05:00
|
|
|
static factory(container, options) {
|
2019-02-19 16:26:07 -05:00
|
|
|
if (!container) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2019-12-02 22:06:35 -05:00
|
|
|
return new PersistentUserCallout(container, options);
|
2019-02-19 16:26:07 -05:00
|
|
|
}
|
2018-12-06 08:41:20 -05:00
|
|
|
}
|