refactor alerts
This commit is contained in:
parent
28948d497f
commit
60075db602
2 changed files with 10 additions and 50 deletions
20
src/App.tsx
20
src/App.tsx
|
@ -1,22 +1,14 @@
|
||||||
import React, { useContext } from 'react';
|
import React, { useContext } from 'react';
|
||||||
import {
|
|
||||||
Page,
|
|
||||||
PageSection,
|
|
||||||
Button,
|
|
||||||
AlertVariant,
|
|
||||||
} from '@patternfly/react-core';
|
|
||||||
|
|
||||||
import { ClientList } from './clients/ClientList';
|
import { ClientList } from './clients/ClientList';
|
||||||
import { DataLoader } from './components/data-loader/DataLoader';
|
import { DataLoader } from './components/data-loader/DataLoader';
|
||||||
import { HttpClientContext } from './http-service/HttpClientContext';
|
import { HttpClientContext } from './http-service/HttpClientContext';
|
||||||
import { Client } from './clients/client-model';
|
import { Client } from './clients/client-model';
|
||||||
|
import { Page, PageSection } from '@patternfly/react-core';
|
||||||
import { Header } from './PageHeader';
|
import { Header } from './PageHeader';
|
||||||
import { PageNav } from './PageNav';
|
import { PageNav } from './PageNav';
|
||||||
import { AlertPanel } from './components/alert/AlertPanel';
|
|
||||||
import { useAlerts, withAlerts } from './components/alert/Alerts';
|
|
||||||
|
|
||||||
const AppComponent = () => {
|
export const App = () => {
|
||||||
const [alerts, add, hide] = useAlerts();
|
|
||||||
const httpClient = useContext(HttpClientContext);
|
const httpClient = useContext(HttpClientContext);
|
||||||
|
|
||||||
const loader = async () => {
|
const loader = async () => {
|
||||||
|
@ -27,18 +19,10 @@ const AppComponent = () => {
|
||||||
return (
|
return (
|
||||||
<Page header={<Header />} sidebar={<PageNav />}>
|
<Page header={<Header />} sidebar={<PageNav />}>
|
||||||
<PageSection>
|
<PageSection>
|
||||||
<AlertPanel alerts={alerts} onCloseAlert={hide} />
|
|
||||||
<DataLoader loader={loader}>
|
<DataLoader loader={loader}>
|
||||||
{(clients) => <ClientList clients={clients} />}
|
{(clients) => <ClientList clients={clients} />}
|
||||||
</DataLoader>
|
</DataLoader>
|
||||||
<Button
|
|
||||||
onClick={() => add('Crazy stuff happened', AlertVariant.danger)}
|
|
||||||
>
|
|
||||||
Click
|
|
||||||
</Button>
|
|
||||||
</PageSection>
|
</PageSection>
|
||||||
</Page>
|
</Page>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const App = withAlerts(AppComponent);
|
|
||||||
|
|
|
@ -1,48 +1,24 @@
|
||||||
import React, {
|
import { useState } from 'react';
|
||||||
useState,
|
|
||||||
FunctionComponent,
|
|
||||||
useContext,
|
|
||||||
Dispatch,
|
|
||||||
SetStateAction,
|
|
||||||
} from 'react';
|
|
||||||
import { AlertType } from './AlertPanel';
|
import { AlertType } from './AlertPanel';
|
||||||
import { AlertVariant } from '@patternfly/react-core';
|
import { AlertVariant } from '@patternfly/react-core';
|
||||||
|
|
||||||
const AlertsContext = React.createContext<
|
|
||||||
[AlertType[], Dispatch<SetStateAction<AlertType[]>>] | undefined
|
|
||||||
>(undefined);
|
|
||||||
|
|
||||||
export function withAlerts(WrappedComponent: FunctionComponent) {
|
|
||||||
return function (props: any) {
|
|
||||||
const state = useState<AlertType[]>([]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<AlertsContext.Provider value={state}>
|
|
||||||
<WrappedComponent {...props} />
|
|
||||||
</AlertsContext.Provider>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useAlerts(): [
|
export function useAlerts(): [
|
||||||
AlertType[],
|
|
||||||
(message: string, type: AlertVariant) => void,
|
(message: string, type: AlertVariant) => void,
|
||||||
|
AlertType[],
|
||||||
(key: number) => void
|
(key: number) => void
|
||||||
] {
|
] {
|
||||||
|
const [alerts, setAlerts] = useState<AlertType[]>([]);
|
||||||
const createId = () => new Date().getTime();
|
const createId = () => new Date().getTime();
|
||||||
const [alerts, setAlerts] = useContext(AlertsContext)!;
|
|
||||||
|
|
||||||
const hideAlert = (key: number) => {
|
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();
|
const key = createId();
|
||||||
setAlerts([...alerts, { key, message, variant: type }]);
|
setAlerts([...alerts, { key, message, variant }]);
|
||||||
if (type !== AlertVariant.danger) {
|
setTimeout(() => hideAlert(key), 8000);
|
||||||
setTimeout(() => hideAlert(key), 8000);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return [alerts, add, hideAlert];
|
return [add, alerts, hideAlert];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue