mirror of
https://github.com/portainer/portainer.git
synced 2025-08-08 15:25:22 +02:00
refactor(auth): cache user data [EE-4935] (#8380)
This commit is contained in:
parent
a748e15c16
commit
00bbf4ac63
8 changed files with 59 additions and 46 deletions
|
@ -1,6 +1,8 @@
|
|||
import _ from 'lodash-es';
|
||||
|
||||
import { UserTokenModel, UserViewModel } from '@/portainer/models/user';
|
||||
import { getUser, getUsers } from '@/portainer/users/user.service';
|
||||
import { getUsers } from '@/portainer/users/user.service';
|
||||
import { getUser } from '@/portainer/users/queries/useUser';
|
||||
|
||||
import { TeamMembershipModel } from '../../models/teamMembership';
|
||||
|
||||
|
@ -15,8 +17,8 @@ export function UserService($q, Users, TeamService, TeamMembershipService) {
|
|||
return users.map((u) => new UserViewModel(u));
|
||||
};
|
||||
|
||||
service.user = async function (includeAdministrators) {
|
||||
const user = await getUser(includeAdministrators);
|
||||
service.user = async function (userId) {
|
||||
const user = await getUser(userId);
|
||||
|
||||
return new UserViewModel(user);
|
||||
};
|
||||
|
|
27
app/portainer/users/queries/useUser.ts
Normal file
27
app/portainer/users/queries/useUser.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { useQuery } from 'react-query';
|
||||
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
import { withError } from '@/react-tools/react-query';
|
||||
|
||||
import { buildUrl } from '../user.service';
|
||||
import { User, UserId } from '../types';
|
||||
|
||||
export function useUser(
|
||||
id: UserId,
|
||||
{ staleTime }: { staleTime?: number } = {}
|
||||
) {
|
||||
return useQuery(['users', id], () => getUser(id), {
|
||||
...withError('Unable to retrieve user details'),
|
||||
staleTime,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUser(id: UserId) {
|
||||
try {
|
||||
const { data: user } = await axios.get<User>(buildUrl(id));
|
||||
|
||||
return user;
|
||||
} catch (e) {
|
||||
throw parseAxiosError(e as Error, 'Unable to retrieve user details');
|
||||
}
|
||||
}
|
|
@ -19,16 +19,6 @@ export async function getUsers(
|
|||
}
|
||||
}
|
||||
|
||||
export async function getUser(id: UserId) {
|
||||
try {
|
||||
const { data: user } = await axios.get<User>(buildUrl(id));
|
||||
|
||||
return user;
|
||||
} catch (e) {
|
||||
throw parseAxiosError(e as Error, 'Unable to retrieve user details');
|
||||
}
|
||||
}
|
||||
|
||||
export async function getUserMemberships(id: UserId) {
|
||||
try {
|
||||
const { data } = await axios.get<TeamMembership[]>(
|
||||
|
@ -40,7 +30,7 @@ export async function getUserMemberships(id: UserId) {
|
|||
}
|
||||
}
|
||||
|
||||
function buildUrl(id?: UserId, entity?: string) {
|
||||
export function buildUrl(id?: UserId, entity?: string) {
|
||||
let url = '/users';
|
||||
|
||||
if (id) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue