summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/environments/environment_details/index.vue
blob: b43f4233b9c5b8f05b2da0209ab05f3bc080a909 (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
<script>
import { GlLoadingIcon } from '@gitlab/ui';
import { logError } from '~/lib/logger';
import environmentDetailsQuery from '../graphql/queries/environment_details.query.graphql';
import { convertToDeploymentTableRow } from '../helpers/deployment_data_transformation_helper';
import EmptyState from './empty_state.vue';
import DeploymentsTable from './deployments_table.vue';
import Pagination from './pagination.vue';
import { ENVIRONMENT_DETAILS_PAGE_SIZE } from './constants';

export default {
  components: {
    Pagination,
    DeploymentsTable,
    EmptyState,
    GlLoadingIcon,
  },
  props: {
    projectFullPath: {
      type: String,
      required: true,
    },
    environmentName: {
      type: String,
      required: true,
    },
    after: {
      type: String,
      required: false,
      default: null,
    },
    before: {
      type: String,
      required: false,
      default: null,
    },
  },
  apollo: {
    project: {
      query: environmentDetailsQuery,
      variables() {
        return {
          projectFullPath: this.projectFullPath,
          environmentName: this.environmentName,
          first: this.before ? null : ENVIRONMENT_DETAILS_PAGE_SIZE,
          last: this.before ? ENVIRONMENT_DETAILS_PAGE_SIZE : null,
          after: this.after,
          before: this.before,
        };
      },
    },
  },
  data() {
    return {
      project: {},
      isInitialPageDataReceived: false,
      isPrefetchingPages: false,
    };
  },
  computed: {
    deployments() {
      return this.project.environment?.deployments.nodes.map(convertToDeploymentTableRow) || [];
    },
    isLoading() {
      return this.$apollo.queries.project.loading;
    },
    isDeploymentTableShown() {
      return this.isInitialPageDataReceived === true && this.deployments.length > 0;
    },
    pageInfo() {
      return this.project.environment?.deployments.pageInfo || {};
    },
    isPaginationDisabled() {
      return this.isLoading || this.isPrefetchingPages;
    },
  },
  watch: {
    async project(newProject) {
      this.isInitialPageDataReceived = true;
      this.isPrefetchingPages = true;

      try {
        // TLDR: when we load a page, if there's next and/or previous pages existing, we'll load their data as well to improve percepted performance.
        const {
          endCursor,
          hasPreviousPage,
          hasNextPage,
          startCursor,
        } = newProject.environment.deployments.pageInfo;

        // At the moment we have a limit of deployments being requested only from a signle environment entity per query,
        // and apparently two batched queries count as one on server-side
        // to load both next and previous page data, we have to query them sequentially
        if (hasNextPage) {
          await this.$apollo.query({
            query: environmentDetailsQuery,
            variables: {
              projectFullPath: this.projectFullPath,
              environmentName: this.environmentName,
              first: ENVIRONMENT_DETAILS_PAGE_SIZE,
              after: endCursor,
              before: null,
              last: null,
            },
          });
        }

        if (hasPreviousPage) {
          await this.$apollo.query({
            query: environmentDetailsQuery,
            variables: {
              projectFullPath: this.projectFullPath,
              environmentName: this.environmentName,
              first: null,
              after: null,
              before: startCursor,
              last: ENVIRONMENT_DETAILS_PAGE_SIZE,
            },
          });
        }
      } catch (error) {
        logError(error);
      }

      this.isPrefetchingPages = false;
    },
  },
};
</script>
<template>
  <div class="gl-relative gl-min-h-6">
    <div
      v-if="isLoading"
      class="gl-absolute gl-top-0 gl-left-0 gl-w-full gl-h-full gl-z-index-200 gl-bg-gray-10 gl-opacity-3"
    ></div>
    <gl-loading-icon v-if="isLoading" size="lg" class="gl-absolute gl-top-half gl-left-50p" />
    <div v-if="isDeploymentTableShown">
      <deployments-table :deployments="deployments" />
      <pagination :page-info="pageInfo" :disabled="isPaginationDisabled" />
    </div>
    <empty-state v-if="!isDeploymentTableShown && !isLoading" />
  </div>
</template>