Welcome to mirror list, hosted at ThFree Co, Russian Federation.

new_issue_dropdown.vue « new_issue_dropdown « components « vue_shared « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b787cee3f01d26fa395ffb74dc47733b765ec665 (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
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
<script>
import {
  GlDropdown,
  GlDropdownItem,
  GlDropdownText,
  GlLoadingIcon,
  GlSearchBoxByType,
} from '@gitlab/ui';
import { createAlert } from '~/flash';
import { DASH_SCOPE, joinPaths } from '~/lib/utils/url_utility';
import { __, sprintf } from '~/locale';
import { DEBOUNCE_DELAY } from '~/vue_shared/components/filtered_search_bar/constants';
import AccessorUtilities from '~/lib/utils/accessor';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';
import searchUserProjects from './graphql/search_user_projects.query.graphql';

export default {
  i18n: {
    defaultDropdownText: __('Select project to create issue'),
    noMatchesFound: __('No matches found'),
    toggleButtonLabel: __('Toggle project select'),
  },
  components: {
    GlDropdown,
    GlDropdownItem,
    GlDropdownText,
    GlLoadingIcon,
    GlSearchBoxByType,
    LocalStorageSync,
  },
  props: {
    query: {
      type: Object,
      required: false,
      default: () => searchUserProjects,
    },
    queryVariables: {
      type: Object,
      required: false,
      default: () => ({}),
    },
    extractProjects: {
      type: Function,
      required: false,
      default: (data) => data?.projects?.nodes,
    },
    withLocalStorage: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      projects: [],
      search: '',
      selectedProject: {},
      shouldSkipQuery: true,
    };
  },
  apollo: {
    projects: {
      query() {
        return this.query;
      },
      variables() {
        return {
          search: this.search,
          ...this.queryVariables,
        };
      },
      update(data) {
        return this.extractProjects(data) || [];
      },
      error(error) {
        createAlert({
          message: __('An error occurred while loading projects.'),
          captureError: true,
          error,
        });
      },
      skip() {
        return this.shouldSkipQuery;
      },
      debounce: DEBOUNCE_DELAY,
    },
  },
  computed: {
    dropdownHref() {
      return this.hasSelectedProject
        ? joinPaths(this.selectedProject.webUrl, DASH_SCOPE, 'issues/new')
        : undefined;
    },
    dropdownText() {
      return this.hasSelectedProject
        ? sprintf(__('New issue in %{project}'), { project: this.selectedProject.name })
        : this.$options.i18n.defaultDropdownText;
    },
    hasSelectedProject() {
      return this.selectedProject.webUrl;
    },
    projectsWithIssuesEnabled() {
      return this.projects.filter((project) => project.issuesEnabled);
    },
    showNoSearchResultsText() {
      return !this.projectsWithIssuesEnabled.length && this.search;
    },
    canUseLocalStorage() {
      return this.withLocalStorage && AccessorUtilities.canUseLocalStorage();
    },
    selectedProjectForLocalStorage() {
      const { webUrl, name } = this.selectedProject;

      return { webUrl, name };
    },
  },
  methods: {
    handleDropdownClick() {
      if (!this.dropdownHref) {
        this.$refs.dropdown.show();
      }
    },
    handleDropdownShown() {
      if (this.shouldSkipQuery) {
        this.shouldSkipQuery = false;
      }
      this.$refs.search.focusInput();
    },
    selectProject(project) {
      this.selectedProject = project;
    },
    initFromLocalStorage(storedProject) {
      // Historically, the selected project was saved with the URL as the `url` property, so we are
      // falling back to that legacy property if `webUrl` is empty. This ensures that we support
      // localStorage data that was persisted prior to this change.
      let webUrl = storedProject.webUrl || storedProject.url;

      // The select2 implementation used to include the resource path in the local storage. We
      // need to clean this up so that we can then re-build a fresh URL in the computed prop.
      const path = 'issues/new';
      webUrl = webUrl.endsWith(path) ? webUrl.slice(0, webUrl.length - path.length) : webUrl;

      this.selectedProject = { webUrl, name: storedProject.name };
    },
  },
  // This key is hardcoded for now as we'll only be using the localStorage capability in the
  // instance-level issues dashboard. If we want to make this feature available in the groups'
  // issues lists, we should make this key dynamic.
  localStorageKey: 'group--new-issue-recent-project',
};
</script>

<template>
  <local-storage-sync
    :storage-key="$options.localStorageKey"
    :value="selectedProjectForLocalStorage"
    @input="initFromLocalStorage"
  >
    <gl-dropdown
      ref="dropdown"
      right
      split
      :split-href="dropdownHref"
      :text="dropdownText"
      :toggle-text="$options.i18n.toggleButtonLabel"
      variant="confirm"
      data-testid="new-resource-dropdown"
      @click="handleDropdownClick"
      @shown="handleDropdownShown"
    >
      <gl-search-box-by-type ref="search" v-model.trim="search" />
      <gl-loading-icon v-if="$apollo.queries.projects.loading" />
      <template v-else>
        <gl-dropdown-item
          v-for="project of projectsWithIssuesEnabled"
          :key="project.id"
          @click="selectProject(project)"
        >
          {{ project.nameWithNamespace }}
        </gl-dropdown-item>
        <gl-dropdown-text v-if="showNoSearchResultsText">
          {{ $options.i18n.noMatchesFound }}
        </gl-dropdown-text>
      </template>
    </gl-dropdown>
  </local-storage-sync>
</template>