-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactor Omnichannel Edit Tags UI (#30732)
- Loading branch information
1 parent
ee95791
commit b21ee52
Showing
14 changed files
with
278 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 0 additions & 42 deletions
42
apps/meteor/ee/client/omnichannel/tags/RemoveTagButton.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import type { ILivechatDepartment, ILivechatTag, Serialized } from '@rocket.chat/core-typings'; | ||
import { Field, FieldLabel, FieldRow, FieldError, TextInput, Button, ButtonGroup, FieldGroup, Box } from '@rocket.chat/fuselage'; | ||
import { useMutableCallback, useUniqueId } from '@rocket.chat/fuselage-hooks'; | ||
import { useToastMessageDispatch, useRouter, useMethod, useTranslation } from '@rocket.chat/ui-contexts'; | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import React from 'react'; | ||
import { useForm, Controller } from 'react-hook-form'; | ||
|
||
import AutoCompleteDepartmentMultiple from '../../../../client/components/AutoCompleteDepartmentMultiple'; | ||
import { | ||
ContextualbarScrollableContent, | ||
ContextualbarFooter, | ||
ContextualbarTitle, | ||
Contextualbar, | ||
ContextualbarHeader, | ||
ContextualbarClose, | ||
} from '../../../../client/components/Contextualbar'; | ||
import { useRemoveTag } from './useRemoveTag'; | ||
|
||
type TagEditPayload = { | ||
name: string; | ||
description: string; | ||
departments: { label: string; value: string }[]; | ||
}; | ||
|
||
type TagEditProps = { | ||
tagData?: ILivechatTag; | ||
currentDepartments?: Serialized<ILivechatDepartment>[]; | ||
}; | ||
|
||
const TagEdit = ({ tagData, currentDepartments }: TagEditProps) => { | ||
const t = useTranslation(); | ||
const router = useRouter(); | ||
const queryClient = useQueryClient(); | ||
const handleDeleteTag = useRemoveTag(); | ||
|
||
const dispatchToastMessage = useToastMessageDispatch(); | ||
const saveTag = useMethod('livechat:saveTag'); | ||
|
||
const { _id, name, description } = tagData || {}; | ||
|
||
const { | ||
control, | ||
formState: { isDirty, errors }, | ||
handleSubmit, | ||
} = useForm<TagEditPayload>({ | ||
mode: 'onBlur', | ||
values: { | ||
name: name || '', | ||
description: description || '', | ||
departments: currentDepartments?.map((dep) => ({ label: dep.name, value: dep._id })) || [], | ||
}, | ||
}); | ||
|
||
const handleSave = useMutableCallback(async ({ name, description, departments }: TagEditPayload) => { | ||
const departmentsId = departments?.map((dep) => dep.value) || ['']; | ||
|
||
try { | ||
await saveTag(_id as unknown as string, { name, description }, departmentsId); | ||
dispatchToastMessage({ type: 'success', message: t('Saved') }); | ||
queryClient.invalidateQueries(['livechat-tags']); | ||
} catch (error) { | ||
dispatchToastMessage({ type: 'error', message: error }); | ||
} finally { | ||
router.navigate('/omnichannel/tags'); | ||
} | ||
}); | ||
|
||
const formId = useUniqueId(); | ||
const nameField = useUniqueId(); | ||
const descriptionField = useUniqueId(); | ||
const departmentsField = useUniqueId(); | ||
|
||
return ( | ||
<Contextualbar> | ||
<ContextualbarHeader> | ||
<ContextualbarTitle>{_id ? t('Edit_Tag') : t('New_Tag')}</ContextualbarTitle> | ||
<ContextualbarClose onClick={() => router.navigate('/omnichannel/tags')}></ContextualbarClose> | ||
</ContextualbarHeader> | ||
<ContextualbarScrollableContent> | ||
<Box id={formId} is='form' autoComplete='off' onSubmit={handleSubmit(handleSave)}> | ||
<FieldGroup> | ||
<Field> | ||
<FieldLabel htmlFor={nameField} required> | ||
{t('Name')} | ||
</FieldLabel> | ||
<FieldRow> | ||
<Controller | ||
name='name' | ||
control={control} | ||
rules={{ required: t('The_field_is_required', 'name') }} | ||
render={({ field }) => <TextInput {...field} error={errors?.name?.message} aria-describedby={`${nameField}-error`} />} | ||
/> | ||
</FieldRow> | ||
{errors?.name && ( | ||
<FieldError aria-live='assertive' id={`${nameField}-error`}> | ||
{errors?.name?.message} | ||
</FieldError> | ||
)} | ||
</Field> | ||
<Field> | ||
<FieldLabel htmlFor={descriptionField}>{t('Description')}</FieldLabel> | ||
<FieldRow> | ||
<Controller name='description' control={control} render={({ field }) => <TextInput id={descriptionField} {...field} />} /> | ||
</FieldRow> | ||
</Field> | ||
<Field> | ||
<FieldLabel htmlFor={departmentsField}>{t('Departments')}</FieldLabel> | ||
<FieldRow> | ||
<Controller | ||
name='departments' | ||
control={control} | ||
render={({ field: { onChange, value, onBlur } }) => ( | ||
<AutoCompleteDepartmentMultiple id={departmentsField} onChange={onChange} value={value} onBlur={onBlur} showArchived /> | ||
)} | ||
/> | ||
</FieldRow> | ||
</Field> | ||
</FieldGroup> | ||
</Box> | ||
</ContextualbarScrollableContent> | ||
<ContextualbarFooter> | ||
<ButtonGroup stretch> | ||
<Button onClick={() => router.navigate('/omnichannel/tags')}>{t('Cancel')}</Button> | ||
<Button form={formId} disabled={!isDirty} type='submit' primary> | ||
{t('Save')} | ||
</Button> | ||
</ButtonGroup> | ||
{_id && ( | ||
<ButtonGroup stretch mbs={8}> | ||
<Button icon='trash' danger onClick={() => handleDeleteTag(_id)}> | ||
{t('Delete')} | ||
</Button> | ||
</ButtonGroup> | ||
)} | ||
</ContextualbarFooter> | ||
</Contextualbar> | ||
); | ||
}; | ||
|
||
export default TagEdit; |
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
apps/meteor/ee/client/omnichannel/tags/TagEditWithData.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { ILivechatTag } from '@rocket.chat/core-typings'; | ||
import { Callout } from '@rocket.chat/fuselage'; | ||
import { useEndpoint, useTranslation } from '@rocket.chat/ui-contexts'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import React from 'react'; | ||
|
||
import { ContextualbarSkeleton } from '../../../../client/components/Contextualbar'; | ||
import TagEdit from './TagEdit'; | ||
import TagEditWithDepartmentData from './TagEditWithDepartmentData'; | ||
|
||
const TagEditWithData = ({ tagId }: { tagId: ILivechatTag['_id'] }) => { | ||
const t = useTranslation(); | ||
|
||
const getTagById = useEndpoint('GET', '/v1/livechat/tags/:tagId', { tagId }); | ||
const { data, isLoading, isError } = useQuery(['livechat-getTagById', tagId], async () => getTagById(), { refetchOnWindowFocus: false }); | ||
|
||
if (isLoading) { | ||
return <ContextualbarSkeleton />; | ||
} | ||
|
||
if (isError) { | ||
return ( | ||
<Callout m={16} type='danger'> | ||
{t('Not_Available')} | ||
</Callout> | ||
); | ||
} | ||
|
||
if (data?.departments && data.departments.length > 0) { | ||
return <TagEditWithDepartmentData tagData={data} />; | ||
} | ||
|
||
return <TagEdit tagData={data} />; | ||
}; | ||
|
||
export default TagEditWithData; |
Oops, something went wrong.