summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/notifications/components/custom_notifications_modal.vue
blob: 0f628897e1754317cb72efae9aa503323c630dfd (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
<script>
import { GlModal, GlSprintf, GlLink, GlLoadingIcon, GlFormGroup, GlFormCheckbox } from '@gitlab/ui';
import Api from '~/api';
import { i18n } from '../constants';

export default {
  name: 'CustomNotificationsModal',
  components: {
    GlModal,
    GlSprintf,
    GlLink,
    GlLoadingIcon,
    GlFormGroup,
    GlFormCheckbox,
  },
  inject: {
    projectId: {
      default: null,
    },
    groupId: {
      default: null,
    },
    helpPagePath: {
      default: '',
    },
  },
  props: {
    modalId: {
      type: String,
      required: false,
      default: 'custom-notifications-modal',
    },
  },
  data() {
    return {
      isLoading: false,
      events: [],
    };
  },
  methods: {
    open() {
      this.$refs.modal.show();
    },
    buildEvents(events) {
      return Object.keys(events).map((key) => ({
        id: key,
        enabled: Boolean(events[key]),
        name: this.$options.i18n.eventNames[key] || '',
        loading: false,
      }));
    },
    async onOpen() {
      if (!this.events.length) {
        await this.loadNotificationSettings();
      }
    },
    async loadNotificationSettings() {
      this.isLoading = true;

      try {
        const {
          data: { events },
        } = await Api.getNotificationSettings(this.projectId, this.groupId);

        this.events = this.buildEvents(events);
      } catch (error) {
        this.$toast.show(this.$options.i18n.loadNotificationLevelErrorMessage, { type: 'error' });
      } finally {
        this.isLoading = false;
      }
    },
    async updateEvent(isEnabled, event) {
      const index = this.events.findIndex((e) => e.id === event.id);

      // update loading state for the given event
      this.$set(this.events, index, { ...this.events[index], loading: true });

      try {
        const {
          data: { events },
        } = await Api.updateNotificationSettings(this.projectId, this.groupId, {
          [event.id]: isEnabled,
        });

        this.events = this.buildEvents(events);
      } catch (error) {
        this.$toast.show(this.$options.i18n.updateNotificationLevelErrorMessage, { type: 'error' });
      }
    },
  },
  i18n,
};
</script>

<template>
  <gl-modal
    ref="modal"
    :modal-id="modalId"
    :title="$options.i18n.customNotificationsModal.title"
    @show="onOpen"
  >
    <div class="container-fluid">
      <div class="row">
        <div class="col-lg-4">
          <h4 class="gl-mt-0" data-testid="modalBodyTitle">
            {{ $options.i18n.customNotificationsModal.bodyTitle }}
          </h4>
          <gl-sprintf :message="$options.i18n.customNotificationsModal.bodyMessage">
            <template #notificationLink="{ content }">
              <gl-link :href="helpPagePath" target="_blank">{{ content }}</gl-link>
            </template>
          </gl-sprintf>
        </div>
        <div class="col-lg-8">
          <gl-loading-icon v-if="isLoading" size="lg" class="gl-mt-3" />
          <template v-else>
            <gl-form-group v-for="event in events" :key="event.id">
              <gl-form-checkbox v-model="event.enabled" @change="updateEvent($event, event)">
                <strong>{{ event.name }}</strong
                ><gl-loading-icon v-if="event.loading" :inline="true" class="gl-ml-2" />
              </gl-form-checkbox>
            </gl-form-group>
          </template>
        </div>
      </div>
    </div>
  </gl-modal>
</template>