From 4398010a9f9672cc071529e701ada70a20fd07a7 Mon Sep 17 00:00:00 2001 From: Jasneet Singh Date: Mon, 6 Jan 2025 16:38:58 -0500 Subject: [PATCH] Inform Admin incase of server issue. --- Client/src/Utils/NetworkService.js | 45 +++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/Client/src/Utils/NetworkService.js b/Client/src/Utils/NetworkService.js index 4812947d0..edef40f0b 100644 --- a/Client/src/Utils/NetworkService.js +++ b/Client/src/Utils/NetworkService.js @@ -1,14 +1,20 @@ +import { toast } from 'react-toastify'; import axios from "axios"; const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL; const FALLBACK_BASE_URL = "http://localhost:5000/api/v1"; import { clearAuthState } from "../Features/Auth/authSlice"; import { clearUptimeMonitorState } from "../Features/UptimeMonitors/uptimeMonitorsSlice"; + class NetworkService { constructor(store, dispatch, navigate) { this.store = store; this.dispatch = dispatch; this.navigate = navigate; let baseURL = BASE_URL; + this.lastToastTimestamp = 0; + this.errorOccurred = false; + this.user = this.store.getState().auth?.user; + this.isAdmin = (this.user?.role?.includes("admin") ?? false) || (this.user?.role?.includes("superadmin") ?? false); this.axiosInstance = axios.create(); this.setBaseUrl(baseURL); this.unsubscribe = store.subscribe(() => { @@ -21,15 +27,17 @@ class NetworkService { baseURL = FALLBACK_BASE_URL; } this.setBaseUrl(baseURL); - }); + }); this.axiosInstance.interceptors.response.use( - (response) => response, - (error) => { - if (error.response && error.response.status === 401) { - dispatch(clearAuthState()); - dispatch(clearUptimeMonitorState()); - navigate("/login"); + (response) => { + if (this.isAdmin && this.errorOccurred && response.status >= 200 && response.status < 300) { + this.debounceToast("Checkmate server is running properly.", "success"); + this.errorOccurred = false;// Reset error flag if a successful response is received after errors } + return response; + }, + (error) => { + this.handleError(error); return Promise.reject(error); } ); @@ -39,6 +47,29 @@ class NetworkService { this.axiosInstance.defaults.baseURL = url; }; + handleError(error) { + if (error.response) { + const status = error.response.status; + if (status === 401) { + this.dispatch(clearAuthState()); + this.dispatch(clearUptimeMonitorState()); + this.navigate("/login"); + } else if (this.isAdmin && status >= 500) { + this.debounceToast("Checkmate server is not running or has issues. Please check.", "error"); + this.errorOccurred = true; + } + } + } + + debounceToast(message, type, debounceDuration = 5000) { + const now = Date.now(); + if (now - this.lastToastTimestamp > debounceDuration) { + this.lastToastTimestamp = now; + type === "success" ? toast.success(message) : toast.error(message); + } + } + + cleanup() { if (this.unsubscribe) { this.unsubscribe();