Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

feat: ability to hide switch tools #63

Merged
merged 14 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ This app exports functions to be imported by other microapps.
- `getAuthUserProfile` - returns a promise which resolves to the user profile object
- `disableSidebarForRoute` - disable (remove) sidebar for some route
- `enableSidebarForRoute` - enable sidebar for the route, which was previously disabled
- `disableNavigationForRoute` - disable (remove) navigation for some route
- `enableNavigationForRoute` - enable (remove) navigation for some route

#### How to export

Expand Down
23 changes: 9 additions & 14 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import React, { useState, useCallback, useMemo, useEffect } from "react";
import _ from "lodash";
import MainMenu from "./components/MainMenu";
import NavBar from "./components/NavBar";
import { matchPath, Router, useLocation } from "@reach/router";
import { navigate, Router, useLocation } from "@reach/router";
import { useSelector } from "react-redux";
import useMatchSomeRoute from "./hooks/useMatchSomeRoute";
import NotificationsModal from "./components/NotificationsModal";
import "./styles/main.module.scss";
import { checkOnboardingPath } from "./utils";

const App = () => {
// all menu options
Expand All @@ -19,14 +18,18 @@ const App = () => {
const apps = useMemo(() => _.flatMap(menu, "apps"), [menu]);
// list of routes where we have to disabled sidebar
const disabledRoutes = useSelector((state) => state.menu.disabledRoutes);
// list of routes where we have to disabled navigations
const disabledNavigations = useSelector(
(state) => state.menu.disabledNavigations
);
// user profile information
const auth = useSelector((state) => state.auth);
// `true` is sidebar has to be disabled for the current route
const isSideBarDisabled = useMatchSomeRoute(disabledRoutes);
// `true` is navigation has to be disabled for the current route
const isNavigationDisabled = useMatchSomeRoute(disabledNavigations);
// Left sidebar collapse state
const [sidebarCollapsed, setSidebarCollapsed] = useState(false);
// hide switch tools and notification when user is onboarding
const [hideSwitchTools, setHideSwitchTools] = useState(false);
// Toggle left sidebar callback
const toggleSidebar = useCallback(() => {
setSidebarCollapsed(!sidebarCollapsed);
Expand All @@ -47,19 +50,11 @@ const App = () => {
} else {
document.body.classList.remove("no-sidebar");
}
}, [isSideBarDisabled]);

useEffect(() => {
if (matchPath("onboard/*", location.pathname)) {
setHideSwitchTools(true);
} else {
setHideSwitchTools(false);
}
}, [location]);
}, [isSideBarDisabled, location.pathname]);

return (
<>
<NavBar hideSwitchTools={hideSwitchTools} />
<NavBar hideSwitchTools={isNavigationDisabled} />
{!isSideBarDisabled && (
<div className="main-menu-wrapper">
<Router>
Expand Down
24 changes: 24 additions & 0 deletions src/actions/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,28 @@ export default {
type: ACTIONS.MENU.ENABLE_SIDEBAR_FOR_ROUTE,
payload: route,
}),

/**
* Disable navigation for route.
*
* @param {String} route route path
*
* @returns {{ type: String, payload: any }} action object
*/
disableNavigationForRoute: (route) => ({
type: ACTIONS.MENU.DISABLE_NAVIGATION_FOR_ROUTE,
payload: route,
}),

/**
* Enable navigation for route.
*
* @param {String} route route path
*
* @returns {{ type: String, payload: any }} action object
*/
enableNavigationForRoute: (route) => ({
type: ACTIONS.MENU.ENABLE_NAVIGATION_FOR_ROUTE,
payload: route,
}),
};
2 changes: 1 addition & 1 deletion src/components/NavBar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Shows global top navigation bar with all apps menu, logo and user menu.
*/
import React, {
import React, {
useState,
useCallback,
Fragment,
Expand Down
2 changes: 2 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ export const ACTIONS = {
SET_APP_MENU: "SET_APP_MENU",
DISABLE_SIDEBAR_FOR_ROUTE: "DISABLE_SIDEBAR_FOR_ROUTE",
ENABLE_SIDEBAR_FOR_ROUTE: "ENABLE_SIDEBAR_FOR_ROUTE",
DISABLE_NAVIGATION_FOR_ROUTE: "DISABLE_NAVIGATION_FOR_ROUTE",
ENABLE_NAVIGATION_FOR_ROUTE: "ENABLE_NAVIGATION_FOR_ROUTE",
},
};
30 changes: 30 additions & 0 deletions src/reducers/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ACTIONS, APP_CATEGORIES } from "../constants";
const initialState = {
categories: APP_CATEGORIES, // Default Apps Menu structure.
disabledRoutes: [],
disabledNavigations: [],
};

/**
Expand Down Expand Up @@ -132,6 +133,35 @@ const menuReducer = (state = initialState, action) => {
};
}

case ACTIONS.MENU.DISABLE_NAVIGATION_FOR_ROUTE: {
// if route is already disabled, don't do anything
if (state.disabledNavigations.indexOf(action.payload) > -1) {
return state;
}

return {
...state,
// add route to the disabled list
disabledNavigations: [...state.disabledNavigations, action.payload],
};
}

case ACTIONS.MENU.ENABLE_NAVIGATION_FOR_ROUTE: {
// if route is not disabled, don't do anything
if (state.disabledNavigations.indexOf(action.payload) === -1) {
return state;
}

return {
...state,
// remove the route from the disabled list
disabledNavigations: _.without(
state.disabledNavigations,
action.payload
),
};
}

default:
return state;
}
Expand Down
4 changes: 4 additions & 0 deletions src/topcoder-micro-frontends-navbar-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
getAuthUserProfile,
setUserProfilePhoto,
setNotificationPlatform,
disableNavigationForRoute,
enableNavigationForRoute,
} from "./utils/exports";

import { login, businessLogin, logout } from "./utils";
Expand Down Expand Up @@ -47,5 +49,7 @@ export {
disableSidebarForRoute,
enableSidebarForRoute,
setNotificationPlatform,
disableNavigationForRoute,
enableNavigationForRoute,
PLATFORM,
};
6 changes: 5 additions & 1 deletion src/utils/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ export const {
disableSidebarForRoute,
enableSidebarForRoute,
setNotificationPlatform,
setUserProfilePhoto
setUserProfilePhoto,
disableNavigationForRoute,
enableNavigationForRoute,
} = bindActionCreators(
{
setAppMenu: menuActions.setAppMenu,
disableSidebarForRoute: menuActions.disableSidebarForRoute,
enableSidebarForRoute: menuActions.enableSidebarForRoute,
setNotificationPlatform: notificationActions.setNotificationPlatform,
setUserProfilePhoto: authActions.setProfilePhoto,
disableNavigationForRoute: menuActions.disableNavigationForRoute,
enableNavigationForRoute: menuActions.enableNavigationForRoute,
},
store.dispatch
);
Expand Down
22 changes: 21 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from 'lodash';
import _ from "lodash";
import moment from "moment";
import config from "../../config";

Expand Down Expand Up @@ -47,3 +47,23 @@ export const login = () => {
export const businessLogin = () => {
window.location = getBusinessLoginUrl();
};

/**
* Checks If current user's profile creation time
*
* @param profile {Object} user profile
*
* @returns {boolean}
*/
export const checkProfileCreationDate = (profile) => {
const thresholdDate = moment(
config.PROFILE_CREATION_DATE_THRESHOLD,
"YYYY-MM-DD"
);

if (profile?.createdAt) {
return thresholdDate.isBefore(moment(profile?.createdAt));
}

return false;
};