keycloak-scim/src/components/alert/Alerts.tsx

25 lines
706 B
TypeScript
Raw Normal View History

2020-08-08 13:52:23 +00:00
import { useState } from 'react';
2020-08-07 13:44:34 +00:00
import { AlertType } from './AlertPanel';
import { AlertVariant } from '@patternfly/react-core';
export function useAlerts(): [
(message: string, type: AlertVariant) => void,
2020-08-08 13:52:23 +00:00
AlertType[],
2020-08-07 13:44:34 +00:00
(key: number) => void
] {
2020-08-08 13:52:23 +00:00
const [alerts, setAlerts] = useState<AlertType[]>([]);
2020-08-07 13:44:34 +00:00
const createId = () => new Date().getTime();
const hideAlert = (key: number) => {
2020-08-08 13:52:23 +00:00
setAlerts((alerts) => [...alerts.filter((el) => el.key !== key)]);
2020-08-07 13:44:34 +00:00
};
2020-08-08 13:52:23 +00:00
const add = (message: string, variant: AlertVariant) => {
2020-08-07 13:44:34 +00:00
const key = createId();
2020-08-08 13:52:23 +00:00
setAlerts([...alerts, { key, message, variant }]);
setTimeout(() => hideAlert(key), 8000);
2020-08-07 13:44:34 +00:00
};
2020-08-08 13:52:23 +00:00
return [add, alerts, hideAlert];
2020-08-07 13:44:34 +00:00
}