2021-08-10 11:18:48 +00:00
|
|
|
import React, { createContext, FunctionComponent, useState } from "react";
|
2021-07-28 12:01:42 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { AlertVariant } from "@patternfly/react-core";
|
|
|
|
import type { AxiosError } from "axios";
|
|
|
|
|
2021-07-21 09:30:18 +00:00
|
|
|
import useRequiredContext from "../../utils/useRequiredContext";
|
2022-02-23 10:29:50 +00:00
|
|
|
import useSetTimeout from "../../utils/useSetTimeout";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { AlertPanel, AlertType } from "./AlertPanel";
|
2020-08-07 13:44:34 +00:00
|
|
|
|
2020-10-06 19:25:05 +00:00
|
|
|
type AlertProps = {
|
2021-02-17 07:17:04 +00:00
|
|
|
addAlert: (
|
|
|
|
message: string,
|
|
|
|
variant?: AlertVariant,
|
|
|
|
description?: string
|
|
|
|
) => void;
|
2021-07-28 12:01:42 +00:00
|
|
|
|
|
|
|
addError: (message: string, error: any) => void;
|
2020-10-06 19:25:05 +00:00
|
|
|
};
|
|
|
|
|
2021-07-21 09:30:18 +00:00
|
|
|
export const AlertContext = createContext<AlertProps | undefined>(undefined);
|
2020-10-06 19:25:05 +00:00
|
|
|
|
2021-07-21 09:30:18 +00:00
|
|
|
export const useAlerts = () => useRequiredContext(AlertContext);
|
2020-10-06 19:25:05 +00:00
|
|
|
|
2021-08-10 11:18:48 +00:00
|
|
|
export const AlertProvider: FunctionComponent = ({ children }) => {
|
2021-07-28 12:01:42 +00:00
|
|
|
const { t } = useTranslation();
|
2020-08-08 13:52:23 +00:00
|
|
|
const [alerts, setAlerts] = useState<AlertType[]>([]);
|
2022-02-23 10:29:50 +00:00
|
|
|
const setTimeout = useSetTimeout();
|
2020-11-05 21:26:43 +00:00
|
|
|
|
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-10-06 19:25:05 +00:00
|
|
|
const addAlert = (
|
2020-09-15 19:44:28 +00:00
|
|
|
message: string,
|
2021-07-01 06:48:03 +00:00
|
|
|
variant: AlertVariant = AlertVariant.success,
|
2021-02-17 07:17:04 +00:00
|
|
|
description?: string
|
2020-09-15 19:44:28 +00:00
|
|
|
) => {
|
2021-07-01 06:48:03 +00:00
|
|
|
const key = createId();
|
|
|
|
setTimeout(() => hideAlert(key), 8000);
|
|
|
|
setAlerts([{ key, message, variant, description }, ...alerts]);
|
2020-08-07 13:44:34 +00:00
|
|
|
};
|
|
|
|
|
2021-07-28 12:01:42 +00:00
|
|
|
const addError = (message: string, error: Error | AxiosError) => {
|
|
|
|
addAlert(
|
|
|
|
t(message, {
|
|
|
|
error:
|
|
|
|
"response" in error
|
|
|
|
? error.response?.data?.errorMessage || error.response?.data?.error
|
|
|
|
: error,
|
|
|
|
}),
|
|
|
|
AlertVariant.danger
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-10-06 19:25:05 +00:00
|
|
|
return (
|
2021-07-28 12:01:42 +00:00
|
|
|
<AlertContext.Provider value={{ addAlert, addError }}>
|
2020-10-06 19:25:05 +00:00
|
|
|
<AlertPanel alerts={alerts} onCloseAlert={hideAlert} />
|
|
|
|
{children}
|
|
|
|
</AlertContext.Provider>
|
|
|
|
);
|
|
|
|
};
|