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

organization_switcher.vue « components « super_sidebar « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7122f147d3e2c4f60a5e92f7f2b11f6abc5f1525 (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
<script>
import { GlDisclosureDropdown, GlAvatar, GlIcon, GlLoadingIcon } from '@gitlab/ui';
import getCurrentUserOrganizations from '~/organizations/shared/graphql/queries/organizations.query.graphql';
import { AVATAR_SHAPE_OPTION_RECT } from '~/vue_shared/constants';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import { defaultOrganization } from '~/organizations/mock_data';
import { s__ } from '~/locale';

export default {
  AVATAR_SHAPE_OPTION_RECT,
  ITEM_LOADING: {
    id: 'loading',
    text: 'loading',
    extraAttrs: { disabled: true, class: 'gl-shadow-none!' },
  },
  ITEM_EMPTY: {
    id: 'empty',
    text: s__('Organization|No organizations available to switch to.'),
    extraAttrs: { disabled: true, class: 'gl-shadow-none! gl-text-secondary' },
  },
  i18n: {
    currentOrganization: s__('Organization|Current organization'),
    switchOrganizations: s__('Organization|Switch organizations'),
  },
  components: { GlDisclosureDropdown, GlAvatar, GlIcon, GlLoadingIcon },
  data() {
    return {
      organizations: {},
      dropdownShown: false,
    };
  },
  apollo: {
    organizations: {
      query: getCurrentUserOrganizations,
      update(data) {
        return data.currentUser.organizations;
      },
      skip() {
        return !this.dropdownShown;
      },
      error() {
        this.organizations = {
          nodes: [],
          pageInfo: {},
        };
      },
    },
  },
  computed: {
    loading() {
      return this.$apollo.queries.organizations.loading;
    },
    currentOrganization() {
      // TODO - use `gon.current_organization` when backend supports it.
      // https://gitlab.com/gitlab-org/gitlab/-/issues/437095
      return defaultOrganization;
    },
    nodes() {
      return this.organizations.nodes || [];
    },
    items() {
      const currentOrganizationGroup = {
        name: this.$options.i18n.currentOrganization,
        items: [
          {
            id: this.currentOrganization.id,
            text: this.currentOrganization.name,
            href: this.currentOrganization.web_url,
            avatarUrl: this.currentOrganization.avatar_url,
          },
        ],
      };

      if (this.loading || !this.dropdownShown) {
        return [
          currentOrganizationGroup,
          {
            name: this.$options.i18n.switchOrganizations,
            items: [this.$options.ITEM_LOADING],
          },
        ];
      }

      const items = this.nodes
        .map((node) => ({
          id: getIdFromGraphQLId(node.id),
          text: node.name,
          href: node.webUrl,
          avatarUrl: node.avatarUrl,
        }))
        .filter((item) => item.id !== this.currentOrganization.id);

      return [
        currentOrganizationGroup,
        {
          name: this.$options.i18n.switchOrganizations,
          items: items.length ? items : [this.$options.ITEM_EMPTY],
        },
      ];
    },
  },
  methods: {
    onShown() {
      this.dropdownShown = true;
    },
  },
};
</script>

<template>
  <gl-disclosure-dropdown :items="items" class="gl-display-block" @shown="onShown">
    <template #toggle>
      <button
        class="organization-switcher-button gl-display-flex gl-align-items-center gl-gap-3 gl-p-3 gl-rounded-base gl-border-none gl-line-height-1 gl-w-full"
        data-testid="toggle-button"
      >
        <gl-avatar
          :size="24"
          :shape="$options.AVATAR_SHAPE_OPTION_RECT"
          :entity-id="currentOrganization.id"
          :entity-name="currentOrganization.name"
          :src="currentOrganization.avatar_url"
        />
        <span>{{ currentOrganization.name }}</span>
        <gl-icon class="gl-button-icon gl-new-dropdown-chevron" name="chevron-down" />
      </button>
    </template>

    <template #list-item="{ item }">
      <gl-loading-icon v-if="item.id === $options.ITEM_LOADING.id" />
      <span v-else-if="item.id === $options.ITEM_EMPTY.id">{{ item.text }}</span>
      <div v-else class="gl-display-flex gl-align-items-center gl-gap-3">
        <gl-avatar
          :size="24"
          :shape="$options.AVATAR_SHAPE_OPTION_RECT"
          :entity-id="item.id"
          :entity-name="item.text"
          :src="item.avatarUrl"
        />
        <span>{{ item.text }}</span>
      </div>
    </template>
  </gl-disclosure-dropdown>
</template>