summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/admin/users/tabs.js
blob: cbaab7df4e9ec4c4a75733e451e20f416709f880 (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 Api from '~/api';
import { historyPushState } from '~/lib/utils/common_utils';
import { mergeUrlParams } from '~/lib/utils/url_utility';

const COHORTS_PANE = 'cohorts';
const COHORTS_PANE_TAB_CLICK_EVENT = 'i_analytics_cohorts';

const tabClickHandler = (e) => {
  const { hash } = e.currentTarget;

  let tab = null;

  if (hash === `#${COHORTS_PANE}`) {
    tab = COHORTS_PANE;
    Api.trackRedisHllUserEvent(COHORTS_PANE_TAB_CLICK_EVENT);
  }

  const newUrl = mergeUrlParams({ tab }, window.location.href);
  historyPushState(newUrl);
};

const initTabs = () => {
  const tabLinks = document.querySelectorAll('.js-users-tab-item a');

  if (tabLinks.length) {
    tabLinks.forEach((tabLink) => {
      tabLink.addEventListener('click', (e) => tabClickHandler(e));
    });
  }
};

export default initTabs;