summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/persistent_user_callout.js
blob: 4a08e158f6bca6f1aec90223a4d9942adb9150fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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);
  }
}