Skip to content

Commit

Permalink
🐞 fix (CHASE Frontend): add disable/enable toasts on certain pages (e…
Browse files Browse the repository at this point in the history
….g. the presentation mode page) (#130)

branch: 115-surpress-toastsnotifications-on-presentation-view
  • Loading branch information
Strehk authored Mar 19, 2024
1 parent 6578315 commit be4fef8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function CommitteePresentationMode({
params: { conferenceId: string; committeeId: string };
}) {
const { LL } = useI18nContext();
const { toastError } = useToast();
const { disableToastsOnCurrentPage } = useToast();
const { backend } = useBackend();
const { category } = useContext(StatusTimer);

Expand All @@ -51,7 +51,7 @@ export default function CommitteePresentationMode({
setCommitteeData(response.data);
})
.catch((error) => {
toastError(error);
console.error(error);
});
}

Expand All @@ -64,11 +64,12 @@ export default function CommitteePresentationMode({
setAgendaItem(response.data);
})
.catch((error) => {
toastError(error);
console.error(error);
});
}

useEffect(() => {
disableToastsOnCurrentPage();
getCommitteeData();
getAgendaItems();
const intervalAPICall = setInterval(() => {
Expand Down
30 changes: 28 additions & 2 deletions chase/frontend/contexts/toast.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"use client";
import React, { createContext, useContext, useRef } from "react";
import React, { createContext, useContext, useRef, useState } from "react";
import { Toast, ToastMessage } from "primereact/toast";
import { useI18nContext } from "@/i18n/i18n-react";
import { usePathname } from "next/navigation";

export const ToastContext = createContext({} as ToastContextType);
export const useToast = () => useContext(ToastContext);
Expand All @@ -13,8 +14,22 @@ export const useToast = () => useContext(ToastContext);
export const ToastProvider = ({ children }: { children: React.ReactNode }) => {
const toast = useRef<Toast>(null);
const { LL } = useI18nContext();
const pathname = usePathname();

const [disabledPages, setDisabledPages] = useState<string[]>([]);

const disableToastsOnCurrentPage = () => {
setDisabledPages((prevDisabledPages) => [...prevDisabledPages, pathname]);
};

const enableToastsOnCurrentPage = () => {
setDisabledPages((prevDisabledPages) =>
prevDisabledPages.filter((page) => page !== pathname),
);
};

const showToast = (message: ToastMessage) => {
if (disabledPages.includes(pathname)) return;
toast.current?.show(message);
};

Expand All @@ -24,6 +39,7 @@ export const ToastProvider = ({ children }: { children: React.ReactNode }) => {

const toastError = (error: Error, message?: string) => {
console.error(error);
if (disabledPages.includes(pathname)) return;
showToast({
severity: "error",
summary: LL.admin.onboarding.error.title(),
Expand All @@ -32,7 +48,15 @@ export const ToastProvider = ({ children }: { children: React.ReactNode }) => {
};

return (
<ToastContext.Provider value={{ showToast, clearToast, toastError }}>
<ToastContext.Provider
value={{
showToast,
clearToast,
toastError,
disableToastsOnCurrentPage,
enableToastsOnCurrentPage,
}}
>
<Toast ref={toast} />
{children}
</ToastContext.Provider>
Expand All @@ -43,4 +67,6 @@ export interface ToastContextType {
showToast: (message: ToastMessage) => void;
clearToast: () => void;
toastError: (error: Error, message?: string) => void;
disableToastsOnCurrentPage: () => void;
enableToastsOnCurrentPage: () => void;
}

0 comments on commit be4fef8

Please sign in to comment.