Automatically dismiss alerts (#2955)
This commit is contained in:
parent
cd86e1a3ad
commit
1dc2964757
2 changed files with 26 additions and 21 deletions
|
@ -5,25 +5,19 @@ import {
|
||||||
AlertActionCloseButton,
|
AlertActionCloseButton,
|
||||||
AlertVariant,
|
AlertVariant,
|
||||||
} from "@patternfly/react-core";
|
} from "@patternfly/react-core";
|
||||||
|
import type { AlertType } from "./Alerts";
|
||||||
export type AlertType = {
|
|
||||||
key: string;
|
|
||||||
message: string;
|
|
||||||
variant: AlertVariant;
|
|
||||||
description?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type AlertPanelProps = {
|
type AlertPanelProps = {
|
||||||
alerts: AlertType[];
|
alerts: AlertType[];
|
||||||
onCloseAlert: (key: string) => void;
|
onCloseAlert: (id: number) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function AlertPanel({ alerts, onCloseAlert }: AlertPanelProps) {
|
export function AlertPanel({ alerts, onCloseAlert }: AlertPanelProps) {
|
||||||
return (
|
return (
|
||||||
<AlertGroup isToast>
|
<AlertGroup isToast>
|
||||||
{alerts.map(({ key, variant, message, description }) => (
|
{alerts.map(({ id, variant, message, description }) => (
|
||||||
<Alert
|
<Alert
|
||||||
key={key}
|
key={id}
|
||||||
isLiveRegion
|
isLiveRegion
|
||||||
variant={AlertVariant[variant]}
|
variant={AlertVariant[variant]}
|
||||||
variantLabel=""
|
variantLabel=""
|
||||||
|
@ -31,9 +25,11 @@ export function AlertPanel({ alerts, onCloseAlert }: AlertPanelProps) {
|
||||||
actionClose={
|
actionClose={
|
||||||
<AlertActionCloseButton
|
<AlertActionCloseButton
|
||||||
title={message}
|
title={message}
|
||||||
onClose={() => onCloseAlert(key)}
|
onClose={() => onCloseAlert(id)}
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
|
timeout
|
||||||
|
onTimeout={() => onCloseAlert(id)}
|
||||||
>
|
>
|
||||||
{description && <p>{description}</p>}
|
{description && <p>{description}</p>}
|
||||||
</Alert>
|
</Alert>
|
||||||
|
|
|
@ -5,8 +5,7 @@ import axios from "axios";
|
||||||
import type { AxiosError } from "axios";
|
import type { AxiosError } from "axios";
|
||||||
|
|
||||||
import useRequiredContext from "../../utils/useRequiredContext";
|
import useRequiredContext from "../../utils/useRequiredContext";
|
||||||
import useSetTimeout from "../../utils/useSetTimeout";
|
import { AlertPanel } from "./AlertPanel";
|
||||||
import { AlertPanel, AlertType } from "./AlertPanel";
|
|
||||||
|
|
||||||
export type AddAlertFunction = (
|
export type AddAlertFunction = (
|
||||||
message: string,
|
message: string,
|
||||||
|
@ -25,15 +24,19 @@ export const AlertContext = createContext<AlertProps | undefined>(undefined);
|
||||||
|
|
||||||
export const useAlerts = () => useRequiredContext(AlertContext);
|
export const useAlerts = () => useRequiredContext(AlertContext);
|
||||||
|
|
||||||
|
export type AlertType = {
|
||||||
|
id: number;
|
||||||
|
message: string;
|
||||||
|
variant: AlertVariant;
|
||||||
|
description?: string;
|
||||||
|
};
|
||||||
|
|
||||||
export const AlertProvider: FunctionComponent = ({ children }) => {
|
export const AlertProvider: FunctionComponent = ({ children }) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [alerts, setAlerts] = useState<AlertType[]>([]);
|
const [alerts, setAlerts] = useState<AlertType[]>([]);
|
||||||
const setTimeout = useSetTimeout();
|
|
||||||
|
|
||||||
const createId = () => Math.random().toString(16);
|
const hideAlert = (id: number) => {
|
||||||
|
setAlerts((alerts) => alerts.filter((alert) => alert.id !== id));
|
||||||
const hideAlert = (key: string) => {
|
|
||||||
setAlerts((alerts) => [...alerts.filter((el) => el.key !== key)]);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const addAlert = (
|
const addAlert = (
|
||||||
|
@ -41,9 +44,15 @@ export const AlertProvider: FunctionComponent = ({ children }) => {
|
||||||
variant: AlertVariant = AlertVariant.success,
|
variant: AlertVariant = AlertVariant.success,
|
||||||
description?: string
|
description?: string
|
||||||
) => {
|
) => {
|
||||||
const key = createId();
|
setAlerts([
|
||||||
setTimeout(() => hideAlert(key), 8000);
|
{
|
||||||
setAlerts([{ key, message, variant, description }, ...alerts]);
|
id: Math.random(),
|
||||||
|
message,
|
||||||
|
variant,
|
||||||
|
description,
|
||||||
|
},
|
||||||
|
...alerts,
|
||||||
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
const addError = (message: string, error: Error | AxiosError | string) => {
|
const addError = (message: string, error: Error | AxiosError | string) => {
|
||||||
|
|
Loading…
Reference in a new issue