/
EditUserWithData.tsx
81 lines (67 loc) · 1.88 KB
/
EditUserWithData.tsx
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
import type { IUser } from '@rocket.chat/core-typings';
import { isUserFederated } from '@rocket.chat/core-typings';
import { Box, Callout } from '@rocket.chat/fuselage';
import { useEndpoint, useToastMessageDispatch, useTranslation } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';
import type { ReactElement } from 'react';
import React, { useMemo } from 'react';
import { FormSkeleton } from '../../../components/Skeleton';
import EditUser from './EditUser';
type EditUserWithDataProps = {
uid: IUser['_id'];
onReload: () => void;
};
const EditUserWithData = ({ uid, onReload, ...props }: EditUserWithDataProps): ReactElement => {
const t = useTranslation();
const getRoles = useEndpoint('GET', '/v1/roles.list');
const dispatchToastMessage = useToastMessageDispatch();
const query = useMemo(() => ({ userId: uid }), [uid]);
const {
data: roleData,
isLoading: roleState,
error: roleError,
} = useQuery(
['roles'],
async () => {
const roles = await getRoles();
return roles;
},
{
onError: (error) => {
dispatchToastMessage({ type: 'error', message: error });
},
},
);
const getUsersInfo = useEndpoint('GET', '/v1/users.info');
const {
data,
isLoading: state,
error,
} = useQuery(['users', query, 'admin'], async () => {
const usersInfo = await getUsersInfo(query);
return usersInfo;
});
if (state || roleState) {
return (
<Box p='x24'>
<FormSkeleton />
</Box>
);
}
if (error || roleError) {
return (
<Callout m='x16' type='danger'>
{t('User_not_found')}
</Callout>
);
}
if (data?.user && isUserFederated(data?.user as unknown as IUser)) {
return (
<Callout m='x16' type='danger'>
{t('Edit_Federated_User_Not_Allowed')}
</Callout>
);
}
return <EditUser data={data?.user} roles={roleData?.roles} onReload={onReload} {...props} />;
};
export default EditUserWithData;