summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/alerts_service_settings/components/alerts_service_form.vue
blob: a2d94fb8083e5446a24c452ab718152a0f4bbec3 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<script>
import {
  GlButton,
  GlFormGroup,
  GlFormInput,
  GlLink,
  GlModal,
  GlModalDirective,
  GlSprintf,
} from '@gitlab/ui';
import ClipboardButton from '~/vue_shared/components/clipboard_button.vue';
import ToggleButton from '~/vue_shared/components/toggle_button.vue';
import axios from '~/lib/utils/axios_utils';
import { s__, __ } from '~/locale';
import createFlash from '~/flash';

export default {
  i18n: {
    usageSection: s__(
      'AlertService|You must provide this URL and authorization key to authorize an external service to send alerts to GitLab. You can provide this URL and key to multiple services. After configuring an external service, alerts from your service will display on the GitLab %{linkStart}Alerts%{linkEnd} page.',
    ),
    setupSection: s__(
      "AlertService|Review your external service's documentation to learn where to provide this information to your external service, and the %{linkStart}GitLab documentation%{linkEnd} to learn more about configuring your endpoint.",
    ),
  },
  COPY_TO_CLIPBOARD: __('Copy'),
  RESET_KEY: __('Reset key'),
  components: {
    GlButton,
    GlFormGroup,
    GlFormInput,
    GlLink,
    GlModal,
    GlSprintf,
    ClipboardButton,
    ToggleButton,
  },
  directives: {
    'gl-modal': GlModalDirective,
  },
  props: {
    alertsSetupUrl: {
      type: String,
      required: true,
    },
    alertsUsageUrl: {
      type: String,
      required: true,
    },
    initialAuthorizationKey: {
      type: String,
      required: false,
      default: '',
    },
    formPath: {
      type: String,
      required: true,
    },
    url: {
      type: String,
      required: true,
    },
    initialActivated: {
      type: Boolean,
      required: true,
    },
    isDisabled: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      activated: this.initialActivated,
      loadingActivated: false,
      authorizationKey: this.initialAuthorizationKey,
    };
  },
  computed: {
    sections() {
      return [
        {
          text: this.$options.i18n.usageSection,
          url: this.alertsUsageUrl,
        },
        {
          text: this.$options.i18n.setupSection,
          url: this.alertsSetupUrl,
        },
      ];
    },
  },
  watch: {
    activated() {
      this.updateIcon();
    },
  },
  methods: {
    updateIcon() {
      return document.querySelectorAll('.js-service-active-status').forEach(icon => {
        if (icon.dataset.value === this.activated.toString()) {
          icon.classList.remove('d-none');
        } else {
          icon.classList.add('d-none');
        }
      });
    },
    resetKey() {
      return axios
        .put(this.formPath, { service: { token: '' } })
        .then(res => {
          this.authorizationKey = res.data.token;
        })
        .catch(() => {
          createFlash(__('Failed to reset key. Please try again.'));
        });
    },
    toggleActivated(value) {
      this.loadingActivated = true;
      return axios
        .put(this.formPath, { service: { active: value } })
        .then(() => {
          this.activated = value;
          this.loadingActivated = false;
        })
        .catch(() => {
          createFlash(__('Update failed. Please try again.'));
          this.loadingActivated = false;
        });
    },
  },
};
</script>

<template>
  <div>
    <div data-testid="description">
      <p v-for="section in sections" :key="section.text">
        <gl-sprintf :message="section.text">
          <template #link="{ content }">
            <gl-link :href="section.url" target="_blank">{{ content }}</gl-link>
          </template>
        </gl-sprintf>
      </p>
    </div>
    <gl-form-group :label="__('Active')" label-for="activated" label-class="label-bold">
      <toggle-button
        id="activated"
        :disabled-input="loadingActivated || isDisabled"
        :is-loading="loadingActivated"
        :value="activated"
        @change="toggleActivated"
      />
    </gl-form-group>
    <gl-form-group :label="__('URL')" label-for="url" label-class="label-bold">
      <div class="input-group">
        <gl-form-input id="url" :readonly="true" :value="url" />
        <span class="input-group-append">
          <clipboard-button
            :text="url"
            :title="$options.COPY_TO_CLIPBOARD"
            :disabled="isDisabled"
          />
        </span>
      </div>
    </gl-form-group>
    <gl-form-group
      :label="__('Authorization key')"
      label-for="authorization-key"
      label-class="label-bold"
    >
      <div class="input-group">
        <gl-form-input id="authorization-key" :readonly="true" :value="authorizationKey" />
        <span class="input-group-append">
          <clipboard-button
            :text="authorizationKey"
            :title="$options.COPY_TO_CLIPBOARD"
            :disabled="isDisabled"
          />
        </span>
      </div>
      <gl-button v-gl-modal.authKeyModal class="mt-2" :disabled="isDisabled">{{
        $options.RESET_KEY
      }}</gl-button>
      <gl-modal
        modal-id="authKeyModal"
        :title="$options.RESET_KEY"
        :ok-title="$options.RESET_KEY"
        ok-variant="danger"
        @ok="resetKey"
      >
        {{
          __(
            'Resetting the authorization key for this project will require updating the authorization key in every alert source it is enabled in.',
          )
        }}
      </gl-modal>
    </gl-form-group>
  </div>
</template>