-
Notifications
You must be signed in to change notification settings - Fork 13.9k
chore: API Endpoint support for setting action type
#39983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4ae8949
setting action endpoint
KevLehman c3f08c1
lint
KevLehman 35adffb
remove setting
KevLehman 4b9be66
fix
KevLehman ec206f6
refactor: added base action input
0ff21ae
fix api call
KevLehman 47b9acc
Reorder imports in settings API endpoint
KevLehman 1b4d204
Apply suggestion from @KevLehman
KevLehman c9abbaa
Apply suggestion from @KevLehman
KevLehman 5c25267
Apply suggestion from @KevLehman
KevLehman e06da95
nice
KevLehman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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: 42 additions & 0 deletions
42
apps/meteor/client/views/admin/settings/Setting/inputs/ActionInputBase.tsx
This file contains hidden or 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,42 @@ | ||
| import { Button, FieldRow, FieldHint } from '@rocket.chat/fuselage'; | ||
| import type { TranslationKey } from '@rocket.chat/ui-contexts'; | ||
| import { useToastMessageDispatch } from '@rocket.chat/ui-contexts'; | ||
| import type { ReactElement } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import type { SettingInputProps } from './types'; | ||
|
|
||
| export type ActionInputBaseProps = SettingInputProps & { | ||
| actionText: TranslationKey; | ||
| sectionChanged: boolean; | ||
| onAction: () => Promise<{ message: TranslationKey; params?: string[] }>; | ||
| }; | ||
|
|
||
| function ActionInputBase({ actionText, hint, disabled, sectionChanged, onAction }: ActionInputBaseProps): ReactElement { | ||
| const { t } = useTranslation(); | ||
| const dispatchToastMessage = useToastMessageDispatch(); | ||
|
|
||
| const handleClick = async (): Promise<void> => { | ||
| try { | ||
| const data = await onAction(); | ||
| const params = data.params || []; | ||
| dispatchToastMessage({ type: 'success', message: t(data.message, { postProcess: 'sprintf', sprintf: params }) }); | ||
| } catch (error) { | ||
| dispatchToastMessage({ type: 'error', message: error }); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <FieldRow> | ||
| <Button disabled={disabled || sectionChanged} primary onClick={handleClick}> | ||
| {t(actionText)} | ||
| </Button> | ||
| </FieldRow> | ||
| {sectionChanged && <FieldHint>{t('Save_to_enable_this_action')}</FieldHint>} | ||
| {hint && <FieldHint>{hint}</FieldHint>} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export default ActionInputBase; |
41 changes: 10 additions & 31 deletions
41
apps/meteor/client/views/admin/settings/Setting/inputs/ActionSettingInput.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -1,46 +1,25 @@ | ||
| import { isActionSettingWithEndpoint } from '@rocket.chat/core-typings'; | ||
| import type { ServerMethods } from '@rocket.chat/ddp-client'; | ||
| import { Button, FieldRow, FieldHint } from '@rocket.chat/fuselage'; | ||
| import type { PathPattern, Method } from '@rocket.chat/rest-typings'; | ||
| import type { TranslationKey } from '@rocket.chat/ui-contexts'; | ||
| import { useMethod, useToastMessageDispatch } from '@rocket.chat/ui-contexts'; | ||
| import type { ReactElement } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import EndpointActionInput from './EndpointActionInput'; | ||
| import MethodActionInput from './MethodActionInput'; | ||
| import type { SettingInputProps } from './types'; | ||
|
|
||
| type ActionSettingInputProps = SettingInputProps & { | ||
| value: keyof ServerMethods; | ||
| value: keyof ServerMethods | { method: Method; path: PathPattern }; | ||
| actionText: TranslationKey; | ||
| sectionChanged: boolean; | ||
| }; | ||
|
|
||
| function ActionSettingInput({ actionText, value, hint, disabled, sectionChanged }: ActionSettingInputProps): ReactElement { | ||
| const { t } = useTranslation(); | ||
| function ActionSettingInput({ value, ...rest }: ActionSettingInputProps): ReactElement { | ||
| if (isActionSettingWithEndpoint(value)) { | ||
| return <EndpointActionInput endpoint={{ method: value.method, path: value.path }} {...rest} />; | ||
| } | ||
|
|
||
| const dispatchToastMessage = useToastMessageDispatch(); | ||
| const actionMethod = useMethod(value); | ||
|
|
||
| const handleClick = async (): Promise<void> => { | ||
| try { | ||
| const data: { message: TranslationKey; params?: string[] } = await actionMethod(); | ||
|
|
||
| const params = data.params || []; | ||
| dispatchToastMessage({ type: 'success', message: t(data.message, { postProcess: 'sprintf', sprintf: params }) }); | ||
| } catch (error) { | ||
| dispatchToastMessage({ type: 'error', message: error }); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <FieldRow> | ||
| <Button disabled={disabled || sectionChanged} primary onClick={handleClick}> | ||
| {t(actionText)} | ||
| </Button> | ||
| </FieldRow> | ||
| {sectionChanged && <FieldHint>{t('Save_to_enable_this_action')}</FieldHint>} | ||
| {hint && <FieldHint>{hint}</FieldHint>} | ||
| </> | ||
| ); | ||
| return <MethodActionInput value={value} {...rest} />; | ||
| } | ||
|
|
||
| export default ActionSettingInput; |
19 changes: 19 additions & 0 deletions
19
apps/meteor/client/views/admin/settings/Setting/inputs/EndpointActionInput.tsx
This file contains hidden or 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,19 @@ | ||
| import type { Method, PathPattern } from '@rocket.chat/rest-typings'; | ||
| import { useEndpoint } from '@rocket.chat/ui-contexts'; | ||
|
|
||
| import type { ActionInputBaseProps } from './ActionInputBase'; | ||
| import ActionInputBase from './ActionInputBase'; | ||
|
|
||
| type EndpointActionInputProps = Omit<ActionInputBaseProps, 'onAction'> & { | ||
| endpoint: { | ||
| method: Method; | ||
| path: PathPattern; | ||
| }; | ||
| }; | ||
|
|
||
| function EndpointActionInput({ endpoint, ...rest }: EndpointActionInputProps) { | ||
| const callEndpoint = useEndpoint(endpoint.method, endpoint.path); | ||
| return <ActionInputBase onAction={() => callEndpoint({} as never)} {...rest} />; | ||
| } | ||
|
KevLehman marked this conversation as resolved.
|
||
|
|
||
| export default EndpointActionInput; | ||
16 changes: 16 additions & 0 deletions
16
apps/meteor/client/views/admin/settings/Setting/inputs/MethodActionInput.tsx
This file contains hidden or 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,16 @@ | ||
| import type { ServerMethods } from '@rocket.chat/ddp-client'; | ||
| import { useMethod } from '@rocket.chat/ui-contexts'; | ||
|
|
||
| import type { ActionInputBaseProps } from './ActionInputBase'; | ||
| import ActionInputBase from './ActionInputBase'; | ||
|
|
||
| type MethodActionInputProps = Omit<ActionInputBaseProps, 'onAction'> & { | ||
| value: keyof ServerMethods; | ||
| }; | ||
|
|
||
| function MethodActionInput({ value, ...rest }: MethodActionInputProps) { | ||
| const actionMethod = useMethod(value); | ||
| return <ActionInputBase onAction={actionMethod} {...rest} />; | ||
| } | ||
|
|
||
| export default MethodActionInput; |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.