summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/sidebar/components/subscriptions/subscriptions.vue
blob: 6e0040840770bfbbf3d85a27319e9a2d5139ed10 (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
129
130
131
132
133
134
<script>
import { GlIcon, GlTooltipDirective } from '@gitlab/ui';
import { __ } from '~/locale';
import Tracking from '~/tracking';
import toggleButton from '~/vue_shared/components/toggle_button.vue';
import eventHub from '../../event_hub';

const ICON_ON = 'notifications';
const ICON_OFF = 'notifications-off';
const LABEL_ON = __('Notifications on');
const LABEL_OFF = __('Notifications off');

export default {
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  components: {
    GlIcon,
    toggleButton,
  },
  mixins: [Tracking.mixin({ label: 'right_sidebar' })],
  props: {
    loading: {
      type: Boolean,
      required: false,
      default: false,
    },
    projectEmailsDisabled: {
      type: Boolean,
      required: false,
      default: false,
    },
    subscribeDisabledDescription: {
      type: String,
      required: false,
      default: '',
    },
    subscribed: {
      type: Boolean,
      required: false,
      default: null,
    },
    id: {
      type: Number,
      required: false,
      default: null,
    },
  },
  computed: {
    tracking() {
      return {
        // eslint-disable-next-line no-underscore-dangle
        category: this.$options._componentTag,
      };
    },
    showLoadingState() {
      return this.subscribed === null;
    },
    notificationIcon() {
      if (this.projectEmailsDisabled) {
        return ICON_OFF;
      }
      return this.subscribed ? ICON_ON : ICON_OFF;
    },
    notificationTooltip() {
      if (this.projectEmailsDisabled) {
        return this.subscribeDisabledDescription;
      }
      return this.subscribed ? LABEL_ON : LABEL_OFF;
    },
    notificationText() {
      if (this.projectEmailsDisabled) {
        return this.subscribeDisabledDescription;
      }
      return __('Notifications');
    },
  },
  methods: {
    /**
     * We need to emit this event on both component & eventHub
     * for 2 dependencies;
     *
     * 1. eventHub: This component is used in Issue Boards sidebar
     *              where component template is part of HAML
     *              and event listeners are tied to app's eventHub.
     * 2. Component: This compone is also used in Epics in EE
     *               where listeners are tied to component event.
     */
    toggleSubscription() {
      // App's eventHub event emission.
      eventHub.$emit('toggleSubscription', this.id);

      // Component event emission.
      this.$emit('toggleSubscription', this.id);

      this.track('toggle_button', {
        property: 'notifications',
        value: this.subscribed ? 0 : 1,
      });
    },
    onClickCollapsedIcon() {
      this.$emit('toggleSidebar');
    },
  },
};
</script>

<template>
  <div>
    <span
      ref="tooltip"
      v-gl-tooltip.viewport.left
      :title="notificationTooltip"
      class="sidebar-collapsed-icon"
      @click="onClickCollapsedIcon"
    >
      <gl-icon
        :name="notificationIcon"
        :size="16"
        aria-hidden="true"
        class="sidebar-item-icon is-active"
      />
    </span>
    <span class="issuable-header-text hide-collapsed float-left"> {{ notificationText }} </span>
    <toggle-button
      v-if="!projectEmailsDisabled"
      ref="toggleButton"
      :is-loading="showLoadingState"
      :value="subscribed"
      class="float-right hide-collapsed js-issuable-subscribe-button"
      @change="toggleSubscription"
    />
  </div>
</template>