All files / app/assets/javascripts/import_entities/import_projects/components import_projects_table.vue

100% Statements 15/15
100% Branches 2/2
100% Functions 10/10
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227                4x                                                                                                     22x                                   2x       22x 3x     19x 4x         15x       16x           5x         22x 22x       22x 22x                           1x                                                                                                                                                                                                        
<script>
import {
  GlButton,
  GlLoadingIcon,
  GlIntersectionObserver,
  GlModal,
  GlSearchBoxByClick,
} from '@gitlab/ui';
// eslint-disable-next-line no-restricted-imports
import { mapActions, mapState, mapGetters } from 'vuex';
import { n__, __, sprintf } from '~/locale';
 
import ProviderRepoTableRow from './provider_repo_table_row.vue';
import AdvancedSettings from './advanced_settings.vue';
 
export default {
  name: 'ImportProjectsTable',
  components: {
    AdvancedSettings,
    ProviderRepoTableRow,
    GlLoadingIcon,
    GlButton,
    GlModal,
    GlIntersectionObserver,
    GlSearchBoxByClick,
  },
  props: {
    providerTitle: {
      type: String,
      required: true,
    },
    filterable: {
      type: Boolean,
      required: false,
      default: true,
    },
    paginatable: {
      type: Boolean,
      required: false,
      default: false,
    },
    cancelable: {
      type: Boolean,
      required: false,
      default: false,
    },
    optionalStages: {
      type: Array,
      required: false,
      default: () => [],
    },
    isAdvancedSettingsPanelInitiallyExpanded: {
      type: Boolean,
      required: false,
      default: true,
    },
  },
 
  data() {
    return {
      optionalStagesSelection: Object.fromEntries(
        this.optionalStages.map(({ name, selected }) => [name, selected]),
      ),
    };
  },
 
  computed: {
    ...mapState(['filter', 'repositories', 'defaultTargetNamespace', 'pageInfo', 'isLoadingRepos']),
    ...mapGetters([
      'isImportingAnyRepo',
      'importingRepoCount',
      'hasImportableRepos',
      'hasIncompatibleRepos',
      'importAllCount',
    ]),
 
    pagePaginationStateKey() {
      return `${this.filter}-${this.repositories.length}-${this.pageInfo.page}`;
    },
 
    importAllButtonText() {
      if (this.isImportingAnyRepo) {
        return n__('Importing %d repository', 'Importing %d repositories', this.importingRepoCount);
      }
 
      if (this.hasIncompatibleRepos)
        return n__(
          'Import %d compatible repository',
          'Import %d compatible repositories',
          this.importAllCount,
        );
      return n__('Import %d repository', 'Import %d repositories', this.importAllCount);
    },
 
    emptyStateText() {
      return sprintf(__('No %{providerTitle} repositories found'), {
        providerTitle: this.providerTitle,
      });
    },
 
    fromHeaderText() {
      return sprintf(__('From %{providerTitle}'), { providerTitle: this.providerTitle });
    },
  },
 
  mounted() {
    this.fetchJobs();
    this.fetchRepos();
  },
 
  beforeDestroy() {
    this.stopJobsPolling();
    this.clearJobsEtagPoll();
  },
 
  methods: {
    ...mapActions([
      'fetchRepos',
      'fetchJobs',
      'stopJobsPolling',
      'clearJobsEtagPoll',
      'setFilter',
      'importAll',
    ]),
 
    showImportAllModal() {
      this.$refs.importAllModal.show();
    },
  },
};
</script>
 
<template>
  <div>
    <p class="gl-text-gray-900 gl-white-space-nowrap gl-mt-3">
      {{ s__('ImportProjects|Select the repositories you want to import') }}
    </p>
    <template v-if="hasIncompatibleRepos">
      <slot name="incompatible-repos-warning"></slot>
    </template>
    <slot name="filter" v-bind="{ showImportAllModal, importAllButtonText }">
      <div class="gl-display-flex gl-justify-content-space-between gl-flex-wrap gl-mb-5">
        <gl-button
          variant="confirm"
          :loading="isImportingAnyRepo"
          :disabled="!hasImportableRepos"
          type="button"
          @click="showImportAllModal"
          >{{ importAllButtonText }}</gl-button
        >
 
        <slot name="actions"></slot>
        <form v-if="filterable" class="gl-ml-auto" novalidate @submit.prevent>
          <gl-search-box-by-click
            name="filter"
            :placeholder="__('Filter by name')"
            autofocus
            @submit="setFilter({ filter: $event })"
            @clear="setFilter({ filter: '' })"
          />
        </form>
      </div>
    </slot>
    <advanced-settings
      v-if="optionalStages && optionalStages.length"
      v-model="optionalStagesSelection"
      :stages="optionalStages"
      :is-initially-expanded="isAdvancedSettingsPanelInitiallyExpanded"
      class="gl-mb-5"
    />
    <gl-modal
      ref="importAllModal"
      modal-id="import-all-modal"
      :title="s__('ImportProjects|Import repositories')"
      :ok-title="__('Import')"
      @ok="importAll({ optionalStages: optionalStagesSelection })"
    >
      {{
        n__(
          'Are you sure you want to import %d repository?',
          'Are you sure you want to import %d repositories?',
          importAllCount,
        )
      }}
    </gl-modal>
    <div v-if="repositories.length" class="gl-w-full">
      <table class="table gl-table">
        <thead>
          <tr>
            <th class="gl-w-half">
              {{ fromHeaderText }}
            </th>
            <th class="gl-w-half">
              {{ __('To GitLab') }}
            </th>
            <th>
              {{ __('Status') }}
            </th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <template v-for="repo in repositories">
            <provider-repo-table-row
              :key="repo.importSource.providerLink"
              :repo="repo"
              :user-namespace="defaultTargetNamespace"
              :optional-stages="optionalStagesSelection"
              :cancelable="cancelable"
            />
          </template>
        </tbody>
      </table>
    </div>
    <gl-intersection-observer
      v-if="!isLoadingRepos && paginatable && pageInfo.hasNextPage"
      :key="pagePaginationStateKey"
      @appear="fetchRepos"
    />
    <gl-loading-icon v-if="isLoadingRepos" class="gl-mt-7" size="lg" />
 
    <div v-if="!isLoadingRepos && repositories.length === 0" class="gl-text-center">
      <strong>{{ emptyStateText }}</strong>
    </div>
  </div>
</template>