From 23f04011f067004e15e7623e1e65f80a07bb0587 Mon Sep 17 00:00:00 2001
From: Bennet Ranft <76742318+bennetrr@users.noreply.github.com>
Date: Tue, 13 Aug 2024 14:59:09 +0200
Subject: [PATCH] fix: Toasts
---
.idea/brickinv.iml | 12 ++++
.idea/modules.xml | 8 +++
src/frontend/src/utils/ToastContext.ts | 57 ++++++++++++++++++-
.../src/utils/UsePartLoadingEffect.ts | 2 +-
src/frontend/src/utils/UseSetLoadingEffect.ts | 2 +-
5 files changed, 77 insertions(+), 4 deletions(-)
create mode 100644 .idea/brickinv.iml
create mode 100644 .idea/modules.xml
diff --git a/.idea/brickinv.iml b/.idea/brickinv.iml
new file mode 100644
index 0000000..449f465
--- /dev/null
+++ b/.idea/brickinv.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..d644a3d
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
diff --git a/src/frontend/src/utils/ToastContext.ts b/src/frontend/src/utils/ToastContext.ts
index 382a412..276f45a 100644
--- a/src/frontend/src/utils/ToastContext.ts
+++ b/src/frontend/src/utils/ToastContext.ts
@@ -1,7 +1,60 @@
import { createContext, useContext } from 'react';
-import { Toast } from 'primereact/toast';
+import { Toast, ToastMessage } from 'primereact/toast';
const ToastContext = createContext(null);
export const ToastProvider = ToastContext.Provider;
-export const useToast: (() => Toast) = () => useContext(ToastContext) as Toast;
+interface IToast {
+ /**
+ * Used to show the message.
+ * @param {ToastMessage | ToastMessage[]} message - Message to show
+ */
+ show(message: ToastMessage | ToastMessage[]): void;
+ /**
+ * Clears the all messages from Toast.
+ */
+ clear(): void;
+ /**
+ * Used to add new messages after removing all old messages.
+ * @param {ToastMessage | ToastMessage[]} message - Message to replace
+ */
+ replace(message: ToastMessage | ToastMessage[]): void;
+ /**
+ * Used to remove messages.
+ * @param {ToastMessage | ToastMessage[]} message - Message to remove
+ */
+ remove(message: ToastMessage | ToastMessage[]): void;
+}
+
+export function useToast(): IToast {
+ const toast = useContext(ToastContext);
+
+ return {
+ show(message: ToastMessage | ToastMessage[]) {
+ if (!toast) {
+ console.error('Toast is not initialized, but messages were sent:', message);
+ return;
+ }
+
+ toast.show(message);
+ },
+ clear() {
+ if (toast) {
+ toast.clear();
+ }
+ },
+ remove(message: ToastMessage | ToastMessage[]) {
+ if (toast) {
+ toast.remove(message);
+ }
+ },
+ replace(message: ToastMessage | ToastMessage[]) {
+ if (!toast) {
+ console.error('Toast is not initialized, but messages were sent:', message);
+ return;
+ }
+
+ toast.replace(message);
+ }
+ }
+}
diff --git a/src/frontend/src/utils/UsePartLoadingEffect.ts b/src/frontend/src/utils/UsePartLoadingEffect.ts
index 48cda69..e3512cf 100644
--- a/src/frontend/src/utils/UsePartLoadingEffect.ts
+++ b/src/frontend/src/utils/UsePartLoadingEffect.ts
@@ -43,7 +43,7 @@ export default function usePartLoadingEffect(set: ISet | undefined, force: boole
} finally {
setLoading(false);
}
- }, [clerk.session?.id, setStore.items.length, set]);
+ }, [clerk.session?.id, setStore, setStore.items.length, set, toast]);
return loading;
}
diff --git a/src/frontend/src/utils/UseSetLoadingEffect.ts b/src/frontend/src/utils/UseSetLoadingEffect.ts
index f21dd4d..3293718 100644
--- a/src/frontend/src/utils/UseSetLoadingEffect.ts
+++ b/src/frontend/src/utils/UseSetLoadingEffect.ts
@@ -39,7 +39,7 @@ export default function useSetLoadingEffect(force: boolean = false): boolean {
} finally {
setLoading(false);
}
- }, [clerk.session?.id, setStore.items.length]);
+ }, [clerk.session?.id, setStore, setStore.items.length, toast]);
return loading;
}