summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/deploy_keys/components/app.vue
blob: fe04644905405f8c2d26b040bd3ac42c589a3046 (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
<script>
  import Flash from '../../flash';
  import eventHub from '../eventhub';
  import DeployKeysService from '../service';
  import DeployKeysStore from '../store';
  import keysPanel from './keys_panel.vue';
  import loadingIcon from '../../vue_shared/components/loading_icon.vue';

  export default {
    data() {
      return {
        isLoading: false,
        store: new DeployKeysStore(),
      };
    },
    props: {
      endpoint: {
        type: String,
        required: true,
      },
    },
    computed: {
      hasKeys() {
        return Object.keys(this.keys).length;
      },
      keys() {
        return this.store.keys;
      },
    },
    components: {
      keysPanel,
      loadingIcon,
    },
    methods: {
      fetchKeys() {
        this.isLoading = true;

        this.service.getKeys()
          .then((data) => {
            this.isLoading = false;
            this.store.keys = data;
          })
          .catch(() => new Flash('Error getting deploy keys'));
      },
      enableKey(deployKey) {
        this.service.enableKey(deployKey.id)
          .then(() => this.fetchKeys())
          .catch(() => new Flash('Error enabling deploy key'));
      },
      disableKey(deployKey, callback) {
        // eslint-disable-next-line no-alert
        if (confirm('You are going to remove this deploy key. Are you sure?')) {
          this.service.disableKey(deployKey.id)
            .then(() => this.fetchKeys())
            .then(callback)
            .catch(() => new Flash('Error removing deploy key'));
        } else {
          callback();
        }
      },
    },
    created() {
      this.service = new DeployKeysService(this.endpoint);

      eventHub.$on('enable.key', this.enableKey);
      eventHub.$on('remove.key', this.disableKey);
      eventHub.$on('disable.key', this.disableKey);
    },
    mounted() {
      this.fetchKeys();
    },
    beforeDestroy() {
      eventHub.$off('enable.key', this.enableKey);
      eventHub.$off('remove.key', this.disableKey);
      eventHub.$off('disable.key', this.disableKey);
    },
  };
</script>

<template>
  <div class="append-bottom-default deploy-keys">
    <loading-icon
      v-if="isLoading && !hasKeys"
      size="2"
      label="Loading deploy keys"
    />
    <div v-else-if="hasKeys">
      <keys-panel
        title="Enabled deploy keys for this project"
        :keys="keys.enabled_keys"
        :store="store"
        :endpoint="endpoint"
      />
      <keys-panel
        title="Deploy keys from projects you have access to"
        :keys="keys.available_project_keys"
        :store="store"
        :endpoint="endpoint"
      />
      <keys-panel
        v-if="keys.public_keys.length"
        title="Public deploy keys available to any project"
        :keys="keys.public_keys"
        :store="store"
        :endpoint="endpoint"
      />
    </div>
  </div>
</template>