Alerting: run validation before testing contact point (#43721)

This commit is contained in:
Gilles De Mey 2022-01-10 13:28:36 +01:00 committed by GitHub
parent be61f192c8
commit 7b841c7947
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,7 +37,7 @@ export function ChannelSubForm<R extends ChannelValues>({
}: Props<R>): JSX.Element { }: Props<R>): JSX.Element {
const styles = useStyles2(getStyles); const styles = useStyles2(getStyles);
const name = (fieldName: string) => `${pathPrefix}${fieldName}`; const name = (fieldName: string) => `${pathPrefix}${fieldName}`;
const { control, watch, register } = useFormContext(); const { control, watch, register, trigger, formState } = useFormContext();
const selectedType = watch(name('type')) ?? defaultValues.type; // nope, setting "default" does not work at all. const selectedType = watch(name('type')) ?? defaultValues.type; // nope, setting "default" does not work at all.
const { loading: testingReceiver } = useUnifiedAlertingSelector((state) => state.testReceivers); const { loading: testingReceiver } = useUnifiedAlertingSelector((state) => state.testReceivers);
@ -66,6 +66,15 @@ export function ChannelSubForm<R extends ChannelValues>({
[notifiers] [notifiers]
); );
const handleTest = async () => {
await trigger();
const isValid = Object.keys(formState.errors).length === 0;
if (isValid && onTest) {
onTest();
}
};
const notifier = notifiers.find(({ type }) => type === selectedType); const notifier = notifiers.find(({ type }) => type === selectedType);
// if there are mandatory options defined, optional options will be hidden by a collapse // if there are mandatory options defined, optional options will be hidden by a collapse
// if there aren't mandatory options, all options will be shown without collapse // if there aren't mandatory options, all options will be shown without collapse
@ -105,7 +114,7 @@ export function ChannelSubForm<R extends ChannelValues>({
size="xs" size="xs"
variant="secondary" variant="secondary"
type="button" type="button"
onClick={() => onTest()} onClick={() => handleTest()}
icon={testingReceiver ? 'fa fa-spinner' : 'message'} icon={testingReceiver ? 'fa fa-spinner' : 'message'}
> >
Test Test