summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/clusters_list/components/agent_options.vue
blob: a364122ba5626b0cbc6e2d084be5104513b931f5 (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
<script>
import {
  GlDropdown,
  GlDropdownItem,
  GlModal,
  GlModalDirective,
  GlSprintf,
  GlFormGroup,
  GlFormInput,
} from '@gitlab/ui';
import { s__, __, sprintf } from '~/locale';
import { DELETE_AGENT_MODAL_ID } from '../constants';
import deleteAgent from '../graphql/mutations/delete_agent.mutation.graphql';
import getAgentsQuery from '../graphql/queries/get_agents.query.graphql';
import { removeAgentFromStore } from '../graphql/cache_update';

export default {
  i18n: {
    dropdownText: __('More options'),
    deleteButton: s__('ClusterAgents|Delete agent'),
    modalTitle: __('Are you sure?'),
    modalBody: s__(
      'ClusterAgents|Are you sure you want to delete this agent? You cannot undo this.',
    ),
    modalInputLabel: s__('ClusterAgents|To delete the agent, type %{name} to confirm:'),
    modalAction: s__('ClusterAgents|Delete'),
    modalCancel: __('Cancel'),
    successMessage: s__('ClusterAgents|%{name} successfully deleted'),
    defaultError: __('An error occurred. Please try again.'),
  },
  components: {
    GlDropdown,
    GlDropdownItem,
    GlModal,
    GlSprintf,
    GlFormGroup,
    GlFormInput,
  },
  directives: {
    GlModalDirective,
  },
  inject: ['projectPath'],
  props: {
    agent: {
      required: true,
      type: Object,
      validator: (value) => ['id', 'name'].every((prop) => value[prop]),
    },
    defaultBranchName: {
      default: '.noBranch',
      required: false,
      type: String,
    },
    maxAgents: {
      default: null,
      required: false,
      type: Number,
    },
  },
  data() {
    return {
      loading: false,
      error: null,
      deleteConfirmText: null,
      agentName: this.agent.name,
    };
  },
  computed: {
    getAgentsQueryVariables() {
      return {
        defaultBranchName: this.defaultBranchName,
        first: this.maxAgents,
        last: null,
        projectPath: this.projectPath,
      };
    },
    modalId() {
      return sprintf(DELETE_AGENT_MODAL_ID, {
        agentName: this.agent.name,
      });
    },
    primaryModalProps() {
      return {
        text: this.$options.i18n.modalAction,
        attributes: [
          { disabled: this.loading || this.disableModalSubmit, loading: this.loading },
          { variant: 'danger' },
        ],
      };
    },
    cancelModalProps() {
      return {
        text: this.$options.i18n.modalCancel,
        attributes: [],
      };
    },
    disableModalSubmit() {
      return this.deleteConfirmText !== this.agent.name;
    },
  },
  methods: {
    async deleteAgent() {
      if (this.disableModalSubmit || this.loading) {
        return;
      }

      this.loading = true;
      this.error = null;

      try {
        const { errors } = await this.deleteAgentMutation();

        if (errors.length) {
          throw new Error(errors[0]);
        }
      } catch (error) {
        this.error = error?.message || this.$options.i18n.defaultError;
      } finally {
        this.loading = false;
        const successMessage = sprintf(this.$options.i18n.successMessage, { name: this.agentName });

        this.$toast.show(this.error || successMessage);

        this.$refs.modal.hide();
      }
    },
    deleteAgentMutation() {
      return this.$apollo
        .mutate({
          mutation: deleteAgent,
          variables: {
            input: {
              id: this.agent.id,
            },
          },
          update: (store) => {
            const deleteClusterAgent = this.agent;
            removeAgentFromStore(
              store,
              deleteClusterAgent,
              getAgentsQuery,
              this.getAgentsQueryVariables,
            );
          },
        })

        .then(({ data: { clusterAgentDelete } }) => {
          return clusterAgentDelete;
        });
    },
    hideModal() {
      this.loading = false;
      this.error = null;
      this.deleteConfirmText = null;
    },
  },
};
</script>

<template>
  <div>
    <gl-dropdown
      icon="ellipsis_v"
      right
      :disabled="loading"
      :text="$options.i18n.dropdownText"
      text-sr-only
      category="tertiary"
      no-caret
    >
      <gl-dropdown-item v-gl-modal-directive="modalId">
        {{ $options.i18n.deleteButton }}
      </gl-dropdown-item>
    </gl-dropdown>

    <gl-modal
      ref="modal"
      :modal-id="modalId"
      :title="$options.i18n.modalTitle"
      :action-primary="primaryModalProps"
      :action-cancel="cancelModalProps"
      size="sm"
      @primary="deleteAgent"
      @hide="hideModal"
    >
      <p>{{ $options.i18n.modalBody }}</p>

      <gl-form-group>
        <template #label>
          <gl-sprintf :message="$options.i18n.modalInputLabel">
            <template #name>
              <code>{{ agent.name }}</code>
            </template>
          </gl-sprintf>
        </template>
        <gl-form-input v-model="deleteConfirmText" @keydown.enter="deleteAgent" />
      </gl-form-group>
    </gl-modal>
  </div>
</template>