Skip to content

Commit

Permalink
Proper fix for GForm not resetting the hidden fields to undefined (#3691
Browse files Browse the repository at this point in the history
)

# What this PR does

## Which issue(s) this PR fixes

#3690

## Checklist

- [ ] Unit, integration, and e2e (if applicable) tests updated
- [ ] Documentation added (or `pr:no public docs` PR label added if not
required)
- [ ] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
  • Loading branch information
teodosii authored Jan 16, 2024
1 parent 6cb0b14 commit f655315
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 deletions.
14 changes: 11 additions & 3 deletions src/components/GForm/GForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ class GForm extends React.Component<GFormProps, {}> {
<Form maxWidth="none" id={form.name} defaultValues={data} onSubmit={this.handleSubmit}>
{({ register, errors, control, getValues, setValue }) => {
const renderField = (formItem: FormItem, formIndex: number) => {
if (formItem.isVisible && !formItem.isVisible(getValues())) {
return null;
if (this.isFormItemHidden(formItem, getValues())) {
return null; // don't render the field
}

const disabled = formItem.disabled
Expand Down Expand Up @@ -270,13 +270,21 @@ class GForm extends React.Component<GFormProps, {}> {
this.forceUpdate();
};

isFormItemHidden(formItem: FormItem, data) {
return formItem?.isHidden?.(data);
}

handleSubmit = (data) => {
const { form, onSubmit } = this.props;

const normalizedData = Object.keys(data).reduce((acc, key) => {
const formItem = form.fields.find((formItem) => formItem.name === key);

const value = formItem?.normalize ? formItem.normalize(data[key]) : nullNormalizer(data[key]);
let value = formItem?.normalize ? formItem.normalize(data[key]) : nullNormalizer(data[key]);

if (this.isFormItemHidden(formItem, data)) {
value = undefined;
}

return {
...acc,
Expand Down
2 changes: 1 addition & 1 deletion src/components/GForm/GForm.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface FormItem {
description?: ReactNode;
placeholder?: string;
normalize?: (value: any) => any;
isVisible?: (data: any) => any;
isHidden?: (data: any) => any;
getDisabled?: (value: any) => any;
validation?: {
required?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/components/TextEllipsisTooltip/TextEllipsisTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TEXT_ELLIPSIS_CLASS } from 'utils/consts';
const cx = cn.bind(styles);

interface TextEllipsisTooltipProps {
content: string;
content?: string;
queryClassName?: string;
placement?: string;
className?: string;
Expand Down
26 changes: 13 additions & 13 deletions src/containers/OutgoingWebhookForm/OutgoingWebhookForm.config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function createForm(
},
],
},
isVisible: (data) => isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.TriggerType),
isHidden: (data) => !isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.TriggerType),
normalize: (value) => value,
},
{
Expand Down Expand Up @@ -136,16 +136,16 @@ export function createForm(
},
],
},
isVisible: (data) => isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.HttpMethod),
isHidden: (data) => !isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.HttpMethod),
normalize: (value) => value,
},
{
name: WebhookFormFieldName.IntegrationFilter,
label: 'Integrations',
type: FormItemType.MultiSelect,
isVisible: (data) =>
isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.IntegrationFilter) &&
data.trigger_type !== WebhookTriggerType.EscalationStep.key,
isHidden: (data) =>
!isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.IntegrationFilter) ||
data.trigger_type === WebhookTriggerType.EscalationStep.key,
extra: {
placeholder: 'Choose (Optional)',
modelName: 'alertReceiveChannelStore',
Expand All @@ -170,7 +170,7 @@ export function createForm(
extra: {
height: 30,
},
isVisible: (data) => isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.Url),
isHidden: (data) => !isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.Url),
},
{
name: WebhookFormFieldName.Headers,
Expand All @@ -180,24 +180,24 @@ export function createForm(
extra: {
rows: 3,
},
isVisible: (data) => isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.Headers),
isHidden: (data) => !isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.Headers),
},
{
name: WebhookFormFieldName.Username,
type: FormItemType.Input,
isVisible: (data) => isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.Username),
isHidden: (data) => !isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.Username),
},
{
name: WebhookFormFieldName.Password,
type: FormItemType.Password,
isVisible: (data) => isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.Password),
isHidden: (data) => !isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.Password),
},
{
name: WebhookFormFieldName.AuthorizationHeader,
description:
'Value of the Authorization header, do not need to prefix with "Authorization:". For example: Bearer AbCdEf123456',
type: FormItemType.Password,
isVisible: (data) => isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.AuthorizationHeader),
isHidden: (data) => !isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.AuthorizationHeader),
},
{
name: WebhookFormFieldName.TriggerTemplate,
Expand All @@ -207,14 +207,14 @@ export function createForm(
extra: {
rows: 2,
},
isVisible: (data) => isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.TriggerTemplate),
isHidden: (data) => !isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.TriggerTemplate),
},
{
name: WebhookFormFieldName.ForwardAll,
normalize: (value) => (value ? Boolean(value) : value),
type: FormItemType.Switch,
description: "Forwards whole payload of the alert group and context data to the webhook's url as POST/PUT data",
isVisible: (data) => isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.ForwardAll),
isHidden: (data) => !isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.ForwardAll),
},
{
name: WebhookFormFieldName.Data,
Expand All @@ -224,7 +224,7 @@ export function createForm(
hasLabelsFeature ? ' {{ webhook }}' : ''
}`,
extra: {},
isVisible: (data) => isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.Data),
isHidden: (data) => !isPresetFieldVisible(data.preset, presets, WebhookFormFieldName.Data),
},
],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

.tabs__content {
padding-top: 16px;
padding-bottom: 16px;
}

.form-row {
Expand Down
9 changes: 4 additions & 5 deletions src/pages/outgoing_webhooks/OutgoingWebhooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,7 @@ class OutgoingWebhooks extends React.Component<OutgoingWebhooksProps, OutgoingWe
};

renderTeam(record: OutgoingWebhook, teams: any) {
return (
<TextEllipsisTooltip placement="top" content={teams[record.team]?.name}>
<TeamName className={TEXT_ELLIPSIS_CLASS} team={teams[record.team]} />
</TextEllipsisTooltip>
);
return <TeamName className={TEXT_ELLIPSIS_CLASS} team={teams[record.team]} />;
}

renderActionButtons = (record: OutgoingWebhook) => {
Expand Down Expand Up @@ -435,6 +431,9 @@ class OutgoingWebhooks extends React.Component<OutgoingWebhooksProps, OutgoingWe
is_legacy: false,
};

// don't pass trigger_type to backend as it's not editable
delete data.trigger_type;

outgoingWebhookStore
.update(id, data)
.then(() => this.update())
Expand Down

0 comments on commit f655315

Please sign in to comment.