summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/persistent_user_callout.js
blob: 9cea89f4990e7d127abfafdd85d7026fcaa9e05a (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import createFlash from './flash';
import axios from './lib/utils/axios_utils';
import { parseBoolean } from './lib/utils/common_utils';
import { __ } from './locale';

const DEFERRED_LINK_CLASS = 'deferred-link';

export default class PersistentUserCallout {
  constructor(container, options = container.dataset) {
    const { dismissEndpoint, featureId, groupId, namespaceId, deferLinks } = options;
    this.container = container;
    this.dismissEndpoint = dismissEndpoint;
    this.featureId = featureId;
    this.groupId = groupId;
    this.namespaceId = namespaceId;
    this.deferLinks = parseBoolean(deferLinks);
    this.closeButtons = this.container.querySelectorAll('.js-close');

    this.init();
  }

  init() {
    const followLink = this.container.querySelector('.js-follow-link');

    if (this.closeButtons.length) {
      this.handleCloseButtonCallout();
    } else if (followLink) {
      this.handleFollowLinkCallout(followLink);
    }
  }

  handleCloseButtonCallout() {
    this.closeButtons.forEach((closeButton) => {
      closeButton.addEventListener('click', this.dismiss);
    });

    if (this.deferLinks) {
      this.container.addEventListener('click', (event) => {
        const isDeferredLink = event.target.classList.contains(DEFERRED_LINK_CLASS);
        if (isDeferredLink) {
          const { href, target } = event.target;

          this.dismiss(event, { href, target });
        }
      });
    }
  }

  handleFollowLinkCallout(followLink) {
    followLink.addEventListener('click', (event) => this.registerCalloutWithLink(event));
  }

  dismiss = (event, deferredLinkOptions = null) => {
    event.preventDefault();

    axios
      .post(this.dismissEndpoint, {
        feature_name: this.featureId,
        group_id: this.groupId,
        namespace_id: this.namespaceId,
      })
      .then(() => {
        this.container.remove();
        this.closeButtons.forEach((closeButton) => {
          closeButton.removeEventListener('click', this.dismiss);
        });

        if (deferredLinkOptions) {
          const { href, target } = deferredLinkOptions;
          window.open(href, target);
        }
      })
      .catch(() => {
        createFlash({
          message: __(
            'An error occurred while dismissing the alert. Refresh the page and try again.',
          ),
        });
      });
  };

  registerCalloutWithLink(event) {
    event.preventDefault();

    const { href } = event.currentTarget;

    axios
      .post(this.dismissEndpoint, {
        feature_name: this.featureId,
      })
      .then(() => {
        window.location.assign(href);
      })
      .catch(() => {
        createFlash({
          message: __(
            'An error occurred while acknowledging the notification. Refresh the page and try again.',
          ),
        });
      });
  }

  static factory(container, options) {
    if (!container) {
      return undefined;
    }

    return new PersistentUserCallout(container, options);
  }
}