From 893e7093d82a347fa76935bdc616b01ed14e6a3e Mon Sep 17 00:00:00 2001 From: Delirium Date: Mon, 18 Mar 2024 16:10:13 +0100 Subject: [PATCH] fix(ui): fix alert creation modal (#1972) --- .../Alerts/AlertFormModal/AlertFormModal.tsx | 31 +++++-------------- .../DashboardView/DashboardView.tsx | 22 ++++++++++--- frontend/app/components/Modal/index.tsx | 10 ++++-- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/frontend/app/components/Alerts/AlertFormModal/AlertFormModal.tsx b/frontend/app/components/Alerts/AlertFormModal/AlertFormModal.tsx index 93999281a..3821a3f45 100644 --- a/frontend/app/components/Alerts/AlertFormModal/AlertFormModal.tsx +++ b/frontend/app/components/Alerts/AlertFormModal/AlertFormModal.tsx @@ -78,30 +78,15 @@ function AlertFormModal(props: Props) { }; return ( - - {'Create Alert'} - - } - isDisplayed={showModal} + - ) - } + onDelete={onDelete} /> ); } diff --git a/frontend/app/components/Dashboard/components/DashboardView/DashboardView.tsx b/frontend/app/components/Dashboard/components/DashboardView/DashboardView.tsx index 3ea2ff765..6018ef5f7 100644 --- a/frontend/app/components/Dashboard/components/DashboardView/DashboardView.tsx +++ b/frontend/app/components/Dashboard/components/DashboardView/DashboardView.tsx @@ -24,7 +24,7 @@ type Props = IProps & RouteComponentProps; function DashboardView(props: Props) { const { siteId, dashboardId } = props; const { dashboardStore } = useStore(); - const { showModal } = useModal(); + const { showModal, hideModal } = useModal(); const showAlertModal = dashboardStore.showAlertModal; const loading = dashboardStore.fetchingDashboard; @@ -40,6 +40,22 @@ function DashboardView(props: Props) { }); }; + useEffect(() => { + if (showAlertModal) { + showModal( + { + hideModal(); + dashboardStore.toggleAlertModal(false) + }} + />, + { right: false, width: 580 }, + () => dashboardStore.toggleAlertModal(false) + ) + } + }, [showAlertModal]) + const pushQuery = () => { if (!queryParams.has('modal')) props.history.push('?modal=addMetric'); }; @@ -85,10 +101,6 @@ function DashboardView(props: Props) { onEditHandler={onAddWidgets} id="report" /> - dashboardStore.toggleAlertModal(false)} - /> ); diff --git a/frontend/app/components/Modal/index.tsx b/frontend/app/components/Modal/index.tsx index 3df7a98cb..e8de0b021 100644 --- a/frontend/app/components/Modal/index.tsx +++ b/frontend/app/components/Modal/index.tsx @@ -8,30 +8,36 @@ const ModalContext = createContext({ right: true, onClose: () => {}, }, - showModal: (component: any, props: any) => {}, + showModal: (component: any, props: Record, onClose?: () => void) => {}, hideModal: () => {}, }); export class ModalProvider extends Component { + onCloseCb = () => null; + handleKeyDown = (e: any) => { if (e.keyCode === 27) { this.hideModal(); } }; - showModal = (component, props = { right: true }) => { + showModal = (component, props = { right: true }, onClose?: () => void) => { this.setState({ component, props, }); document.addEventListener('keydown', this.handleKeyDown); document.querySelector('body').style.overflow = 'hidden'; + this.onCloseCb = onClose || this.onCloseCb; }; hideModal = () => { document.removeEventListener('keydown', this.handleKeyDown); document.querySelector('body').style.overflow = 'visible'; const { props } = this.state; + if (this.onCloseCb) { + this.onCloseCb(); + } if (props.onClose) { props.onClose(); }