From 9c8b10a86e6f5b82b9fca979e22b9b3bbd1e85e9 Mon Sep 17 00:00:00 2001 From: Shekar Siri Date: Tue, 15 Nov 2022 14:42:35 +0100 Subject: [PATCH] fix(ui) - alert from reset --- .../components/Alerts/AlertForm/Condition.tsx | 4 +- .../Dashboard/components/Alerts/NewAlert.tsx | 213 +++++++++--------- 2 files changed, 109 insertions(+), 108 deletions(-) diff --git a/frontend/app/components/Dashboard/components/Alerts/AlertForm/Condition.tsx b/frontend/app/components/Dashboard/components/Alerts/AlertForm/Condition.tsx index dcb24d6e4..3fa9a9a43 100644 --- a/frontend/app/components/Dashboard/components/Alerts/AlertForm/Condition.tsx +++ b/frontend/app/components/Dashboard/components/Alerts/AlertForm/Condition.tsx @@ -63,7 +63,7 @@ function Condition({ isSearchable={true} options={triggerOptions} name="left" - value={triggerOptions.find((i) => i.value === instance.query.left)} + value={triggerOptions.find((i) => i.value === instance.query.left) || ''} onChange={({ value }) => writeQueryOption(null, { name: 'left', value: value.value })} /> @@ -75,7 +75,7 @@ function Condition({ placeholder="Select Condition" options={conditions} name="operator" - value={conditions.find(c => c.value === instance.query.operator)} + value={conditions.find(c => c.value === instance.query.operator) || ''} onChange={({ value }) => writeQueryOption(null, { name: 'operator', value: value.value }) } diff --git a/frontend/app/components/Dashboard/components/Alerts/NewAlert.tsx b/frontend/app/components/Dashboard/components/Alerts/NewAlert.tsx index aee10e143..0ea012e71 100644 --- a/frontend/app/components/Dashboard/components/Alerts/NewAlert.tsx +++ b/frontend/app/components/Dashboard/components/Alerts/NewAlert.tsx @@ -18,9 +18,11 @@ import NotifyHooks from './AlertForm/NotifyHooks'; import AlertListItem from './AlertListItem'; import Condition from './AlertForm/Condition'; - const Circle = ({ text }: { text: string }) => ( -
+
{text}
); @@ -54,7 +56,7 @@ interface IProps extends RouteComponentProps { loading: boolean; deleting: boolean; triggerOptions: any[]; - list: any, + list: any; fetchTriggerOptions: () => void; edit: (query: any) => void; init: (alert?: Alert) => any; @@ -83,6 +85,7 @@ const NewAlert = (props: IProps) => { } = props; useEffect(() => { + init({}); if (list.size === 0) fetchList(); props.fetchTriggerOptions(); fetchWebhooks(); @@ -90,19 +93,22 @@ const NewAlert = (props: IProps) => { useEffect(() => { if (list.size > 0) { - const alertId = location.pathname.split('/').pop() - const currentAlert = list.toJS().find((alert: Alert) => alert.alertId === parseInt(alertId, 10)); + const alertId = location.pathname.split('/').pop(); + const currentAlert = list + .toJS() + .find((alert: Alert) => alert.alertId === parseInt(alertId, 10)); init(currentAlert); } - }, [list]) - + }, [list]); const write = ({ target: { value, name } }: React.ChangeEvent) => props.edit({ [name]: value }); + const writeOption = ( _: React.ChangeEvent, { name, value }: { name: string; value: Record } ) => props.edit({ [name]: value.value }); + const onChangeCheck = ({ target: { checked, name } }: React.ChangeEvent) => props.edit({ [name]: checked }); @@ -115,39 +121,35 @@ const NewAlert = (props: IProps) => { }) ) { remove(instance.alertId).then(() => { - props.history.push(withSiteId(alerts(), siteId)) + props.history.push(withSiteId(alerts(), siteId)); }); } }; + const onSave = (instance: Alert) => { const wasUpdating = instance.exists(); save(instance).then(() => { if (!wasUpdating) { toast.success('New alert saved'); - props.history.push(withSiteId(alerts(), siteId)) + props.history.push(withSiteId(alerts(), siteId)); } else { toast.success('Alert updated'); } }); }; - const onClose = () => { - props.history.push(withSiteId(alerts(), siteId)) - } - const slackChannels = webhooks .filter((hook) => hook.type === SLACK) .map(({ webhookId, name }) => ({ value: webhookId, label: name })) // @ts-ignore .toJS(); + const hooks = webhooks .filter((hook) => hook.type === WEBHOOK) .map(({ webhookId, name }) => ({ value: webhookId, label: name })) // @ts-ignore .toJS(); - - const writeQueryOption = ( e: React.ChangeEvent, { name, value }: { name: string; value: string } @@ -184,87 +186,84 @@ const NewAlert = (props: IProps) => { onSubmit={() => onSave(instance)} id="alert-form" > -
+

- write({ target: { value: name, name: 'name' }} as any)} canEdit /> + write({ target: { value: name, name: 'name' } } as any)} + canEdit + />

-
-
+
-
-
- props.edit({ [name]: value })} - value={{ value: instance.detectionMethod }} - list={[ - { name: 'Threshold', value: 'threshold' }, - { name: 'Change', value: 'change' }, - ]} - /> -
- {isThreshold && - 'Eg. When Threshold is above 1ms over the past 15mins, notify me through Slack #foss-notifications.'} - {!isThreshold && - 'Eg. Alert me if % change of memory.avg is greater than 10% over the past 4 hours compared to the previous 4 hours.'} -
-
-
- } - /> -
- } - /> -
- } - /> -
- -
- +
+ props.edit({ [name]: value })} + value={{ value: instance.detectionMethod }} + list={[ + { name: 'Threshold', value: 'threshold' }, + { name: 'Change', value: 'change' }, + ]} + /> +
+ {isThreshold && + 'Eg. When Threshold is above 1ms over the past 15mins, notify me through Slack #foss-notifications.'} + {!isThreshold && + 'Eg. Alert me if % change of memory.avg is greater than 10% over the past 4 hours compared to the previous 4 hours.'} +
+
+
+ } + /> +
-
+ } + /> +
+ } + /> +
+
+ +
@@ -276,21 +275,23 @@ const NewAlert = (props: IProps) => { ); }; -export default withRouter(connect( - (state) => ({ +export default withRouter( + connect( + (state) => ({ + // @ts-ignore + instance: state.getIn(['alerts', 'instance']), + //@ts-ignore + list: state.getIn(['alerts', 'list']), + // @ts-ignore + triggerOptions: state.getIn(['alerts', 'triggerOptions']), + // @ts-ignore + loading: state.getIn(['alerts', 'saveRequest', 'loading']), + // @ts-ignore + deleting: state.getIn(['alerts', 'removeRequest', 'loading']), + // @ts-ignore + webhooks: state.getIn(['webhooks', 'list']), + }), + { fetchTriggerOptions, init, edit, save, remove, fetchWebhooks, fetchList } // @ts-ignore - instance: state.getIn(['alerts', 'instance']), - //@ts-ignore - list: state.getIn(['alerts', 'list']), - // @ts-ignore - triggerOptions: state.getIn(['alerts', 'triggerOptions']), - // @ts-ignore - loading: state.getIn(['alerts', 'saveRequest', 'loading']), - // @ts-ignore - deleting: state.getIn(['alerts', 'removeRequest', 'loading']), - // @ts-ignore - webhooks: state.getIn(['webhooks', 'list']), - }), - { fetchTriggerOptions, init, edit, save, remove, fetchWebhooks, fetchList } - // @ts-ignore -)(NewAlert)); + )(NewAlert) +);