summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/onboarding_issues/index.js
blob: b23a10c9254eda4ee5775f0690a482e6db669358 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import $ from 'jquery';
import { parseBoolean, getCookie, setCookie, removeCookie } from '~/lib/utils/common_utils';
import { __, sprintf } from '~/locale';
import Tracking from '~/tracking';

const COOKIE_NAME = 'onboarding_issues_settings';

const POPOVER_LOCATIONS = {
  GROUPS_SHOW: 'groups#show',
  PROJECTS_SHOW: 'projects#show',
  ISSUES_INDEX: 'issues#index',
};

const removeLearnGitLabCookie = () => {
  removeCookie(COOKIE_NAME);
};

function disposePopover(event) {
  event.preventDefault();
  this.popover('dispose');
  removeLearnGitLabCookie();
  Tracking.event('Growth::Conversion::Experiment::OnboardingIssues', 'dismiss_popover');
}

const showPopover = (el, path, footer, options) => {
  // Cookie value looks like `{ 'groups#show': true, 'projects#show': true, 'issues#index': true }`. When it doesn't exist, don't show the popover.
  const cookie = getCookie(COOKIE_NAME);
  if (!cookie) return;

  // When the popover action has already been taken, don't show the popover.
  const settings = JSON.parse(cookie);
  if (!parseBoolean(settings[path])) return;

  const defaultOptions = {
    boundary: 'window',
    html: true,
    placement: 'top',
    template: `<div class="popover blue learn-gitlab d-none d-xl-block" role="tooltip">
                <div class="arrow"></div>
                <div class="close cursor-pointer gl-font-base text-white gl-opacity-10 p-2">&#10005</div>
                <div class="popover-body gl-font-base gl-line-height-20 pb-0 px-3"></div>
                <div class="bold text-right text-white p-2">${footer}</div>
               </div>`,
  };

  // When one of the popovers is dismissed, remove the cookie.
  const closeButton = () => document.querySelector('.learn-gitlab.popover .close');

  // We still have to use jQuery, since Bootstrap's Popover is based on jQuery.
  const jQueryEl = $(el);
  const clickCloseButton = disposePopover.bind(jQueryEl);

  jQueryEl
    .popover({ ...defaultOptions, ...options })
    .on('inserted.bs.popover', () => closeButton().addEventListener('click', clickCloseButton))
    .on('hide.bs.dropdown', () => closeButton().removeEventListener('click', clickCloseButton))
    .popover('show');

  // The previous popover actions have been taken, don't show those popovers anymore.
  Object.keys(settings).forEach((pathSetting) => {
    if (path !== pathSetting) {
      settings[pathSetting] = false;
    } else {
      setCookie(COOKIE_NAME, settings);
    }
  });

  // The final popover action will be taken on click, we then no longer need the cookie.
  if (path === POPOVER_LOCATIONS.ISSUES_INDEX) {
    el.addEventListener('click', removeLearnGitLabCookie);
  }
};

export const showLearnGitLabGroupItemPopover = (id) => {
  const el = document.querySelector(`#group-${id} .group-text a`);

  if (!el) return;

  const options = {
    content: __(
      'Here are all your projects in your group, including the one you just created. To start, let’s take a look at your personalized learning project which will help you learn about GitLab at your own pace.',
    ),
  };

  showPopover(el, POPOVER_LOCATIONS.GROUPS_SHOW, '1 / 2', options);
};

export const showLearnGitLabProjectPopover = () => {
  // Do not show a popover if we are not viewing the 'Learn GitLab' project.
  if (!window.location.pathname.includes('learn-gitlab')) return;

  const el = document.querySelector('a.shortcuts-issues');

  if (!el) return;

  const options = {
    content: sprintf(
      __(
        'Go to %{strongStart}Issues%{strongEnd} &gt; %{strongStart}Boards%{strongEnd} to access your personalized learning issue board.',
      ),
      { strongStart: '<strong>', strongEnd: '</strong>' },
      false,
    ),
  };

  showPopover(el, POPOVER_LOCATIONS.PROJECTS_SHOW, '2 / 2', options);
};

export const showLearnGitLabIssuesPopover = () => {
  // Do not show a popover if we are not viewing the 'Learn GitLab' project.
  if (!window.location.pathname.includes('learn-gitlab')) return;

  const el = document.querySelector('a[data-qa-selector="issue_boards_link"]');

  if (!el) return;

  const options = {
    content: sprintf(
      __(
        'Go to %{strongStart}Issues%{strongEnd} &gt; %{strongStart}Boards%{strongEnd} to access your personalized learning issue board.',
      ),
      { strongStart: '<strong>', strongEnd: '</strong>' },
      false,
    ),
  };

  showPopover(el, POPOVER_LOCATIONS.ISSUES_INDEX, '2 / 2', options);
};