-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathAddAgent.tsx
More file actions
54 lines (46 loc) · 1.71 KB
/
AddAgent.tsx
File metadata and controls
54 lines (46 loc) · 1.71 KB
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
import { Button, Box, Field, FieldLabel, FieldRow } from '@rocket.chat/fuselage';
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
import { UserAutoComplete } from '@rocket.chat/ui-client';
import { useToastMessageDispatch } from '@rocket.chat/ui-contexts';
import { useQueryClient } from '@tanstack/react-query';
import { useId, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useEndpointMutation } from '../../../../hooks/useEndpointMutation';
import { omnichannelQueryKeys } from '../../../../lib/queryKeys';
const AddAgent = () => {
const { t } = useTranslation();
const [username, setUsername] = useState('');
const dispatchToastMessage = useToastMessageDispatch();
const queryClient = useQueryClient();
const usernameFieldId = useId();
const { mutateAsync: saveAction } = useEndpointMutation('POST', '/v1/livechat/users/:type', {
keys: { type: 'agent' },
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: omnichannelQueryKeys.agents() });
setUsername('');
dispatchToastMessage({ type: 'success', message: t('Agent_added') });
},
});
const handleSave = useEffectEvent(async () => {
await saveAction({ username });
});
const handleChange = (value: unknown): void => {
if (typeof value === 'string') {
setUsername(value);
}
};
return (
<Box display='flex' alignItems='center'>
<Field>
<FieldLabel htmlFor={usernameFieldId}>{t('Username')}</FieldLabel>
<FieldRow>
<UserAutoComplete id={usernameFieldId} value={username} onChange={handleChange} />
<Button disabled={!username} onClick={handleSave} mis={8} primary>
{t('Add_agent')}
</Button>
</FieldRow>
</Field>
</Box>
);
};
export default AddAgent;