-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] Sanitize customUserStatus and fix infinite loop (#25449)
- Loading branch information
1 parent
e90eebb
commit ea5fe92
Showing
11 changed files
with
317 additions
and
405 deletions.
There are no files selected for viewing
84 changes: 0 additions & 84 deletions
84
apps/meteor/client/views/admin/customUserStatus/AddCustomUserStatus.tsx
This file was deleted.
Oops, something went wrong.
95 changes: 0 additions & 95 deletions
95
apps/meteor/client/views/admin/customUserStatus/CustomUserStatus.tsx
This file was deleted.
Oops, something went wrong.
133 changes: 133 additions & 0 deletions
133
apps/meteor/client/views/admin/customUserStatus/CustomUserStatusForm.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,133 @@ | ||
import { IUserStatus } from '@rocket.chat/core-typings'; | ||
import { Button, ButtonGroup, TextInput, Field, Select, Icon, SelectOption } from '@rocket.chat/fuselage'; | ||
import { useSetModal, useRoute, useToastMessageDispatch, useMethod, useTranslation } from '@rocket.chat/ui-contexts'; | ||
import React, { useCallback, ReactElement } from 'react'; | ||
import { useForm, Controller } from 'react-hook-form'; | ||
|
||
import GenericModal from '../../../components/GenericModal'; | ||
import VerticalBar from '../../../components/VerticalBar'; | ||
|
||
type CustomUserStatusFormProps = { | ||
onClose: () => void; | ||
onReload: () => void; | ||
status?: IUserStatus; | ||
}; | ||
|
||
const CustomUserStatusForm = ({ onClose, onReload, status }: CustomUserStatusFormProps): ReactElement => { | ||
const t = useTranslation(); | ||
const { _id, name, statusType } = status || {}; | ||
const setModal = useSetModal(); | ||
const route = useRoute('custom-user-status'); | ||
const dispatchToastMessage = useToastMessageDispatch(); | ||
|
||
const { | ||
register, | ||
control, | ||
handleSubmit, | ||
formState: { isDirty, errors }, | ||
} = useForm({ | ||
defaultValues: { name: status?.name ?? '', statusType: status?.statusType ?? '' }, | ||
}); | ||
|
||
const saveStatus = useMethod('insertOrUpdateUserStatus'); | ||
const deleteStatus = useMethod('deleteCustomUserStatus'); | ||
|
||
const handleSave = useCallback( | ||
async (data) => { | ||
try { | ||
await saveStatus({ _id, previousName: name, previousStatusType: statusType, ...data }); | ||
|
||
dispatchToastMessage({ | ||
type: 'success', | ||
message: t('Custom_User_Status_Updated_Successfully'), | ||
}); | ||
|
||
onReload(); | ||
route.push({}); | ||
} catch (error) { | ||
dispatchToastMessage({ type: 'error', message: String(error) }); | ||
} | ||
}, | ||
[saveStatus, _id, name, statusType, route, dispatchToastMessage, t, onReload], | ||
); | ||
|
||
const handleDeleteStatus = useCallback(() => { | ||
const handleCancel = (): void => { | ||
setModal(null); | ||
}; | ||
|
||
const handleDelete = async (): Promise<void> => { | ||
try { | ||
await deleteStatus(_id); | ||
dispatchToastMessage({ type: 'success', message: t('Custom_User_Status_Has_Been_Deleted') }); | ||
onReload(); | ||
route.push({}); | ||
} catch (error) { | ||
dispatchToastMessage({ type: 'error', message: String(error) }); | ||
} finally { | ||
setModal(null); | ||
} | ||
}; | ||
|
||
setModal(() => ( | ||
<GenericModal variant='danger' onConfirm={handleDelete} onCancel={handleCancel} onClose={handleCancel} confirmText={t('Delete')}> | ||
{t('Custom_User_Status_Delete_Warning')} | ||
</GenericModal> | ||
)); | ||
}, [_id, route, deleteStatus, dispatchToastMessage, onReload, setModal, t]); | ||
|
||
const presenceOptions: SelectOption[] = [ | ||
['online', t('Online')], | ||
['busy', t('Busy')], | ||
['away', t('Away')], | ||
['offline', t('Offline')], | ||
]; | ||
|
||
return ( | ||
<VerticalBar.ScrollableContent> | ||
<Field> | ||
<Field.Label>{t('Name')}</Field.Label> | ||
<Field.Row> | ||
<TextInput {...register('name', { required: true })} placeholder={t('Name')} /> | ||
</Field.Row> | ||
{errors?.name && <Field.Error>{t('error-the-field-is-required', { field: t('Name') })}</Field.Error>} | ||
</Field> | ||
<Field> | ||
<Field.Label>{t('Presence')}</Field.Label> | ||
<Field.Row> | ||
<Controller | ||
name='statusType' | ||
control={control} | ||
rules={{ required: true }} | ||
render={({ field }): ReactElement => <Select {...field} placeholder={t('Presence')} options={presenceOptions} />} | ||
/> | ||
</Field.Row> | ||
{errors?.statusType && <Field.Error>{t('error-the-field-is-required', { field: t('Presence') })}</Field.Error>} | ||
</Field> | ||
<Field> | ||
<Field.Row> | ||
<ButtonGroup stretch w='full'> | ||
<Button onClick={onClose}>{t('Cancel')}</Button> | ||
<Button primary onClick={handleSubmit(handleSave)} disabled={!isDirty}> | ||
{t('Save')} | ||
</Button> | ||
</ButtonGroup> | ||
</Field.Row> | ||
</Field> | ||
{_id && ( | ||
<Field> | ||
<Field.Row> | ||
<ButtonGroup stretch w='full'> | ||
<Button primary danger onClick={handleDeleteStatus}> | ||
<Icon name='trash' mie='x4' /> | ||
{t('Delete')} | ||
</Button> | ||
</ButtonGroup> | ||
</Field.Row> | ||
</Field> | ||
)} | ||
</VerticalBar.ScrollableContent> | ||
); | ||
}; | ||
|
||
export default CustomUserStatusForm; |
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
Oops, something went wrong.