summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/import/details/components/import_details_table.vue
blob: 813dc1f2645c9ac19cd6b4bf7245b6fb27c62757 (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
<script>
import { GlEmptyState, GlIcon, GlLink, GlLoadingIcon, GlTable } from '@gitlab/ui';
import { __, s__ } from '~/locale';
import { createAlert } from '~/alert';
import { parseIntPagination, normalizeHeaders } from '~/lib/utils/common_utils';
import { getParameterValues } from '~/lib/utils/url_utility';

import PaginationBar from '~/vue_shared/components/pagination_bar/pagination_bar.vue';
import { STATISTIC_ITEMS } from '../../constants';
import { fetchImportFailures } from '../api';

const DEFAULT_PAGE_SIZE = 20;

export default {
  components: {
    GlEmptyState,
    GlIcon,
    GlLink,
    GlLoadingIcon,
    GlTable,
    PaginationBar,
  },
  STATISTIC_ITEMS,
  LOCAL_STORAGE_KEY: 'gl-import-details-page-size',
  fields: [
    {
      key: 'type',
      label: __('Type'),
      tdClass: 'gl-white-space-nowrap',
    },
    {
      key: 'title',
      label: __('Title'),
      tdClass: 'gl-md-w-30 gl-word-break-word',
    },
    {
      key: 'provider_url',
      label: __('URL'),
      tdClass: 'gl-white-space-nowrap',
    },
    {
      key: 'details',
      label: __('Details'),
    },
  ],

  i18n: {
    fetchErrorMessage: s__('Import|An error occurred while fetching import details.'),
    emptyText: s__('Import|No import details'),
  },

  inject: {
    failuresPath: {
      default: undefined,
    },
  },

  data() {
    return {
      items: [],
      loading: false,
      page: 1,
      perPage: DEFAULT_PAGE_SIZE,
      totalPages: 0,
      total: 0,
    };
  },

  computed: {
    hasItems() {
      return this.items.length > 0;
    },

    pageInfo() {
      return {
        page: this.page,
        perPage: this.perPage,
        totalPages: this.totalPages,
        total: this.total,
      };
    },
  },

  mounted() {
    this.loadImportFailures();
  },

  methods: {
    setPage(page) {
      this.page = page;
      this.loadImportFailures();
    },

    setPageSize(size) {
      this.perPage = size;
      this.page = 1;
      this.loadImportFailures();
    },

    async loadImportFailures() {
      if (!this.failuresPath) {
        return;
      }

      this.loading = true;
      try {
        const response = await fetchImportFailures(this.failuresPath, {
          projectId: getParameterValues('project_id')[0],
          page: this.page,
          perPage: this.perPage,
        });

        const { page, perPage, totalPages, total } = parseIntPagination(
          normalizeHeaders(response.headers),
        );
        this.page = page;
        this.perPage = perPage;
        this.totalPages = totalPages;
        this.total = total;
        this.items = response.data;
      } catch (error) {
        createAlert({ message: this.$options.i18n.fetchErrorMessage });
      }
      this.loading = false;
    },
  },
};
</script>

<template>
  <div>
    <gl-table :fields="$options.fields" :items="items" class="gl-mt-5" :busy="loading" show-empty>
      <template #table-busy>
        <gl-loading-icon size="lg" class="gl-my-5" />
      </template>

      <template #empty>
        <gl-empty-state :title="$options.i18n.emptyText" />
      </template>

      <template #cell(type)="{ item: { type } }">
        {{ $options.STATISTIC_ITEMS[type] }}
      </template>
      <template #cell(provider_url)="{ item: { provider_url } }">
        <gl-link v-if="provider_url" :href="provider_url" target="_blank">
          {{ provider_url }}
          <gl-icon name="external-link" />
        </gl-link>
      </template>
    </gl-table>
    <pagination-bar
      v-if="hasItems"
      :page-info="pageInfo"
      class="gl-mt-5"
      :storage-key="$options.LOCAL_STORAGE_KEY"
      @set-page="setPage"
      @set-page-size="setPageSize"
    />
  </div>
</template>