您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import {
  21. QueryFunctionContext,
  22. useMutation,
  23. useQueries,
  24. useQueryClient,
  25. } from '@tanstack/react-query';
  26. import { range } from 'lodash';
  27. import { deleteUser, getUsers, postUser, updateUser } from '../api/users';
  28. import { RestUserBase } from '../types/users';
  29. const STALE_TIME = 4 * 60 * 1000;
  30. export function useUsersQueries<U extends RestUserBase>(
  31. getParams: Omit<Parameters<typeof getUsers>[0], 'pageSize' | 'pageIndex'>,
  32. numberOfPages: number
  33. ) {
  34. type QueryKey = [
  35. 'user',
  36. 'list',
  37. number,
  38. Omit<Parameters<typeof getUsers>[0], 'pageSize' | 'pageIndex'>
  39. ];
  40. const results = useQueries({
  41. queries: range(1, numberOfPages + 1).map((page: number) => ({
  42. queryKey: ['user', 'list', page, getParams],
  43. queryFn: ({ queryKey: [_u, _l, page, getParams] }: QueryFunctionContext<QueryKey>) =>
  44. getUsers<U>({ ...getParams, pageIndex: page }),
  45. staleTime: STALE_TIME,
  46. })),
  47. });
  48. return results.reduce(
  49. (acc, { data, isLoading }) => ({
  50. users: acc.users.concat(data?.users ?? []),
  51. total: data?.page.total,
  52. isLoading: acc.isLoading || isLoading,
  53. }),
  54. { users: [] as U[], total: 0, isLoading: false }
  55. );
  56. }
  57. export function useInvalidateUsersList() {
  58. const queryClient = useQueryClient();
  59. return () => queryClient.invalidateQueries({ queryKey: ['user', 'list'] });
  60. }
  61. export function usePostUserMutation() {
  62. const queryClient = useQueryClient();
  63. return useMutation({
  64. mutationFn: async (data: Parameters<typeof postUser>[0]) => {
  65. await postUser(data);
  66. },
  67. onSuccess() {
  68. queryClient.invalidateQueries({ queryKey: ['user', 'list'] });
  69. },
  70. });
  71. }
  72. export function useUpdateUserMutation() {
  73. const queryClient = useQueryClient();
  74. return useMutation({
  75. mutationFn: async (data: Parameters<typeof updateUser>[0]) => {
  76. await updateUser(data);
  77. },
  78. onSuccess() {
  79. queryClient.invalidateQueries({ queryKey: ['user', 'list'] });
  80. },
  81. });
  82. }
  83. export function useDeactivateUserMutation() {
  84. const queryClient = useQueryClient();
  85. return useMutation({
  86. mutationFn: async (data: Parameters<typeof deleteUser>[0]) => {
  87. await deleteUser(data);
  88. },
  89. onSuccess() {
  90. queryClient.invalidateQueries({ queryKey: ['user', 'list'] });
  91. },
  92. });
  93. }