Skip to content

Commit

Permalink
dismiss toast if export cr gives 404
Browse files Browse the repository at this point in the history
  • Loading branch information
invincibleJai committed Sep 1, 2021
1 parent e0dec06 commit 6645cda
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export const ExportAppContextProvider = ExportAppContext.Provider;
export const useExportAppFormToast = () => {
const toast = useToast();
const { t } = useTranslation();
const [currentToasts, setCurrentToasts] = React.useState<{ toastId: string; toastKey: string }[]>(
[],
);
const [exportAppToast, setExportAppToast, exportAppToastLoaded] = useUserSettings<
ExportAppUserSettings
>(`${USERSETTINGS_PREFIX}.exportApp`, {}, true);
Expand All @@ -40,9 +43,21 @@ export const useExportAppFormToast = () => {
exportAppWatchResources,
);

React.useEffect(() => {
if (extraResources) {
const keys = Object.keys(extraResources);
keys.forEach((k) => {
if (extraResources[k].loadError?.response?.status === 404) {
const toastDismiss = currentToasts.find((curToast) => curToast.toastKey === k);
toastDismiss?.toastId && toast.removeToast(toastDismiss.toastId);
}
});
}
}, [currentToasts, extraResources, toast]);

const showDownloadToast = React.useCallback(
(expNamespace: string, routeUrl: string) => {
toast.addToast({
(expNamespace: string, routeUrl: string, key: string) => {
const toastId = toast.addToast({
variant: AlertVariant.info,
title: t('topology~Export Application'),
content: t(
Expand All @@ -63,6 +78,7 @@ export const useExportAppFormToast = () => {
},
],
});
setCurrentToasts((toasts) => [...toasts, { toastId, toastKey: key }]);
},
[t, toast],
);
Expand All @@ -84,7 +100,11 @@ export const useExportAppFormToast = () => {
showDownloadToast(
extraResources[k].data.metadata.namespace,
extraResources[k].data.status.route,
k,
);
} else if (extraResources[k].loadError?.response?.status === 404) {
const exportAppToastConfig = _.omit(exportAppToast, k);
setExportAppToast(exportAppToastConfig);
}
});
}
Expand Down

0 comments on commit 6645cda

Please sign in to comment.