gitlab-org--gitlab-foss/app/assets/javascripts/persistent_user_callout.js
Luke Bennett 1263c79095
Add PersistentUserCallout factory method
Use factory method to DRY safe instantiation.
Add tests for PersistentUserCallout.
2019-03-06 10:40:19 +00:00

42 lines
1,004 B
JavaScript

import axios from './lib/utils/axios_utils';
import { __ } from './locale';
import Flash from './flash';
export default class PersistentUserCallout {
constructor(container) {
const { dismissEndpoint, featureId } = container.dataset;
this.container = container;
this.dismissEndpoint = dismissEndpoint;
this.featureId = featureId;
this.init();
}
init() {
const closeButton = this.container.querySelector('.js-close');
closeButton.addEventListener('click', event => this.dismiss(event));
}
dismiss(event) {
event.preventDefault();
axios
.post(this.dismissEndpoint, {
feature_name: this.featureId,
})
.then(() => {
this.container.remove();
})
.catch(() => {
Flash(__('An error occurred while dismissing the alert. Refresh the page and try again.'));
});
}
static factory(container) {
if (!container) {
return undefined;
}
return new PersistentUserCallout(container);
}
}