summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/runner/components/runner_pause_button.vue
blob: 334e5f6023a66fb49d8e30e1d1c9a9eb9e4c2e8a (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
<script>
import { GlButton, GlTooltipDirective } from '@gitlab/ui';
import runnerToggleActiveMutation from '~/runner/graphql/shared/runner_toggle_active.mutation.graphql';
import { createAlert } from '~/flash';
import { captureException } from '~/runner/sentry_utils';
import { I18N_PAUSE, I18N_PAUSE_TOOLTIP, I18N_RESUME, I18N_RESUME_TOOLTIP } from '../constants';

export default {
  name: 'RunnerPauseButton',
  components: {
    GlButton,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  props: {
    runner: {
      type: Object,
      required: true,
    },
    compact: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  emits: ['toggledPaused'],
  data() {
    return {
      updating: false,
    };
  },
  computed: {
    isActive() {
      return this.runner.active;
    },
    icon() {
      return this.isActive ? 'pause' : 'play';
    },
    label() {
      return this.isActive ? I18N_PAUSE : I18N_RESUME;
    },
    buttonContent() {
      if (this.compact) {
        return null;
      }
      return this.label;
    },
    ariaLabel() {
      if (this.compact) {
        return this.label;
      }
      return null;
    },
    tooltip() {
      // Prevent a "sticky" tooltip: If this button is disabled,
      // mouseout listeners don't run leaving the tooltip stuck
      if (!this.updating) {
        return this.isActive ? I18N_PAUSE_TOOLTIP : I18N_RESUME_TOOLTIP;
      }
      return '';
    },
  },
  methods: {
    async onToggle() {
      this.updating = true;
      try {
        const input = {
          id: this.runner.id,
          active: !this.isActive,
        };

        const {
          data: {
            runnerUpdate: { errors },
          },
        } = await this.$apollo.mutate({
          mutation: runnerToggleActiveMutation,
          variables: {
            input,
          },
        });

        if (errors && errors.length) {
          throw new Error(errors.join(' '));
        }
        this.$emit('toggledPaused');
      } catch (e) {
        this.onError(e);
      } finally {
        this.updating = false;
      }
    },
    onError(error) {
      const { message } = error;

      createAlert({ message });
      captureException({ error, component: this.$options.name });
    },
  },
};
</script>

<template>
  <gl-button
    v-gl-tooltip="tooltip"
    v-bind="$attrs"
    :aria-label="ariaLabel"
    :icon="icon"
    :loading="updating"
    @click="onToggle"
    v-on="$listeners"
  >
    <!--
      Use <template v-if> to ensure a square button is shown when compact: true.
      Sending empty content will still show a distorted/rectangular button.
    -->
    <template v-if="buttonContent">{{ buttonContent }}</template>
  </gl-button>
</template>