use patternfly timout instead of own impl (#361)

This commit is contained in:
Erik Jan de Wit 2021-02-16 09:39:46 +01:00 committed by GitHub
parent 6e69bd006d
commit 688c4a0071
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 21 deletions

View file

@ -24,6 +24,7 @@ export function AlertPanel({ alerts, onCloseAlert }: AlertPanelProps) {
<Alert
key={key}
isLiveRegion
timeout={true}
variant={AlertVariant[variant]}
variantLabel=""
title={message}

View file

@ -1,10 +1,4 @@
import React, {
useState,
createContext,
ReactNode,
useContext,
useEffect,
} from "react";
import React, { useState, createContext, ReactNode, useContext } from "react";
import { AlertType, AlertPanel } from "./AlertPanel";
import { AlertVariant } from "@patternfly/react-core";
@ -25,25 +19,11 @@ type TimeOut = {
export const AlertProvider = ({ children }: { children: ReactNode }) => {
const [alerts, setAlerts] = useState<AlertType[]>([]);
const [timers, setTimers] = useState<TimeOut[]>([]);
useEffect(() => {
const timersKeys = timers.map((timer) => timer.key);
const timeOuts = alerts
.filter((alert) => !timersKeys.includes(alert.key))
.map((alert) => {
const timeOut = setTimeout(() => hideAlert(alert.key), 8000);
return { key: alert.key, timeOut };
});
setTimers([...timers, ...timeOuts]);
return () => timers.forEach((timer) => clearTimeout(timer.timeOut));
}, [alerts]);
const createId = () => new Date().getTime();
const hideAlert = (key: number) => {
setAlerts((alerts) => [...alerts.filter((el) => el.key !== key)]);
setTimers((timers) => [...timers.filter((timer) => timer.key === key)]);
};
const addAlert = (