Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge pull request #146 from tupuio/140-protect-pages #150

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
15 changes: 1 addition & 14 deletions components/Sidebar/MobileNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@ import { Box, Flex, HStack, Text, VStack } from "@chakra-ui/layout";
import {
Menu,
MenuButton,
MenuDivider,
MenuItem,
MenuList,
} from "@chakra-ui/menu";
import { FiChevronDown, FiMenu } from "react-icons/fi";
import { signOut } from "next-auth/react";
import { useRouter } from 'next/router'

const MobileNav = ({ session, onOpen, mode, setMode, ...rest }) => {
const MobileNav = ({ session, onOpen, ...rest }) => {
const name = session?.user?.name;
const router = useRouter();

const toggleMode = () => {
setMode(mode === "mentor" ? "mentee" : "mentor");
router.push('/');
};

return (
<Flex
ml={{ base: 0, md: 60 }}
Expand Down Expand Up @@ -69,9 +63,6 @@ const MobileNav = ({ session, onOpen, mode, setMode, ...rest }) => {
ml="2"
>
<Text fontSize="sm">{name}</Text>
<Text fontSize="xs" color="gray.600">
{mode === "mentor" ? "Mentor" : "Mentee"}
</Text>
</VStack>
<Box display={{ base: "none", md: "flex" }}>
<FiChevronDown />
Expand All @@ -82,10 +73,6 @@ const MobileNav = ({ session, onOpen, mode, setMode, ...rest }) => {
bg={useColorModeValue("white", "gray.900")}
borderColor={useColorModeValue("gray.200", "gray.700")}
>
<MenuItem onClick={toggleMode}>
Switch to {mode === "mentor" ? "Mentee" : "Mentor"} view
</MenuItem>
<MenuDivider />
<MenuItem onClick={() => signOut()}>Sign out</MenuItem>
</MenuList>
</Menu>
Expand Down
91 changes: 86 additions & 5 deletions components/Sidebar/SidebarContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
} from "react-icons/fi";
import useSWR from "swr";
import NavItem from "./NavItem";
import { useSession } from "next-auth/react";
import { doesUserHaveRole } from "../../utils/session";
import { MENTOR_ROLE, MENTEE_ROLE } from "../../constants";

const fetcher = (...args) => fetch(...args).then((res) => res.json());

Expand All @@ -27,9 +30,13 @@ const SidebarContent = ({ onClose, mode, ...rest }) => {
const requestsCount = requestsData?.count || 0;
const applicationsCount = applicationsData?.count || 0;
const menteesCount = menteesData?.count || 0;
const MentorLinkItems = [
const { data: session } = useSession()

const GeneralLinkItems = [
{ name: "Your profile", icon: FiUser, href: "/profile" },
{ name: "Preferences", icon: FiSliders, href: "/preferences" },
]

const MentorLinkItems = [
{
name: "Requests",
icon: FiInbox,
Expand All @@ -40,8 +47,8 @@ const SidebarContent = ({ onClose, mode, ...rest }) => {
];

const MenteeLinkItems = [
{ name: "Your profile", icon: FiUser, href: "/profile" },
{ name: "Find a mentor", icon: FiSearch, href: "/mentors" },
{ name: "Preferences", icon: FiSliders, href: "/preferences" },
{ name: "Applications", icon: FiInbox, href: "/applications", tag: () => applicationsCount, },
{ name: "Mentorships", icon: FiUsers, href: "/mentorships", tag: () => mentorshipsCount, },
];
Expand All @@ -56,6 +63,12 @@ const SidebarContent = ({ onClose, mode, ...rest }) => {
links = mode === "mentor" ? MentorLinkItems : MenteeLinkItems;
}


// TODO: handle admins
const shouldRenderMentorLinks = profileData?.published && doesUserHaveRole(session, MENTOR_ROLE)

const shouldRenderMenteeLinks = profileData?.published && doesUserHaveRole(session, MENTEE_ROLE)

return (
<Box
transition="3s ease"
Expand All @@ -77,7 +90,75 @@ const SidebarContent = ({ onClose, mode, ...rest }) => {
onClick={onClose}
/>
</Flex>
<Flex h="14" alignItems="center" mx="8" justifyContent="space-between">
<Box mt={50}>
{GeneralLinkItems.map((link) => (
<NavItem
key={link.name}
icon={link.icon}
href={link.href}
bg={mode === "mentor" ? "brand.blue" : "brand.green"}
>
{link.name}
{link.tag && link.tag() > 0 && (
<Tag size="sm" colorScheme="teal" ml={2}>
{link.tag()}
</Tag>
)}
</NavItem>
))}
</Box>
{shouldRenderMentorLinks &&
<>
<Flex h="14" alignItems="center" mx="8" justifyContent="space-between">
<Text textColor="white" fontSize="xl" fontWeight="normal">
Mentor
</Text>
</Flex>
<Box>
{MentorLinkItems.map((link) => (
<NavItem
key={link.name}
icon={link.icon}
href={link.href}
bg={mode === "mentor" ? "brand.blue" : "brand.green"}
>
{link.name}
{link.tag && link.tag() > 0 && (
<Tag size="sm" colorScheme="teal" ml={2}>
{link.tag()}
</Tag>
)}
</NavItem>
))}
</Box>
</>
}
{shouldRenderMenteeLinks &&
<>
<Flex h="14" alignItems="center" mx="8" justifyContent="space-between">
<Text textColor="white" fontSize="xl" fontWeight="normal">
Mentee
</Text>
</Flex>
<Box>
{MenteeLinkItems.map((link) => (
<NavItem
key={link.name}
icon={link.icon}
href={link.href}
bg={mode === "mentor" ? "brand.blue" : "brand.green"}
>
{link.name}
{link.tag && link.tag() > 0 && (
<Tag size="sm" colorScheme="teal" ml={2}>
{link.tag()}
</Tag>
)}
</NavItem>
))}
</Box>
</>}
{/* <Flex h="14" alignItems="center" mx="8" justifyContent="space-between">
<Text textColor="white" fontSize="2xl" fontWeight="normal">
{mode}
</Text>
Expand All @@ -104,7 +185,7 @@ const SidebarContent = ({ onClose, mode, ...rest }) => {
tupu.io <ExternalLinkIcon mx="2px" />
</Link>
</NavItem>
</Box>
</Box> */}
</Box>
);
};
Expand Down
8 changes: 2 additions & 6 deletions components/Sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SigninPass from "../auth/SigninPass";
import MobileNav from "./MobileNav";
import SidebarContent from "./SidebarContent";

export default function SidebarWithHeader({ mode, setMode, children }) {
export default function SidebarWithHeader({ children }) {
const { data: session } = useSession();
const { isOpen, onOpen, onClose } = useDisclosure();
const router = useRouter();
Expand All @@ -22,13 +22,11 @@ export default function SidebarWithHeader({ mode, setMode, children }) {
return <SigninPass />;
}

console.log(session);

return (
<Box minH="100vh" bg="gray.100">
<SidebarContent
onClose={() => onClose}
mode={mode}
display={{ base: "none", md: "block" }}
/>
<Drawer
Expand All @@ -41,14 +39,12 @@ export default function SidebarWithHeader({ mode, setMode, children }) {
size="full"
>
<DrawerContent>
<SidebarContent mode={mode} onClose={onClose} />
<SidebarContent onClose={onClose} />
</DrawerContent>
</Drawer>
<MobileNav
session={session}
onOpen={onOpen}
mode={mode}
setMode={setMode}
/>
<Box ml={{ base: 0, md: 60 }} p="4">
{children}
Expand Down
8 changes: 3 additions & 5 deletions pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ChakraProvider } from "@chakra-ui/react";
import { extendTheme } from "@chakra-ui/react";
import { useState } from "react";
import SidebarWithHeader from "../components/Sidebar";
import { SessionProvider } from "next-auth/react";
import AuthChecker from "../components/AuthChecker/AuthChecker";
Expand Down Expand Up @@ -43,18 +42,17 @@ function TupuApp({
Component,
pageProps: { session, providers, ...pageProps },
}) {
const [mode, setMode] = useState("mentee");

return (
<SessionProvider session={session}>
<ChakraProvider theme={theme}>
<SidebarWithHeader mode={mode} setMode={setMode}>
<SidebarWithHeader>
{Component.auth ? (
<AuthChecker authObject={Component.auth}>
<Component mode={mode} {...pageProps} />
<Component {...pageProps} />
</AuthChecker>
) : (
<Component mode={mode} {...pageProps} />
<Component {...pageProps} />
)}
</SidebarWithHeader>
</ChakraProvider>
Expand Down
6 changes: 6 additions & 0 deletions utils/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MENTOR_ROLE } from "../constants"

export const doesUserHaveRole = (session, role) => {
return session?.user?.roles?.includes(role)
}

You are viewing a condensed version of this merge commit. You can view the full changes here.