|
1 | | -import type { ILivechatDepartment, ILivechatUnitMonitor, Serialized, IOmnichannelBusinessUnit } from '@rocket.chat/core-typings'; |
| 1 | +import type { |
| 2 | + ILivechatDepartment, |
| 3 | + ILivechatUnitMonitor, |
| 4 | + Serialized, |
| 5 | + IOmnichannelBusinessUnit, |
| 6 | + OmnichannelBusinessUnitPayload, |
| 7 | +} from '@rocket.chat/core-typings'; |
2 | 8 | import type { SelectOption } from '@rocket.chat/fuselage'; |
3 | 9 | import { FieldError, Field, TextInput, Button, Select, ButtonGroup, FieldGroup, Box, FieldLabel, FieldRow } from '@rocket.chat/fuselage'; |
4 | 10 | import { useEffectEvent } from '@rocket.chat/fuselage-hooks'; |
5 | | -import { useToastMessageDispatch, useMethod, useTranslation } from '@rocket.chat/ui-contexts'; |
| 11 | +import { useToastMessageDispatch, useTranslation, useEndpoint } from '@rocket.chat/ui-contexts'; |
6 | 12 | import { useQueryClient } from '@tanstack/react-query'; |
7 | 13 | import { useId, useMemo } from 'react'; |
8 | 14 | import { useForm, Controller } from 'react-hook-form'; |
@@ -34,13 +40,14 @@ type UnitEditProps = { |
34 | 40 | unitData?: Serialized<IOmnichannelBusinessUnit>; |
35 | 41 | unitMonitors?: Serialized<ILivechatUnitMonitor>[]; |
36 | 42 | unitDepartments?: Serialized<ILivechatDepartment>[]; |
37 | | - onClose: () => void; |
| 43 | + onUpdate?: (params: OmnichannelBusinessUnitPayload) => void; |
38 | 44 | onDelete?: () => void; |
| 45 | + onClose: () => void; |
39 | 46 | }; |
40 | 47 |
|
41 | | -const UnitEdit = ({ unitData, unitMonitors, unitDepartments, onClose, onDelete }: UnitEditProps) => { |
| 48 | +const UnitEdit = ({ unitData, unitMonitors, unitDepartments, onUpdate, onDelete, onClose }: UnitEditProps) => { |
42 | 49 | const t = useTranslation(); |
43 | | - const saveUnit = useMethod('livechat:saveUnit'); |
| 50 | + const saveUnit = useEndpoint('POST', '/v1/livechat/units'); |
44 | 51 | const dispatchToastMessage = useToastMessageDispatch(); |
45 | 52 | const queryClient = useQueryClient(); |
46 | 53 |
|
@@ -94,8 +101,19 @@ const UnitEdit = ({ unitData, unitMonitors, unitDepartments, onClose, onDelete } |
94 | 101 | username: monitor.label, |
95 | 102 | })); |
96 | 103 |
|
| 104 | + const payload = { |
| 105 | + unitData: { name, visibility }, |
| 106 | + unitMonitors: monitorsData, |
| 107 | + unitDepartments: departmentsData, |
| 108 | + }; |
| 109 | + |
97 | 110 | try { |
98 | | - await saveUnit(_id as unknown as string, { name, visibility }, monitorsData, departmentsData); |
| 111 | + if (_id && onUpdate) { |
| 112 | + await onUpdate(payload); |
| 113 | + } else { |
| 114 | + await saveUnit(payload); |
| 115 | + } |
| 116 | + |
99 | 117 | dispatchToastMessage({ type: 'success', message: t('Saved') }); |
100 | 118 | queryClient.invalidateQueries({ |
101 | 119 | queryKey: ['livechat-units'], |
|
0 commit comments