aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/queries/groups.ts
blob: a75925936124659fc5c8f6d1a1ab8f3a66776f93 (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
/*
 * SonarQube
 * Copyright (C) 2009-2023 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import {
  QueryFunctionContext,
  useMutation,
  useQueries,
  useQuery,
  useQueryClient,
} from '@tanstack/react-query';
import { range } from 'lodash';
import {
  createGroup,
  deleteGroup,
  getUsersGroups,
  getUsersInGroup,
  updateGroup,
} from '../api/user_groups';
import { Group } from '../types/types';

const STALE_TIME = 4 * 60 * 1000;

export function useGroupsQueries(
  getParams: Omit<Parameters<typeof getUsersGroups>[0], 'pageSize' | 'pageIndex'>,
  numberOfPages: number,
) {
  type QueryKey = [
    'group',
    'list',
    number,
    Omit<Parameters<typeof getUsersGroups>[0], 'pageSize' | 'pageIndex'>,
  ];
  const results = useQueries({
    queries: range(1, numberOfPages + 1).map((page: number) => ({
      queryKey: ['group', 'list', page, getParams],
      queryFn: ({ queryKey: [_u, _l, page, getParams] }: QueryFunctionContext<QueryKey>) =>
        getUsersGroups({ ...getParams, p: page }),
      staleTime: STALE_TIME,
    })),
  });

  return results.reduce<{ groups: Group[]; total: number | undefined; isLoading: boolean }>(
    (acc, { data, isLoading }) => ({
      groups: acc.groups.concat(data?.groups ?? []),
      total: data?.paging.total,
      isLoading: acc.isLoading || isLoading,
    }),
    { groups: [], total: 0, isLoading: false },
  );
}

export function useMembersCountQuery(name: string) {
  return useQuery({
    queryKey: ['group', name, 'members', 'total'],
    queryFn: () => getUsersInGroup({ name, ps: 1 }).then((r) => r.paging.total),
    staleTime: STALE_TIME,
  });
}

export function useCreateGroupMutation() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: Parameters<typeof createGroup>[0]) => createGroup(data),
    onSuccess() {
      queryClient.invalidateQueries({ queryKey: ['group', 'list'] });
    },
  });
}

export function useUpdateGroupMutation() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: Parameters<typeof updateGroup>[0]) => updateGroup(data),
    onSuccess() {
      queryClient.invalidateQueries({ queryKey: ['group', 'list'] });
    },
  });
}

export function useDeleteGroupMutation() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: Parameters<typeof deleteGroup>[0]) => deleteGroup(data),
    onSuccess() {
      queryClient.invalidateQueries({ queryKey: ['group', 'list'] });
    },
  });
}