summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/webhooks/components/test_dropdown.vue
blob: 78e5dff6f5963d4bca6ea74d44a93a712e8c5717 (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
<script>
import { GlDisclosureDropdown } from '@gitlab/ui';
import { __ } from '~/locale';

export default {
  name: 'HookTestDropdown',
  components: {
    GlDisclosureDropdown,
  },
  props: {
    items: {
      type: Array,
      required: true,
    },
    size: {
      type: String,
      required: false,
      default: undefined,
    },
  },
  computed: {
    itemsWithAction() {
      return this.items.map((item) => ({
        text: item.text,
        action: () => this.testHook(item.href),
      }));
    },
  },
  methods: {
    testHook(href) {
      // HACK: Trigger @rails/ujs's data-method handling.
      //
      // The more obvious approaches of (1) declaratively rendering the
      // links using GlDisclosureDropdown's list-item slot and (2) using
      // item.extraAttrs to set the data-method attributes on the links
      // do not work for reasons laid out in
      // https://gitlab.com/gitlab-org/gitlab-ui/-/issues/2134.
      //
      // Sending the POST with axios also doesn't work, since the
      // endpoints return 302 redirects. Since axios uses XMLHTTPRequest,
      // it transparently follows redirects, meaning the Location header
      // of the first response cannot be inspected/acted upon by JS. We
      // could manually trigger a reload afterwards, but that would mean
      // a duplicate fetch of the current page: one by the XHR, and one
      // by the explicit reload. It would also mean losing the flash
      // alert set by the backend, making the feature useless for the
      // user.
      //
      // The ideal fix here would be to refactor the test endpoint to
      // return a JSON response, removing the need for a redirect/page
      // reload to show the result.
      const a = document.createElement('a');
      a.setAttribute('hidden', '');
      a.href = href;
      a.dataset.method = 'post';
      document.body.appendChild(a);
      a.click();
      a.remove();
    },
  },
  i18n: {
    test: __('Test'),
  },
};
</script>

<template>
  <gl-disclosure-dropdown :toggle-text="$options.i18n.test" :items="itemsWithAction" :size="size" />
</template>