summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_alerts.js
blob: b44f787cf30f3e11b5ddaead4e6e586bc2533ee0 (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
import Vue from 'vue';
import Cookies from 'js-cookie';
import { parseBoolean } from '~/lib/utils/common_utils';
import DismissibleAlert from '~/vue_shared/components/dismissible_alert.vue';

const getCookieExpirationPeriod = (expirationPeriod) => {
  const defaultExpirationPeriod = 30;
  const alertExpirationPeriod = Number(expirationPeriod);

  return !expirationPeriod || Number.isNaN(alertExpirationPeriod)
    ? defaultExpirationPeriod
    : alertExpirationPeriod;
};

const mountVueAlert = (el) => {
  const props = {
    html: el.innerHTML,
  };
  const attrs = {
    ...el.dataset,
    dismissible: parseBoolean(el.dataset.dismissible),
  };
  const { dismissCookieName, dismissCookieExpire } = el.dataset;

  return new Vue({
    el,
    render(createElement) {
      return createElement(DismissibleAlert, {
        props,
        attrs,
        on: {
          alertDismissed() {
            if (!dismissCookieName) {
              return;
            }
            Cookies.set(dismissCookieName, true, {
              expires: getCookieExpirationPeriod(dismissCookieExpire),
            });
          },
        },
      });
    },
  });
};

export default () => [...document.querySelectorAll('.js-vue-alert')].map(mountVueAlert);