diff --git a/grafana-plugin/src/containers/OutgoingWebhook2Form/OutgoingWebhook2Form.tsx b/grafana-plugin/src/containers/OutgoingWebhook2Form/OutgoingWebhook2Form.tsx index ec43fe54..bf3dd9d1 100644 --- a/grafana-plugin/src/containers/OutgoingWebhook2Form/OutgoingWebhook2Form.tsx +++ b/grafana-plugin/src/containers/OutgoingWebhook2Form/OutgoingWebhook2Form.tsx @@ -19,22 +19,28 @@ const cx = cn.bind(styles); interface OutgoingWebhook2FormProps { id: OutgoingWebhook2['id'] | 'new'; + action: 'new' | 'update'; onHide: () => void; onUpdate: () => void; } const OutgoingWebhook2Form = observer((props: OutgoingWebhook2FormProps) => { - const { id, onUpdate, onHide } = props; + const { id, action, onUpdate, onHide } = props; const store = useStore(); const { outgoingWebhook2Store } = store; - const data = id === 'new' ? { is_webhook_enabled: true, is_legacy: false } : outgoingWebhook2Store.items[id]; + const data = + id === 'new' + ? { is_webhook_enabled: true, is_legacy: false } + : action === 'new' + ? { ...outgoingWebhook2Store.items[id], is_legacy: false, name: '' } + : outgoingWebhook2Store.items[id]; const handleSubmit = useCallback( (data: Partial) => { - (id === 'new' ? outgoingWebhook2Store.create(data) : outgoingWebhook2Store.update(id, data)).then(() => { + (action === 'new' ? outgoingWebhook2Store.create(data) : outgoingWebhook2Store.update(id, data)).then(() => { onHide(); onUpdate(); @@ -48,7 +54,7 @@ const OutgoingWebhook2Form = observer((props: OutgoingWebhook2FormProps) => { scrollableContent title={ - {id === 'new' ? 'Create' : 'Edit'} Outgoing Webhook + {action === 'new' ? 'Create' : 'Edit'} Outgoing Webhook } onClose={onHide} @@ -58,7 +64,7 @@ const OutgoingWebhook2Form = observer((props: OutgoingWebhook2FormProps) => { diff --git a/grafana-plugin/src/pages/outgoing_webhooks_2/OutgoingWebhooks2.tsx b/grafana-plugin/src/pages/outgoing_webhooks_2/OutgoingWebhooks2.tsx index 63102aa6..89ca5467 100644 --- a/grafana-plugin/src/pages/outgoing_webhooks_2/OutgoingWebhooks2.tsx +++ b/grafana-plugin/src/pages/outgoing_webhooks_2/OutgoingWebhooks2.tsx @@ -39,6 +39,7 @@ const cx = cn.bind(styles); const Action = { STATUS: 'status', EDIT: 'edit', + COPY: 'copy', }; interface OutgoingWebhooks2Props @@ -47,8 +48,8 @@ interface OutgoingWebhooks2Props RouteComponentProps<{ id: string; action: string }> {} interface OutgoingWebhooks2State extends PageBaseState { - outgoingWebhook2IdToEdit?: OutgoingWebhook2['id'] | 'new'; - outgoingWebhook2IdToShowStatus?: OutgoingWebhook2['id']; + outgoingWebhook2Action?: 'new' | 'update'; + outgoingWebhook2Id?: OutgoingWebhook2['id']; } @observer @@ -66,7 +67,7 @@ class OutgoingWebhooks2 extends React.Component { this.setState((_prevState) => ({ errorData: initErrorDataState(), - outgoingWebhook2IdToEdit: undefined, + outgoingWebhook2Id: undefined, })); // reset state on query parse const { @@ -89,10 +90,12 @@ class OutgoingWebhooks2 extends React.Component this.setState({ errorData: { ...getWrongTeamResponseInfo(error) } })); } - if (isNewWebhook || (action === Action.EDIT && outgoingWebhook2)) { - this.setState({ outgoingWebhook2IdToEdit: id }); + if (isNewWebhook || (action === Action.COPY && outgoingWebhook2)) { + this.setState({ outgoingWebhook2Id: id, outgoingWebhook2Action: 'new' }); + } else if (action === Action.EDIT && outgoingWebhook2) { + this.setState({ outgoingWebhook2Id: id, outgoingWebhook2Action: 'update' }); } else if (action === Action.STATUS && outgoingWebhook2) { - this.setState({ outgoingWebhook2IdToShowStatus: id }); + this.setState({ outgoingWebhook2Id: id, outgoingWebhook2Action: undefined }); } }; @@ -104,7 +107,7 @@ class OutgoingWebhooks2 extends React.Component - {outgoingWebhook2IdToEdit && !outgoingWebhook2IdToShowStatus && ( + {outgoingWebhook2Id && outgoingWebhook2Action && ( )} - {outgoingWebhook2IdToShowStatus && ( + {outgoingWebhook2Id && !outgoingWebhook2Action && ( @@ -264,20 +263,32 @@ class OutgoingWebhooks2 extends React.Component - + this.onStatusClick(record.id)} + /> - + this.onEditClick(record.id)} /> + + + this.onCopyClick(record.id)} + /> - - + + @@ -331,15 +342,23 @@ class OutgoingWebhooks2 extends React.Component { const { history } = this.props; - this.setState({ outgoingWebhook2IdToEdit: id, outgoingWebhook2IdToShowStatus: undefined }); + this.setState({ outgoingWebhook2Id: id, outgoingWebhook2Action: 'update' }); history.push(`${PLUGIN_ROOT}/outgoing_webhooks_2/edit/${id}`); }; + onCopyClick = (id: OutgoingWebhook2['id']) => { + const { history } = this.props; + + this.setState({ outgoingWebhook2Id: id, outgoingWebhook2Action: 'new' }); + + history.push(`${PLUGIN_ROOT}/outgoing_webhooks_2/copy/${id}`); + }; + onStatusClick = (id: OutgoingWebhook2['id']) => { const { history } = this.props; - this.setState({ outgoingWebhook2IdToEdit: undefined, outgoingWebhook2IdToShowStatus: id }); + this.setState({ outgoingWebhook2Id: id, outgoingWebhook2Action: undefined }); history.push(`${PLUGIN_ROOT}/outgoing_webhooks_2/status/${id}`); }; @@ -347,7 +366,7 @@ class OutgoingWebhooks2 extends React.Component { const { history } = this.props; - this.setState({ outgoingWebhook2IdToEdit: undefined, outgoingWebhook2IdToShowStatus: undefined }); + this.setState({ outgoingWebhook2Id: undefined, outgoingWebhook2Action: undefined }); history.push(`${PLUGIN_ROOT}/outgoing_webhooks_2`); };