Merge pull request #183 from bluewave-labs/feat/nav-state

Feat/nav state, resovles #177
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-06-24 17:53:10 -04:00
commit 11c1819951
6 files changed
+724 -232

No files matched your search

+647 -219
View File
File diff suppressed because it is too large. Load diff
+28 -6
View File
@@ -16,20 +16,42 @@ import CheckEmail from "./Pages/CheckEmail";
import SetNewPassword from "./Pages/SetNewPassword"; import SetNewPassword from "./Pages/SetNewPassword";
import NewPasswordConfirmed from "./Pages/NewPasswordConfirmed"; import NewPasswordConfirmed from "./Pages/NewPasswordConfirmed";
import ToastComponent from "./Components/Toast"; import ToastComponent from "./Components/Toast";
import ProtectedRoute from "./Components/ProtectedRoute";
function App() { function App() {
return ( return (
<> <>
<Routes> <Routes>
<Route exact path="/" element={<HomeLayout />}> <Route exact path="/" element={<HomeLayout />}>
<Route path="monitors" element={<Monitors />} /> <Route
<Route path="incidents" element={<Incidents />} /> exact
<Route path="status" element={<Status />} /> path="/"
<Route path="integrations" element={<Integrations />} /> element={<ProtectedRoute Component={Monitors} />}
<Route path="settings" element={<Settings />} /> />
<Route
path="/monitors"
element={<ProtectedRoute Component={Monitors} />}
/>
<Route
path="incidents"
element={<ProtectedRoute Component={Incidents} />}
/>
<Route
path="status"
element={<ProtectedRoute Component={Status} />}
/>
<Route
path="integrations"
element={<ProtectedRoute Component={Integrations} />}
/>
<Route
path="settings"
element={<ProtectedRoute Component={Settings} />}
/>
</Route> </Route>
<Route exact path="/register" element={<Register />} />
<Route exact path="/login" element={<Login />} /> <Route exact path="/login" element={<Login />} />
<Route exact path="/register" element={<Register />} />
<Route exact path="/demo" element={<Demo />} /> <Route exact path="/demo" element={<Demo />} />
{/* <Route path="/toast" element={<ToastComponent />} /> */} {/* <Route path="/toast" element={<ToastComponent />} /> */}
<Route path="*" element={<NotFound />} /> <Route path="*" element={<NotFound />} />
+6 -3
View File
@@ -16,7 +16,7 @@ import ChevronDown from "../../assets/Images/Icon-chevron-down.png";
import { clearAuthState } from "../../Features/Auth/authSlice"; import { clearAuthState } from "../../Features/Auth/authSlice";
import { clearMonitorState } from "../../Features/Monitors/monitorsSlice"; import { clearMonitorState } from "../../Features/Monitors/monitorsSlice";
import { useDispatch } from "react-redux"; import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
const settings = ["Profile", "Team", "Invite Colleagues", "Logout"]; const settings = ["Profile", "Team", "Invite Colleagues", "Logout"];
@@ -37,6 +37,7 @@ function NavBar() {
const [anchorElUser, setAnchorElUser] = useState(null); const [anchorElUser, setAnchorElUser] = useState(null);
const dispatch = useDispatch(); const dispatch = useDispatch();
const navigate = useNavigate(); const navigate = useNavigate();
const authState = useSelector((state) => state.auth);
/** /**
* Handles opening the user menu. * Handles opening the user menu.
@@ -55,7 +56,7 @@ function NavBar() {
// Clear auth state // Clear auth state
dispatch(clearAuthState()); dispatch(clearAuthState());
dispatch(clearMonitorState()); dispatch(clearMonitorState());
navigate("/"); navigate("/login");
}; };
/** /**
@@ -156,7 +157,9 @@ function NavBar() {
className="icon-button-avatar" className="icon-button-avatar"
style={{ width: "25px", height: "25px" }} style={{ width: "25px", height: "25px" }}
/> />
<div className="icon-button-toggle-title">Jackie Dawn</div> <div className="icon-button-toggle-title">
{authState.user.firstname} {authState.user.lastname}
</div>
<img <img
className="icon-button-toggle-pic" className="icon-button-toggle-pic"
src={ChevronDown} src={ChevronDown}
@@ -0,0 +1,30 @@
import { Navigate } from "react-router-dom";
import { useSelector } from "react-redux";
import PropTypes from "prop-types";
/**
* ProtectedRoute is a higher-order component that wraps the `Route` component from `react-router-dom`
* to create a protected route. It uses Redux to check if the user is authenticated. If the user is
* authenticated, it renders the component passed to it; otherwise, it redirects the user to the login page.
*
* @param {Object} props - The props passed to the ProtectedRoute component.
* @param {React.ComponentType} props.Component - The component to render if the user is authenticated.
* @param {Object} rest - The remaining props passed to the Route component.
* @returns {React.ReactElement} A Route component that conditionally renders the passed Component or redirects to the login page.
*/
const ProtectedRoute = ({ Component, ...rest }) => {
const authState = useSelector((state) => state.auth);
return authState.authToken ? (
<Component {...rest} />
) : (
<Navigate to="/login" replace />
);
};
ProtectedRoute.propTypes = {
Component: PropTypes.elementType.isRequired,
};
export default ProtectedRoute;
@@ -86,6 +86,7 @@ const monitorsSlice = createSlice({
state.monitors = action.payload.data; state.monitors = action.payload.data;
}) })
.addCase(getMonitorsByUserId.rejected, (state, action) => { .addCase(getMonitorsByUserId.rejected, (state, action) => {
console.log(action);
state.isLoading = false; state.isLoading = false;
state.success = action.payload.success; state.success = action.payload.success;
state.msg = action.payload.msg; state.msg = action.payload.msg;
+12 -4
View File
@@ -52,9 +52,7 @@ const Login = () => {
await loginValidation.validateAsync(form, { abortEarly: false }); await loginValidation.validateAsync(form, { abortEarly: false });
const action = await dispatch(login(form)); const action = await dispatch(login(form));
if (action.meta.requestStatus === "fulfilled") { if (action.meta.requestStatus === "fulfilled") {
const token = action.payload.data; navigate("/monitors");
localStorage.setItem("token", token);
navigate("/");
} }
if (action.meta.requestStatus === "rejected") { if (action.meta.requestStatus === "rejected") {
const error = new Error("Request rejected"); const error = new Error("Request rejected");
@@ -82,6 +80,9 @@ const Login = () => {
setForm(newForm); setForm(newForm);
}; };
const handleSignupClick = () => {
navigate("/register");
};
return ( return (
<div className="login-page"> <div className="login-page">
<BackgroundPattern></BackgroundPattern> <BackgroundPattern></BackgroundPattern>
@@ -139,7 +140,14 @@ const Login = () => {
<div className="login-form-v3-spacing" /> <div className="login-form-v3-spacing" />
<div className="new-account-option"> <div className="new-account-option">
Don’t have an account? Don’t have an account?
<span className="new-account-option-span">Sign up</span> <span
onClick={() => {
navigate("/register");
}}
className="new-account-option-span"
>
Sign up
</span>
</div> </div>
</form> </form>
</div> </div>