Skip to content

Commit b783913

Browse files
committed
WIP 2
1 parent 18aca6d commit b783913

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

client/admin/users/CustomFieldsForm.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,28 @@ const CustomSelect = (props) => {
4242
};
4343

4444
const CustomFieldsAssembler = ({ customFields, didUpdate, customFieldsData, setCustomFieldsData, ...props }) => Object.entries(customFields).map(([key, value]) => {
45-
const [state, setState] = useState((customFieldsData && customFieldsData[key]) ?? value.defaultValue ?? '');
46-
useEffect(() => {
47-
if (didUpdate) {
48-
setCustomFieldsData({ ...customFieldsData, [key]: state });
49-
}
50-
}, [state]);
45+
5146
return value.type === 'text'
5247
? <CustomTextInput key={key} name={key} {...value} setState={setState} state={state} {...props}/>
5348
: <CustomSelect key={key} name={key} {...value} setState={setState} state={state} {...props}/>;
5449
});
5550

5651
export default function CustomFieldsForm({ customFieldsData, setCustomFieldsData, ...props }) {
5752
const fields = useSetting('Accounts_CustomFields');
53+
{ ...fields, ...customFieldsData}
5854

59-
const parsedFields = useMemo(() => JSON.parse(fields), [fields]);
55+
// USE FORM
56+
const parsedFields = useMemo(() => JSON.parse(fields), []);
6057

6158
const [didUpdate, setDidUpdate] = useState(false);
6259

63-
useEffect(() => {
64-
setDidUpdate(true);
65-
if (!Object.values(customFieldsData).length) {
66-
const initialData = Object.entries(parsedFields).reduce((data, [key, value]) => { data[key] = value.defaultValue ?? ''; return data; }, {});
67-
setCustomFieldsData(initialData);
68-
}
69-
}, []);
60+
// useEffect(() => { SETA O DEFAULT NO FORM
61+
// setDidUpdate(true);
62+
// if (!Object.values(customFieldsData).length) {
63+
// const initialData = Object.entries(parsedFields).reduce((data, [key, value]) => { data[key] = value.defaultValue ?? ''; return data; }, {});
64+
// setCustomFieldsData(initialData);
65+
// }
66+
// }, []);
7067

7168
return <CustomFieldsAssembler didUpdate={didUpdate} customFields={parsedFields} customFieldsData={customFieldsData} setCustomFieldsData={setCustomFieldsData} {...props}/>;
7269
}

client/admin/users/EditUser.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React, { useMemo, useState, useCallback } from 'react';
22
import { Field, TextInput, Box, Skeleton, ToggleSwitch, Icon, TextAreaInput, MultiSelectFiltered, Margins, Button, Divider } from '@rocket.chat/fuselage';
33

44
import { useTranslation } from '../../contexts/TranslationContext';
5-
import { useEndpointData } from '../../hooks/useEndpointData';
65
import { useEndpointDataExperimental, ENDPOINT_STATES } from '../../hooks/useEndpointDataExperimental';
76
import { useEndpointAction } from '../../hooks/useEndpointAction';
87
import { useEndpointUpload } from '../../hooks/useEndpointUpload';
@@ -14,10 +13,10 @@ import VerticalBar from '../../components/basic/VerticalBar';
1413

1514
export function EditUserWithData({ userId, ...props }) {
1615
const t = useTranslation();
17-
const roleData = useEndpointData('roles.list', '') || {};
16+
const { data: roleData, state: roleState, error: roleError } = useEndpointDataExperimental('roles.list', '') || {};
1817
const { data, state, error } = useEndpointDataExperimental('users.info', useMemo(() => ({ userId }), [userId]));
1918

20-
if (state === ENDPOINT_STATES.LOADING) {
19+
if ([state, roleState].includes(ENDPOINT_STATES.LOADING)) {
2120
return <Box w='full' pb='x24' {...props}>
2221
<Skeleton mbe='x4'/>
2322
<Skeleton mbe='x8' />
@@ -28,7 +27,7 @@ export function EditUserWithData({ userId, ...props }) {
2827
</Box>;
2928
}
3029

31-
if (error) {
30+
if (error || roleError) {
3231
return <Box mbs='x16' {...props}>{t('User_not_found')}</Box>;
3332
}
3433

@@ -106,7 +105,7 @@ export function EditUser({ data, roles, ...props }) {
106105
const testEqual = (a, b) => a === b || !(a || b);
107106
const handleChange = (field, currentValue, getValue = (e) => e.currentTarget.value, areEqual = testEqual) => (e) => setNewData({ ...newData, [field]: areEqual(getValue(e), currentValue) ? null : getValue(e) });
108107

109-
const availableRoles = roles.map(({ _id, description }) => [_id, description || _id]);
108+
const availableRoles = roles.map(({ _id, description }) => [_id, description || _id]);// memo
110109
const selectedRoles = newData.roles ?? data.roles;
111110
const name = newData.name ?? data.name ?? '';
112111
const username = newData.username ?? data.username;
@@ -182,8 +181,10 @@ export function EditUser({ data, roles, ...props }) {
182181
</Field.Row>
183182
</Field>
184183
<Divider />
185-
<Box fontScale='s2'>{t('Custom_Fields')}</Box>
186-
<CustomFieldsForm customFieldsData={customFieldsData} setCustomFieldsData={setCustomFieldsData}/>
184+
{ customFieldsData && <>
185+
<Box fontScale='s2'>{t('Custom_Fields')}</Box>
186+
<CustomFieldsForm customFieldsData={customFieldsData} setCustomFieldsData={setCustomFieldsData}/>
187+
</>}
187188
<Field>
188189
<Field.Row>
189190
<Box display='flex' flexDirection='row' justifyContent='space-between' w='full'>

client/hooks/useEndpointDataExperimental.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import { useToastMessageDispatch } from '../contexts/ToastMessagesContext';
55

66
export const ENDPOINT_STATES = {
77
LOADING: 'LOADING',
8-
DELAYING: 'DELAYING',
98
DONE: 'DONE',
109
ERROR: 'ERROR',
1110
};
1211

1312
const defaultState = { data: null, state: ENDPOINT_STATES.LOADING };
1413

15-
export const useEndpointDataExperimental = (endpoint, params = {}) => {
14+
export const useEndpointDataExperimental = (endpoint, params = {}, { delayTimeout = 1000 } = {}) => {
1615
const [data, setData] = useState(defaultState);
1716

1817
const getData = useEndpoint('GET', endpoint);
@@ -27,8 +26,8 @@ export const useEndpointDataExperimental = (endpoint, params = {}) => {
2726
return;
2827
}
2928

30-
setData({ state: ENDPOINT_STATES.DELAYING });
31-
}, 1000);
29+
setData({ delaying: true, state: ENDPOINT_STATES.LOADING });
30+
}, delayTimeout);
3231

3332
try {
3433
setData(defaultState);

0 commit comments

Comments
 (0)