From 74aac3cfa839a6e7d30bb8d3a1bc7bbb40f346b9 Mon Sep 17 00:00:00 2001 From: Rodrigo Nunes Date: Thu, 14 Nov 2024 17:30:24 +0000 Subject: [PATCH] Feature: Button that allows the user to switch the side where the sidebar is displayed --- src/App.tsx | 57 ++++++++++++----------- src/components/layout/Header.tsx | 18 ++++++- src/components/layout/SidebarPosition.tsx | 31 ++++++++++++ src/pages/TimeTableSelector.tsx | 36 ++++++++++---- 4 files changed, 103 insertions(+), 39 deletions(-) create mode 100644 src/components/layout/SidebarPosition.tsx diff --git a/src/App.tsx b/src/App.tsx index 8c511a0b..23d4118c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -7,6 +7,7 @@ import { AboutPage, TimeTableSelectorPage, FaqsPage, NotFoundPage } from './page import { getPath, config, dev_config, plausible } from './utils' import Layout from './components/layout' import * as Sentry from "@sentry/react"; +import { SidebarProvider } from "./components/layout/SidebarPosition"; const configToUse = Number(import.meta.env.VITE_APP_PROD) ? config : dev_config @@ -60,33 +61,35 @@ const App = () => { const SentryRoutes = Sentry.withSentryReactRouterV6Routing(Routes); return ( - - - - {pages.map((page, pageIdx) => ( - -
- - -
- - } - /> - ))} - {redirects.map((redirect, redirectIdx) => ( - } - /> - ))} -
-
-
+ + + + + {pages.map((page, pageIdx) => ( + +
+ + +
+ + } + /> + ))} + {redirects.map((redirect, redirectIdx) => ( + } + /> + ))} +
+
+
+
) } diff --git a/src/components/layout/Header.tsx b/src/components/layout/Header.tsx index 98702132..678bb955 100644 --- a/src/components/layout/Header.tsx +++ b/src/components/layout/Header.tsx @@ -8,9 +8,11 @@ import { AtSymbolIcon, RectangleStackIcon, QuestionMarkCircleIcon, + ArrowsRightLeftIcon, } from '@heroicons/react/24/outline' import { LogoNIAEFEUPImage } from '../../images' import { getPath, config } from '../../utils' +import { useSidebarContext } from './SidebarPosition' const navigation = [ { @@ -34,6 +36,8 @@ type Props = { } const Header = ({ siteTitle, location }: Props) => { + const { toggleSidebarPosition } = useSidebarContext(); + return ( { ))} -
+
+ + +
- + + +
diff --git a/src/components/layout/SidebarPosition.tsx b/src/components/layout/SidebarPosition.tsx new file mode 100644 index 00000000..82365956 --- /dev/null +++ b/src/components/layout/SidebarPosition.tsx @@ -0,0 +1,31 @@ +import { createCheckboxScope } from "@radix-ui/react-checkbox"; +import { Children, createContext, useContext, useState } from "react"; + +type SidebarContextType = { + sidebarPosition: 'left' | 'right'; + toggleSidebarPosition: () => void; +}; + +const SidebarContext = createContext(undefined); + +export const SidebarProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { + const [sidebarPosition, setSidebarPosition] = useState<'left' | 'right'>('right'); + + const toggleSidebarPosition = () => { + setSidebarPosition((prev) => (prev === 'right' ? 'left' : 'right')); + }; + + return ( + + {children} + + ); +}; + +export const useSidebarContext = () => { + const context = useContext(SidebarContext); + if (!context) { + throw new Error('useSidebarContext must be used within a SidebarProvider') + } + return context +} \ No newline at end of file diff --git a/src/pages/TimeTableSelector.tsx b/src/pages/TimeTableSelector.tsx index 9eb6f343..4e140c27 100644 --- a/src/pages/TimeTableSelector.tsx +++ b/src/pages/TimeTableSelector.tsx @@ -5,6 +5,7 @@ import { Schedule, Sidebar } from '../components/planner' import { CourseInfo, Major } from '../@types' import MajorContext from '../contexts/MajorContext' import CourseContext from '../contexts/CourseContext' +import { useSidebarContext } from '../components/layout/SidebarPosition' const TimeTableSelectorPage = () => { const [majors, setMajors] = useState([]) @@ -12,6 +13,7 @@ const TimeTableSelectorPage = () => { const [pickedCourses, setPickedCourses] = useState(StorageAPI.getPickedCoursesStorage()); const [checkboxedCourses, setCheckboxedCourses] = useState(StorageAPI.getPickedCoursesStorage()); const [ucsModalOpen, setUcsModalOpen] = useState(false); + const { sidebarPosition } = useSidebarContext(); //TODO: Looks suspicious const [choosingNewCourse, setChoosingNewCourse] = useState(false); @@ -21,6 +23,8 @@ const TimeTableSelectorPage = () => { setMajors(majors) }) }, []) + + return ( { } }>
- {/* Schedule Preview */} -
-
+ {sidebarPosition === 'left' ?( + <> +
+ +
+
+ +
+ + ) : ( + <> +
-
-
- - +
+
+ +
+ + )}
- ) -} + ); +}; + -export default TimeTableSelectorPage; +export default TimeTableSelectorPage; \ No newline at end of file