diff --git a/src/App.tsx b/src/App.tsx index 3b4e665f0a..142780cc86 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,22 +1,14 @@ import React, { useContext } from 'react'; -import { - Page, - PageSection, - Button, - AlertVariant, -} from '@patternfly/react-core'; import { ClientList } from './clients/ClientList'; import { DataLoader } from './components/data-loader/DataLoader'; import { HttpClientContext } from './http-service/HttpClientContext'; import { Client } from './clients/client-model'; +import { Page, PageSection } from '@patternfly/react-core'; import { Header } from './PageHeader'; import { PageNav } from './PageNav'; -import { AlertPanel } from './components/alert/AlertPanel'; -import { useAlerts, withAlerts } from './components/alert/Alerts'; -const AppComponent = () => { - const [alerts, add, hide] = useAlerts(); +export const App = () => { const httpClient = useContext(HttpClientContext); const loader = async () => { @@ -27,18 +19,10 @@ const AppComponent = () => { return ( } sidebar={}> - {(clients) => } - ); }; - -export const App = withAlerts(AppComponent); diff --git a/src/components/alert/Alerts.tsx b/src/components/alert/Alerts.tsx index 771c046d93..efdf25c180 100644 --- a/src/components/alert/Alerts.tsx +++ b/src/components/alert/Alerts.tsx @@ -1,48 +1,24 @@ -import React, { - useState, - FunctionComponent, - useContext, - Dispatch, - SetStateAction, -} from 'react'; +import { useState } from 'react'; import { AlertType } from './AlertPanel'; import { AlertVariant } from '@patternfly/react-core'; -const AlertsContext = React.createContext< - [AlertType[], Dispatch>] | undefined ->(undefined); - -export function withAlerts(WrappedComponent: FunctionComponent) { - return function (props: any) { - const state = useState([]); - - return ( - - - - ); - }; -} - export function useAlerts(): [ - AlertType[], (message: string, type: AlertVariant) => void, + AlertType[], (key: number) => void ] { + const [alerts, setAlerts] = useState([]); const createId = () => new Date().getTime(); - const [alerts, setAlerts] = useContext(AlertsContext)!; const hideAlert = (key: number) => { - setAlerts([...alerts.filter((el) => el.key !== key)]); + setAlerts((alerts) => [...alerts.filter((el) => el.key !== key)]); }; - const add = (message: string, type: AlertVariant) => { + const add = (message: string, variant: AlertVariant) => { const key = createId(); - setAlerts([...alerts, { key, message, variant: type }]); - if (type !== AlertVariant.danger) { - setTimeout(() => hideAlert(key), 8000); - } + setAlerts([...alerts, { key, message, variant }]); + setTimeout(() => hideAlert(key), 8000); }; - return [alerts, add, hideAlert]; + return [add, alerts, hideAlert]; }