summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/pages/projects/new/components/new_project_url_select.vue
blob: ba8858c985af160742cd4f932ddd642b9288604d (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
<script>
import {
  GlButton,
  GlButtonGroup,
  GlDropdown,
  GlDropdownItem,
  GlDropdownSectionHeader,
  GlLoadingIcon,
  GlSearchBoxByType,
} from '@gitlab/ui';
import { MINIMUM_SEARCH_LENGTH } from '~/graphql_shared/constants';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import Tracking from '~/tracking';
import { DEBOUNCE_DELAY } from '~/vue_shared/components/filtered_search_bar/constants';
import searchNamespacesWhereUserCanCreateProjectsQuery from '../queries/search_namespaces_where_user_can_create_projects.query.graphql';

export default {
  components: {
    GlButton,
    GlButtonGroup,
    GlDropdown,
    GlDropdownItem,
    GlDropdownSectionHeader,
    GlLoadingIcon,
    GlSearchBoxByType,
  },
  mixins: [Tracking.mixin()],
  apollo: {
    currentUser: {
      query: searchNamespacesWhereUserCanCreateProjectsQuery,
      variables() {
        return {
          search: this.search,
        };
      },
      skip() {
        return this.search.length > 0 && this.search.length < MINIMUM_SEARCH_LENGTH;
      },
      debounce: DEBOUNCE_DELAY,
    },
  },
  inject: ['namespaceFullPath', 'namespaceId', 'rootUrl', 'trackLabel'],
  data() {
    return {
      currentUser: {},
      search: '',
      selectedNamespace: {
        id: this.namespaceId,
        fullPath: this.namespaceFullPath,
      },
    };
  },
  computed: {
    userGroups() {
      return this.currentUser.groups?.nodes || [];
    },
    userNamespace() {
      return this.currentUser.namespace || {};
    },
  },
  methods: {
    handleClick({ id, fullPath }) {
      this.selectedNamespace = {
        id: getIdFromGraphQLId(id),
        fullPath,
      };
    },
  },
};
</script>

<template>
  <gl-button-group class="gl-w-full">
    <gl-button label>{{ rootUrl }}</gl-button>
    <gl-dropdown
      class="gl-w-full"
      :text="selectedNamespace.fullPath"
      toggle-class="gl-rounded-top-right-base! gl-rounded-bottom-right-base!"
      data-qa-selector="select_namespace_dropdown"
      @show="track('activate_form_input', { label: trackLabel, property: 'project_path' })"
    >
      <gl-search-box-by-type v-model.trim="search" />
      <gl-loading-icon v-if="$apollo.queries.currentUser.loading" />
      <template v-else>
        <gl-dropdown-section-header>{{ __('Groups') }}</gl-dropdown-section-header>
        <gl-dropdown-item v-for="group of userGroups" :key="group.id" @click="handleClick(group)">
          {{ group.fullPath }}
        </gl-dropdown-item>
        <gl-dropdown-section-header>{{ __('Users') }}</gl-dropdown-section-header>
        <gl-dropdown-item @click="handleClick(userNamespace)">
          {{ userNamespace.fullPath }}
        </gl-dropdown-item>
      </template>
    </gl-dropdown>

    <input type="hidden" name="project[namespace_id]" :value="selectedNamespace.id" />
  </gl-button-group>
</template>