summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/usage_ping_consent.js
blob: 3876aa62b751f7e31f387215607ceca44b2632dd (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
import $ from 'jquery';
import { deprecatedCreateFlash as Flash, hideFlash } from './flash';
import axios from './lib/utils/axios_utils';
import { parseBoolean } from './lib/utils/common_utils';
import { __ } from './locale';

export default () => {
  $('body').on('click', '.js-usage-consent-action', (e) => {
    e.preventDefault();
    e.stopImmediatePropagation(); // overwrite rails listener

    const { url, checkEnabled, pingEnabled } = e.target.dataset;
    const data = {
      application_setting: {
        version_check_enabled: parseBoolean(checkEnabled),
        usage_ping_enabled: parseBoolean(pingEnabled),
      },
    };

    const hideConsentMessage = () => hideFlash(document.querySelector('.ping-consent-message'));

    axios
      .put(url, data)
      .then(() => {
        hideConsentMessage();
      })
      .catch(() => {
        hideConsentMessage();
        Flash(__('Something went wrong. Try again later.'));
      });
  });
};