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";
|
2022-05-10 09:50:00 +00:00
|
|
|
import axios from "axios";
|
2021-07-28 12:01:42 +00:00
|
|
|
import type { AxiosError } from "axios";
|
|
|
|
|
2021-07-21 09:30:18 +00:00
|
|
|
import useRequiredContext from "../../utils/useRequiredContext";
|
2022-07-13 10:17:21 +00:00
|
|
|
import { AlertPanel } from "./AlertPanel";
|
2020-08-07 13:44:34 +00:00
|
|
|
|
2022-05-30 11:07:33 +00:00
|
|
|
export type AddAlertFunction = (
|
|
|
|
message: string,
|
|
|
|
variant?: AlertVariant,
|
|
|
|
description?: string
|
|
|
|
) => void;
|
2021-07-28 12:01:42 +00:00
|
|
|
|
2022-05-30 11:07:33 +00:00
|
|
|
export type AddErrorFunction = (message: string, error: any) => void;
|
|
|
|
|
|
|
|
type AlertProps = {
|
|
|
|
addAlert: AddAlertFunction;
|
|
|
|
addError: AddErrorFunction;
|
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
|
|
|
|
2022-07-13 10:17:21 +00:00
|
|
|
export type AlertType = {
|
|
|
|
id: number;
|
|
|
|
message: string;
|
|
|
|
variant: AlertVariant;
|
|
|
|
description?: string;
|
|
|
|
};
|
|
|
|
|
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[]>([]);
|
2020-08-07 13:44:34 +00:00
|
|
|
|
2022-07-13 10:17:21 +00:00
|
|
|
const hideAlert = (id: number) => {
|
|
|
|
setAlerts((alerts) => alerts.filter((alert) => alert.id !== id));
|
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
|
|
|
) => {
|
2022-07-13 10:17:21 +00:00
|
|
|
setAlerts([
|
|
|
|
{
|
|
|
|
id: Math.random(),
|
|
|
|
message,
|
|
|
|
variant,
|
|
|
|
description,
|
|
|
|
},
|
|
|
|
...alerts,
|
|
|
|
]);
|
2020-08-07 13:44:34 +00:00
|
|
|
};
|
|
|
|
|
2022-05-10 09:50:00 +00:00
|
|
|
const addError = (message: string, error: Error | AxiosError | string) => {
|
2021-07-28 12:01:42 +00:00
|
|
|
addAlert(
|
|
|
|
t(message, {
|
2022-05-10 09:50:00 +00:00
|
|
|
error: getErrorMessage(error),
|
2021-07-28 12:01:42 +00:00
|
|
|
}),
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
};
|
2022-05-10 09:50:00 +00:00
|
|
|
|
|
|
|
function getErrorMessage(
|
|
|
|
error: Error | AxiosError<Record<string, unknown>> | string
|
|
|
|
) {
|
|
|
|
if (typeof error === "string") {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!axios.isAxiosError(error)) {
|
|
|
|
return error.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
const responseData = error.response?.data ?? {};
|
|
|
|
|
|
|
|
for (const key of ["error_description", "errorMessage", "error"]) {
|
|
|
|
const value = responseData[key];
|
|
|
|
|
|
|
|
if (typeof value === "string") {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return error.message;
|
|
|
|
}
|