debian-mirror-gitlab/app/assets/javascripts/runner/components/runner_pause_button.vue

119 lines
2.6 KiB
Vue
Raw Normal View History

2022-04-04 11:22:00 +05:30
<script>
import { GlButton, GlTooltipDirective } from '@gitlab/ui';
2022-05-07 20:08:51 +05:30
import runnerToggleActiveMutation from '~/runner/graphql/shared/runner_toggle_active.mutation.graphql';
2022-04-04 11:22:00 +05:30
import { createAlert } from '~/flash';
import { captureException } from '~/runner/sentry_utils';
2022-05-07 20:08:51 +05:30
import { I18N_PAUSE, I18N_PAUSE_TOOLTIP, I18N_RESUME, I18N_RESUME_TOOLTIP } from '../constants';
2022-04-04 11:22:00 +05:30
export default {
name: 'RunnerPauseButton',
components: {
GlButton,
},
directives: {
GlTooltip: GlTooltipDirective,
},
props: {
runner: {
type: Object,
required: true,
},
compact: {
type: Boolean,
required: false,
default: false,
},
},
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() {
2022-05-07 20:08:51 +05:30
// 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;
2022-04-04 11:22:00 +05:30
}
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(' '));
}
} catch (e) {
this.onError(e);
} finally {
this.updating = false;
}
},
onError(error) {
const { message } = error;
2022-05-07 20:08:51 +05:30
createAlert({ message });
2022-04-04 11:22:00 +05:30
captureException({ error, component: this.$options.name });
},
},
};
</script>
<template>
<gl-button
2022-05-07 20:08:51 +05:30
v-gl-tooltip="tooltip"
2022-04-04 11:22:00 +05:30
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>