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

app.vue « components « deploy_keys « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec17bbea48f28d9e00ad4afafd8083ba30a183dd (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
<script>
import { GlButton, GlIcon, GlLoadingIcon } from '@gitlab/ui';
import { createAlert } from '~/alert';
import { s__ } from '~/locale';
import NavigationTabs from '~/vue_shared/components/navigation_tabs.vue';
import eventHub from '../eventhub';
import DeployKeysService from '../service';
import DeployKeysStore from '../store';
import ConfirmModal from './confirm_modal.vue';
import KeysPanel from './keys_panel.vue';

export default {
  components: {
    ConfirmModal,
    KeysPanel,
    NavigationTabs,
    GlButton,
    GlIcon,
    GlLoadingIcon,
  },
  props: {
    endpoint: {
      type: String,
      required: true,
    },
    projectId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      currentTab: 'enabled_keys',
      isLoading: false,
      store: new DeployKeysStore(),
      removeKey: () => {},
      cancel: () => {},
      confirmModalVisible: false,
    };
  },
  scopes: {
    enabled_keys: s__('DeployKeys|Enabled deploy keys'),
    available_project_keys: s__('DeployKeys|Privately accessible deploy keys'),
    public_keys: s__('DeployKeys|Publicly accessible deploy keys'),
  },
  i18n: {
    loading: s__('DeployKeys|Loading deploy keys'),
    addButton: s__('DeployKeys|Add new key'),
  },
  computed: {
    tabs() {
      return Object.keys(this.$options.scopes).map((scope) => {
        const count = Array.isArray(this.keys[scope]) ? this.keys[scope].length : null;

        return {
          name: this.$options.scopes[scope],
          scope,
          isActive: scope === this.currentTab,
          count,
        };
      });
    },
    hasKeys() {
      return Object.keys(this.keys).length;
    },
    keys() {
      return this.store.keys;
    },
  },
  created() {
    this.service = new DeployKeysService(this.endpoint);

    eventHub.$on('enable.key', this.enableKey);
    eventHub.$on('remove.key', this.confirmRemoveKey);
    eventHub.$on('disable.key', this.confirmRemoveKey);
  },
  mounted() {
    this.fetchKeys();
  },
  beforeDestroy() {
    eventHub.$off('enable.key', this.enableKey);
    eventHub.$off('remove.key', this.confirmRemoveKey);
    eventHub.$off('disable.key', this.confirmRemoveKey);
  },
  methods: {
    onChangeTab(tab) {
      this.currentTab = tab;
    },
    fetchKeys() {
      this.isLoading = true;

      return this.service
        .getKeys()
        .then((data) => {
          this.isLoading = false;
          this.store.keys = data;
        })
        .catch(() => {
          this.isLoading = false;
          this.store.keys = {};
          return createAlert({
            message: s__('DeployKeys|Error getting deploy keys'),
          });
        });
    },
    enableKey(deployKey) {
      this.service
        .enableKey(deployKey.id)
        .then(this.fetchKeys)
        .catch(() =>
          createAlert({
            message: s__('DeployKeys|Error enabling deploy key'),
          }),
        );
    },
    confirmRemoveKey(deployKey, callback) {
      const hideModal = () => {
        this.confirmModalVisible = false;
        callback?.();
      };
      this.removeKey = () => {
        this.service
          .disableKey(deployKey.id)
          .then(this.fetchKeys)
          .then(hideModal)
          .catch(() =>
            createAlert({
              message: s__('DeployKeys|Error removing deploy key'),
            }),
          );
      };
      this.cancel = hideModal;
      this.confirmModalVisible = true;
    },
  },
};
</script>

<template>
  <div class="deploy-keys">
    <confirm-modal :visible="confirmModalVisible" @remove="removeKey" @cancel="cancel" />
    <gl-loading-icon
      v-if="isLoading && !hasKeys"
      :label="$options.i18n.loading"
      size="sm"
      class="gl-m-5"
    />
    <template v-else-if="hasKeys">
      <div class="gl-new-card-header gl-align-items-center gl-pt-0 gl-pb-0 gl-pl-0">
        <div class="top-area scrolling-tabs-container inner-page-scroll-tabs gl-border-b-0">
          <div class="fade-left">
            <gl-icon name="chevron-lg-left" :size="12" />
          </div>
          <div class="fade-right">
            <gl-icon name="chevron-lg-right" :size="12" />
          </div>

          <navigation-tabs
            :tabs="tabs"
            scope="deployKeys"
            class="gl-rounded-lg"
            @onChangeTab="onChangeTab"
          />
        </div>

        <div class="gl-new-card-actions">
          <gl-button
            size="small"
            class="js-toggle-button js-toggle-content"
            data-testid="add-new-deploy-key-button"
          >
            {{ $options.i18n.addButton }}
          </gl-button>
        </div>
      </div>
      <keys-panel
        :project-id="projectId"
        :keys="keys[currentTab]"
        :store="store"
        :endpoint="endpoint"
        data-testid="project-deploy-keys-container"
      />
    </template>
  </div>
</template>