Welcome to mirror list, hosted at ThFree Co, Russian Federation.

runner_bulk_delete.vue « components « runner « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 703da01d9c8bed37773b201402bf4a44ad98e0b6 (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
<script>
import { GlButton, GlModalDirective, GlModal, GlSprintf } from '@gitlab/ui';
import { createAlert } from '~/flash';
import { __, s__, n__, sprintf } from '~/locale';
import checkedRunnerIdsQuery from '../graphql/list/checked_runner_ids.query.graphql';
import BulkRunnerDelete from '../graphql/list/bulk_runner_delete.mutation.graphql';
import { RUNNER_TYPENAME } from '../constants';

export default {
  components: {
    GlButton,
    GlModal,
    GlSprintf,
  },
  directives: {
    GlModal: GlModalDirective,
  },
  inject: ['localMutations'],
  props: {
    runners: {
      type: Array,
      default: () => [],
      required: false,
    },
  },
  data() {
    return {
      isDeleting: false,
      checkedRunnerIds: [],
    };
  },
  apollo: {
    checkedRunnerIds: {
      query: checkedRunnerIdsQuery,
    },
  },
  computed: {
    currentCheckedRunnerIds() {
      return this.runners
        .map(({ id }) => id)
        .filter((id) => this.checkedRunnerIds.indexOf(id) >= 0);
    },
    checkedCount() {
      return this.currentCheckedRunnerIds.length || 0;
    },
    bannerMessage() {
      return sprintf(
        n__(
          'Runners|%{strongStart}%{count}%{strongEnd} runner selected',
          'Runners|%{strongStart}%{count}%{strongEnd} runners selected',
          this.checkedCount,
        ),
        {
          count: this.checkedCount,
        },
      );
    },
    modalTitle() {
      return n__('Runners|Delete %d runner', 'Runners|Delete %d runners', this.checkedCount);
    },
    modalActionPrimary() {
      return {
        text: n__(
          'Runners|Permanently delete %d runner',
          'Runners|Permanently delete %d runners',
          this.checkedCount,
        ),
        attributes: {
          loading: this.isDeleting,
          variant: 'danger',
        },
      };
    },
    modalActionCancel() {
      return {
        text: __('Cancel'),
        attributes: {
          loading: this.isDeleting,
        },
      };
    },
    modalMessage() {
      return sprintf(
        n__(
          'Runners|%{strongStart}%{count}%{strongEnd} runner will be permanently deleted and no longer available for projects or groups in the instance. Are you sure you want to continue?',
          'Runners|%{strongStart}%{count}%{strongEnd} runners will be permanently deleted and no longer available for projects or groups in the instance. Are you sure you want to continue?',
          this.checkedCount,
        ),
        { count: this.checkedCount },
      );
    },
  },
  methods: {
    toastConfirmationMessage(deletedCount) {
      return n__(
        'Runners|%d selected runner deleted',
        'Runners|%d selected runners deleted',
        deletedCount,
      );
    },
    onClearChecked() {
      this.localMutations.clearChecked();
    },
    async onConfirmDelete(e) {
      this.isDeleting = true;
      e.preventDefault(); // don't close modal until deletion is complete

      try {
        await this.$apollo.mutate({
          mutation: BulkRunnerDelete,
          variables: {
            input: {
              ids: this.currentCheckedRunnerIds,
            },
          },
          update: (cache, { data }) => {
            const { errors, deletedIds } = data.bulkRunnerDelete;

            if (errors?.length) {
              this.onError(new Error(errors.join(' ')));
              this.$refs.modal.hide();
              return;
            }

            this.$emit('deleted', {
              message: this.toastConfirmationMessage(deletedIds.length),
            });

            // Clean up

            // Remove deleted runners from the cache
            deletedIds.forEach((id) => {
              const cacheId = cache.identify({ __typename: RUNNER_TYPENAME, id });
              cache.evict({ id: cacheId });
            });
            cache.gc();

            this.$refs.modal.hide();
          },
        });
      } catch (error) {
        this.onError(error);
      } finally {
        this.isDeleting = false;
      }
    },
    onError(error) {
      createAlert({
        message: s__(
          'Runners|Something went wrong while deleting. Please refresh the page to try again.',
        ),
        captureError: true,
        error,
      });
    },
  },
  BULK_DELETE_MODAL_ID: 'bulk-delete-modal',
};
</script>

<template>
  <div v-if="checkedCount" class="gl-my-4 gl-p-4 gl-border-1 gl-border-solid gl-border-gray-100">
    <div class="gl-display-flex gl-align-items-center">
      <div>
        <gl-sprintf :message="bannerMessage">
          <template #strong="{ content }">
            <strong>{{ content }}</strong>
          </template>
        </gl-sprintf>
      </div>
      <div class="gl-ml-auto">
        <gl-button variant="default" @click="onClearChecked">{{
          s__('Runners|Clear selection')
        }}</gl-button>
        <gl-button v-gl-modal="$options.BULK_DELETE_MODAL_ID" variant="danger">{{
          s__('Runners|Delete selected')
        }}</gl-button>
      </div>
    </div>
    <gl-modal
      ref="modal"
      size="sm"
      :modal-id="$options.BULK_DELETE_MODAL_ID"
      :title="modalTitle"
      :action-primary="modalActionPrimary"
      :action-cancel="modalActionCancel"
      @primary="onConfirmDelete"
    >
      <gl-sprintf :message="modalMessage">
        <template #strong="{ content }">
          <strong>{{ content }}</strong>
        </template>
      </gl-sprintf>
    </gl-modal>
  </div>
</template>