ChandimaPrabath commited on
Commit
3acdbcc
·
1 Parent(s): b780a2e
frontend/app/components/NexusAuth.js CHANGED
@@ -3,6 +3,7 @@ import './NexusAuth.css';
3
  import { useState, useEffect } from 'react';
4
  import NexusAuthApi from '@/app/lib/Nexus_Auth_API';
5
  import SplashScreen from '@/app/components/SplashScreen';
 
6
  import { CheckCircleIcon } from '@heroicons/react/20/solid';
7
 
8
  const SignupForm = ({ onSignup }) => {
@@ -179,10 +180,11 @@ const LoginForm = ({ onLogin }) => {
179
  );
180
  };
181
 
182
- export const NexusAuthWrapper = ({ children, toast }) => {
183
  const [isLoggedIn, setIsLoggedIn] = useState(false);
184
  const [isSignup, setIsSignup] = useState(false);
185
  const [isLoading, setIsLoading] = useState(true);
 
186
 
187
  useEffect(() => {
188
  const validateUserSession = async () => {
 
3
  import { useState, useEffect } from 'react';
4
  import NexusAuthApi from '@/app/lib/Nexus_Auth_API';
5
  import SplashScreen from '@/app/components/SplashScreen';
6
+ import { useToast } from '@/app/lib/ToastContext';
7
  import { CheckCircleIcon } from '@heroicons/react/20/solid';
8
 
9
  const SignupForm = ({ onSignup }) => {
 
180
  );
181
  };
182
 
183
+ export const NexusAuthWrapper = ({ children }) => {
184
  const [isLoggedIn, setIsLoggedIn] = useState(false);
185
  const [isSignup, setIsSignup] = useState(false);
186
  const [isLoading, setIsLoading] = useState(true);
187
+ const toast = useToast();
188
 
189
  useEffect(() => {
190
  const validateUserSession = async () => {
frontend/app/components/SplashScreen.js CHANGED
@@ -8,7 +8,7 @@ const SplashScreen = () => {
8
 
9
  useEffect(() => {
10
  // Optionally update the loading message over time
11
- const messages = ["Warming up engines...", "Fetching data...", "Almost ready!"];
12
  let index = 0;
13
  const interval = setInterval(() => {
14
  setLoadingMessage(messages[index]);
@@ -32,8 +32,8 @@ const SplashScreen = () => {
32
  };
33
 
34
  const logoStyle = {
35
- width: "200px",
36
- height: "200px",
37
  marginBottom: "1rem",
38
  borderRadius: "50%",
39
  animation: "pulse 2s infinite",
 
8
 
9
  useEffect(() => {
10
  // Optionally update the loading message over time
11
+ const messages = ["Loading...", "Please wait...", "Almost there..."];
12
  let index = 0;
13
  const interval = setInterval(() => {
14
  setLoadingMessage(messages[index]);
 
32
  };
33
 
34
  const logoStyle = {
35
+ width: "400px",
36
+ height: "400px",
37
  marginBottom: "1rem",
38
  borderRadius: "50%",
39
  animation: "pulse 2s infinite",
frontend/app/layout.js CHANGED
@@ -1,27 +1,19 @@
1
  'use client';
2
- import { Geist, Geist_Mono } from "next/font/google";
3
  import "./globals.css";
4
  import { useState } from "react";
5
  import { NexusAuthWrapper } from "@/app/components/NexusAuth";
6
- import { ToastContainer, toast, Flip } from 'react-toastify';
7
  import { CheckCircleIcon, InformationCircleIcon, ExclamationCircleIcon } from '@heroicons/react/20/solid';
8
  import Sidebar from "@/app/components/Sidebar";
9
 
10
- const geistSans = Geist({
11
- variable: "--font-geist-sans",
12
- subsets: ["latin"],
13
- });
14
-
15
- const geistMono = Geist_Mono({
16
- variable: "--font-geist-mono",
17
- subsets: ["latin"],
18
- });
19
 
20
  export default function RootLayout({ children }) {
21
  const [isCollapsed, setIsCollapsed] = useState(true);
22
  const toggleSidebar = () => {
23
  setIsCollapsed(!isCollapsed);
24
  };
 
25
  const contentStyle = {
26
  padding: "30px",
27
  flexGrow: 1,
@@ -31,7 +23,7 @@ export default function RootLayout({ children }) {
31
  transition: "margin-left 0.3s ease",
32
  };
33
 
34
- const headerStyle = {
35
  marginBottom: "30px",
36
  display: "flex",
37
  justifyContent: "center",
@@ -39,40 +31,41 @@ export default function RootLayout({ children }) {
39
 
40
  return (
41
  <html lang="en">
42
- <body className={`${geistSans.variable} ${geistMono.variable}`}>
43
- <ToastContainer
44
- transition={Flip}
45
- theme="dark"
46
- icon={({ type, theme }) => {
47
- // theme is not used in this example but you could
48
- switch (type) {
49
- case 'info':
50
- return <InformationCircleIcon className="text-indigo-400" />;
51
- case 'error':
52
- return <InformationCircleIcon className="text-red-500" />;
53
- case 'success':
54
- return <CheckCircleIcon className="h-5 w-5 text-green-500" />;
55
- case 'warning':
56
- return <ExclamationCircleIcon className="text-yellow-500" />;
57
- default:
58
- return null;
59
- }
60
- }}
61
- />
62
 
63
- <NexusAuthWrapper toast={toast}>
64
- <div className="dashboard-container">
65
- <Sidebar isCollapsed={isCollapsed} toggleSidebar={toggleSidebar} />
66
- <div style={contentStyle}>
67
- <header style={headerStyle}>
68
- <h1>Welcome to Nexus Dashboard</h1>
69
- </header>
70
- <div className="main-content">
71
- {children}
 
72
  </div>
73
  </div>
74
- </div>
75
- </NexusAuthWrapper>
76
  </body>
77
  </html>
78
  );
 
1
  'use client';
 
2
  import "./globals.css";
3
  import { useState } from "react";
4
  import { NexusAuthWrapper } from "@/app/components/NexusAuth";
5
+ import { ToastContainer, Flip } from 'react-toastify';
6
  import { CheckCircleIcon, InformationCircleIcon, ExclamationCircleIcon } from '@heroicons/react/20/solid';
7
  import Sidebar from "@/app/components/Sidebar";
8
 
9
+ import { ToastProvider } from "@/app/lib/ToastContext"; // Update with your file path
 
 
 
 
 
 
 
 
10
 
11
  export default function RootLayout({ children }) {
12
  const [isCollapsed, setIsCollapsed] = useState(true);
13
  const toggleSidebar = () => {
14
  setIsCollapsed(!isCollapsed);
15
  };
16
+
17
  const contentStyle = {
18
  padding: "30px",
19
  flexGrow: 1,
 
23
  transition: "margin-left 0.3s ease",
24
  };
25
 
26
+ const headerStyle = {
27
  marginBottom: "30px",
28
  display: "flex",
29
  justifyContent: "center",
 
31
 
32
  return (
33
  <html lang="en">
34
+ <body>
35
+ <ToastProvider>
36
+ <ToastContainer
37
+ transition={Flip}
38
+ theme="dark"
39
+ icon={({ type, theme }) => {
40
+ switch (type) {
41
+ case 'info':
42
+ return <InformationCircleIcon className="text-indigo-400" />;
43
+ case 'error':
44
+ return <InformationCircleIcon className="text-red-500" />;
45
+ case 'success':
46
+ return <CheckCircleIcon className="h-5 w-5 text-green-500" />;
47
+ case 'warning':
48
+ return <ExclamationCircleIcon className="text-yellow-500" />;
49
+ default:
50
+ return null;
51
+ }
52
+ }}
53
+ />
54
 
55
+ <NexusAuthWrapper>
56
+ <div className="dashboard-container">
57
+ <Sidebar isCollapsed={isCollapsed} toggleSidebar={toggleSidebar} />
58
+ <div style={contentStyle}>
59
+ <header style={headerStyle}>
60
+ <h1>Welcome to Nexus Dashboard</h1>
61
+ </header>
62
+ <div className="main-content">
63
+ {children}
64
+ </div>
65
  </div>
66
  </div>
67
+ </NexusAuthWrapper>
68
+ </ToastProvider>
69
  </body>
70
  </html>
71
  );
frontend/app/lib/Nexus_Auth_API.js CHANGED
@@ -139,7 +139,7 @@ const NexusAuthApi = {
139
  params: { user_id: adminId, token }
140
  });
141
  debugLog("Fetch all users successful", response.data);
142
- return response.data;
143
  } catch (error) {
144
  debugLog("Fetch all users failed", error.response?.data || error.message);
145
  throw error.response?.data || error.message;
 
139
  params: { user_id: adminId, token }
140
  });
141
  debugLog("Fetch all users successful", response.data);
142
+ return response;
143
  } catch (error) {
144
  debugLog("Fetch all users failed", error.response?.data || error.message);
145
  throw error.response?.data || error.message;
frontend/app/lib/ToastContext.js ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 'use client';
2
+
3
+ import { createContext, useContext } from "react";
4
+ import { toast } from "react-toastify";
5
+
6
+ const ToastContext = createContext();
7
+
8
+ export function ToastProvider({ children }) {
9
+ return (
10
+ <ToastContext.Provider value={toast}>
11
+ {children}
12
+ </ToastContext.Provider>
13
+ );
14
+ }
15
+
16
+ export function useToast() {
17
+ const context = useContext(ToastContext);
18
+ if (!context) {
19
+ throw new Error("useToast must be used within a ToastProvider");
20
+ }
21
+ return context;
22
+ }
frontend/app/su/[access_level]/page.js CHANGED
@@ -3,10 +3,12 @@
3
  import { useState, useEffect } from 'react';
4
  import { useParams, useRouter } from 'next/navigation';
5
  import NexusAuthApi from '@/app/lib/Nexus_Auth_API';
 
6
 
7
  export default function SuperUser() {
8
  const router = useRouter();
9
  const { access_level } = useParams();
 
10
 
11
  const [userDetails, setUserDetails] = useState({
12
  username: null,
@@ -47,6 +49,7 @@ export default function SuperUser() {
47
  window.location.reload();
48
  })
49
  .catch((error) => {
 
50
  console.error('Logout failed', error);
51
  });
52
  };
@@ -54,11 +57,18 @@ export default function SuperUser() {
54
  const getAllUsers = () => {
55
  const { userID, token } = userDetails;
56
  NexusAuthApi.getAllUsers(userID, token)
57
- .then((fetchedUsers) => {
58
- setUsers(fetchedUsers);
 
 
 
 
 
 
59
  })
60
  .catch((error) => {
61
  console.error('Get all users failed', error);
 
62
  });
63
  };
64
 
 
3
  import { useState, useEffect } from 'react';
4
  import { useParams, useRouter } from 'next/navigation';
5
  import NexusAuthApi from '@/app/lib/Nexus_Auth_API';
6
+ import { useToast } from '@/app/lib/ToastContext';
7
 
8
  export default function SuperUser() {
9
  const router = useRouter();
10
  const { access_level } = useParams();
11
+ const toast = useToast();
12
 
13
  const [userDetails, setUserDetails] = useState({
14
  username: null,
 
49
  window.location.reload();
50
  })
51
  .catch((error) => {
52
+ toast.error('Logout failed');
53
  console.error('Logout failed', error);
54
  });
55
  };
 
57
  const getAllUsers = () => {
58
  const { userID, token } = userDetails;
59
  NexusAuthApi.getAllUsers(userID, token)
60
+ .then((response) => {
61
+ if (response.status === 200) {
62
+ setUsers(response.data);
63
+ }
64
+ else if (response.status === 401) {
65
+ console.info('Insuffient permissions to get all users');
66
+ toast.error('Insuffient permissions to get all users');
67
+ }
68
  })
69
  .catch((error) => {
70
  console.error('Get all users failed', error);
71
+ toast.error('Get all users failed');
72
  });
73
  };
74