summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/admin/users/components/users_table.vue
blob: ede5c26e487f9ccde02a9f5da13787c95f4ec5e7 (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
<script>
import { GlSkeletonLoader, GlTable } from '@gitlab/ui';
import createFlash from '~/flash';
import { convertNodeIdsFromGraphQLIds } from '~/graphql_shared/utils';
import { thWidthClass } from '~/lib/utils/table_utility';
import { s__, __ } from '~/locale';
import UserDate from '~/vue_shared/components/user_date.vue';
import getUsersGroupCountsQuery from '../graphql/queries/get_users_group_counts.query.graphql';
import UserActions from './user_actions.vue';
import UserAvatar from './user_avatar.vue';

export default {
  components: {
    GlSkeletonLoader,
    GlTable,
    UserAvatar,
    UserActions,
    UserDate,
  },
  props: {
    users: {
      type: Array,
      required: true,
    },
    paths: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      groupCounts: [],
    };
  },
  apollo: {
    groupCounts: {
      query: getUsersGroupCountsQuery,
      variables() {
        return {
          usernames: this.users.map((user) => user.username),
        };
      },
      update(data) {
        const nodes = data?.users?.nodes || [];
        const parsedIds = convertNodeIdsFromGraphQLIds(nodes);

        return parsedIds.reduce((acc, { id, groupCount }) => {
          acc[id] = groupCount || 0;
          return acc;
        }, {});
      },
      error(error) {
        createFlash({
          message: this.$options.i18n.groupCountFetchError,
          captureError: true,
          error,
        });
      },
      skip() {
        return !this.users.length;
      },
    },
  },
  i18n: {
    groupCountFetchError: s__(
      'AdminUsers|Could not load user group counts. Please refresh the page to try again.',
    ),
  },
  fields: [
    {
      key: 'name',
      label: __('Name'),
      thClass: thWidthClass(40),
    },
    {
      key: 'projectsCount',
      label: __('Projects'),
      thClass: thWidthClass(10),
    },
    {
      key: 'groupCount',
      label: __('Groups'),
      thClass: thWidthClass(10),
    },
    {
      key: 'createdAt',
      label: __('Created on'),
      thClass: thWidthClass(15),
    },
    {
      key: 'lastActivityOn',
      label: __('Last activity'),
      thClass: thWidthClass(15),
    },
    {
      key: 'settings',
      label: '',
      thClass: thWidthClass(10),
    },
  ],
};
</script>

<template>
  <div>
    <gl-table
      :items="users"
      :fields="$options.fields"
      :empty-text="s__('AdminUsers|No users found')"
      show-empty
      stacked="md"
      :tbody-tr-attr="{ 'data-qa-selector': 'user_row_content' }"
    >
      <template #cell(name)="{ item: user }">
        <user-avatar :user="user" :admin-user-path="paths.adminUser" />
      </template>

      <template #cell(createdAt)="{ item: { createdAt } }">
        <user-date :date="createdAt" />
      </template>

      <template #cell(lastActivityOn)="{ item: { lastActivityOn } }">
        <user-date :date="lastActivityOn" show-never />
      </template>

      <template #cell(groupCount)="{ item: { id } }">
        <div :data-testid="`user-group-count-${id}`">
          <gl-skeleton-loader v-if="$apollo.loading" :width="40" :lines="1" />
          <span v-else>{{ groupCounts[id] }}</span>
        </div>
      </template>

      <template #cell(projectsCount)="{ item: { id, projectsCount } }">
        <div :data-testid="`user-project-count-${id}`">{{ projectsCount }}</div>
      </template>

      <template #cell(settings)="{ item: user }">
        <user-actions :user="user" :paths="paths" />
      </template>
    </gl-table>
  </div>
</template>