Skip to content

Commit

Permalink
Merge pull request #22 from BU-Spark/activeElementFix
Browse files Browse the repository at this point in the history
Boston Voter App: Active element fix for internal linking buttons
  • Loading branch information
eelkus01 authored Jun 7, 2024
2 parents ec81a7f + 6002d54 commit 625ff6a
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 19 deletions.
14 changes: 0 additions & 14 deletions client/src/app/page.tsx

This file was deleted.

11 changes: 11 additions & 0 deletions client/src/components/button/ButtonFill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Button, ButtonProps } from '@mui/material';
import React from 'react';
import { useRouter } from 'next/navigation';
import { useActivePage } from '@/contexts/ActivePageContext';

// Button name, description, variant, and onClick event are customizable
// Note that description and variant are optional
Expand All @@ -13,12 +14,22 @@ type Props = {
className?: string;
};

const pageNameMap: Record<string, string> = {
'/votingOptions': 'Voting Options',
'/voterInfo': 'Your Voter Info',
'/ballotInfo': 'Ballot Info',
'/dropBoxLocations': 'Drop Box Locations',
'/upcomingElections': 'Upcoming Elections',
};

// By default, variant is contained
const ButtonFill = ({ name, link, variant = "contained", className }: Props) => {

const { setActivePage } = useActivePage();

const router = useRouter();
const handleClick = (page: string) => {
setActivePage(pageNameMap[page]);
router.push(page);
}

Expand Down
3 changes: 2 additions & 1 deletion client/src/components/nav/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AppBar, Box, Button, Tooltip, MenuItem, Toolbar, IconButton, Typography
import MenuIcon from '@mui/icons-material/Menu';
import StarIcon from '@mui/icons-material/Star';
import { useRouter } from 'next/navigation';
import { useActivePage } from '@/contexts/ActivePageContext';

const pages = ['Upcoming Elections', 'Your Voter Info', 'Voting Options', 'Ballot Info', 'Drop Box Locations'];
const links: Record<string, string> = {
Expand All @@ -21,7 +22,7 @@ const links: Record<string, string> = {

function NavBar() {
const [anchorElNav, setAnchorElNav] = React.useState<null | HTMLElement>(null);
const [activePage, setActivePage] = React.useState<string>('null');
const { activePage, setActivePage } = useActivePage();

const router = useRouter()

Expand Down
30 changes: 30 additions & 0 deletions client/src/contexts/ActivePageContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* A context for setting the active page for navbar styling. Needed to allow
* buttons to also change the active page, so made a context.
*/
'use client';

import React, { createContext, useState, useContext, ReactNode } from 'react';

interface ActivePageContextProps {
activePage: string;
setActivePage: (page: string) => void;
}

const ActivePageContext = createContext<ActivePageContextProps | undefined>(undefined);

export const ActivePageProvider = ({ children }: { children: ReactNode }) => {
const [activePage, setActivePage] = useState<string>('Upcoming Elections');
return (
<ActivePageContext.Provider value={{ activePage, setActivePage }}>
{children}
</ActivePageContext.Provider>
);
};

export const useActivePage = () => {
const context = useContext(ActivePageContext);
if (context === undefined) {
throw new Error('useActivePage must be used within an ActivePageProvider');
}
return context;
};
Empty file removed client/src/contexts/index.ts
Empty file.
11 changes: 7 additions & 4 deletions client/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import NavBar from '../components/nav/NavBar'; // Import the NavBar component
import { AppProps } from 'next/app'; // Import AppProps from Next.js
import { useEffect } from 'react';
import { useRouter } from 'next/router';
import { ActivePageProvider } from '@/contexts/ActivePageContext';

function MyApp({ Component, pageProps }: AppProps) {
const router = useRouter();
Expand All @@ -22,10 +23,12 @@ function MyApp({ Component, pageProps }: AppProps) {
}, [router.events]);

return (
<div className='mx-4'>
<NavBar />
<Component {...pageProps} />
</div>
<ActivePageProvider>
{/* <div className='mx-4'> */}
{/* <NavBar /> */}
<Component {...pageProps} />
{/* </div> */}
</ActivePageProvider>
);
}

Expand Down
2 changes: 2 additions & 0 deletions client/src/pages/ballotInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as React from 'react';
import DropDown from './dropDown';
import HelpIcon from '@mui/icons-material/Help';
import BoxAddress from "./boxAddress";
import NavBar from "@/components/nav/NavBar";



Expand All @@ -18,6 +19,7 @@ export default function BallotInfo() {
};
return (
<div className=''>
<NavBar />

<div className='p-4 text-center '>
<h1 className='text-blue-700 font-bold text-6xl '>Ballot Info</h1>
Expand Down
2 changes: 2 additions & 0 deletions client/src/pages/dropBoxLocations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import BoxField from '@/components/box/BoxField';
import SearchIcon from '@mui/icons-material/Search';
import { Box } from '@mui/material';
import Image from 'next/image'
import NavBar from '@/components/nav/NavBar';


export default function DropBoxLocations() {
return (
<div>
<NavBar />
<div className='flex flex-col justify-center items-center p-4 text-center mb-10'>
<h1 className='text-blue-700 font-bold text-6xl '>Drop Box Locations</h1>
<h1 className='font-semibold text-xl p-5'>Find the nearest ballot drop-off station and early voting locations in your area</h1>
Expand Down
16 changes: 16 additions & 0 deletions client/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// pages/index.tsx
'use client';

import React from 'react';
import NavBar from '../components/nav/NavBar';
import UpcomingElections from '@/pages/upcomingElections'; // Ensure this is the correct import path

const Home = () => {
return (
<div className="m-4">
<UpcomingElections />
</div>
);
};

export default Home;
2 changes: 2 additions & 0 deletions client/src/pages/upcomingElections/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import React from "react";
import ElectionDates from './electionDates'
import ButtonFill from "@/components/button/ButtonFill";
import ButtonFillEx from "@/components/button/ButtonFillEx";
import NavBar from "@/components/nav/NavBar";


export default function UpcomingElections() {

return (
<div className=''>
<NavBar />
<div className='flex flex-col justify-center items-center p-4 text-center'>
<h1 className='text-blue-700 font-bold text-7xl '>LET&#39;S VOTE!</h1>
<p className='font-semibold text-xl p-5'>The most important information about Boston&#39;s municipal elections to help you navigate your voting journey</p>
Expand Down
2 changes: 2 additions & 0 deletions client/src/pages/voterInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import Button from '@mui/material/Button';
import '@/app/globals.css'
import ButtonFill from '@/components/button/ButtonFill';
import { useRouter } from 'next/router';
import NavBar from '@/components/nav/NavBar';

export default function VoterInfo() {



return (
<div>
<NavBar />
<div className='flex flex-col justify-center items-center p-4 text-center my-4'>
<h1 className='text-blue-700 font-bold text-6xl '>Your Voter Info</h1>
<h1 className='font-semibold text-xl p-5 mt-2'>Here is everything you need to know about your <br />voter status and personal voting logistics!</h1>
Expand Down
2 changes: 2 additions & 0 deletions client/src/pages/votingOptions/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react'
import DropDownInfo from './DropDownInfo';
import NavBar from '@/components/nav/NavBar';

const VotingOptions = () => {
return (
<div className=''>
<NavBar />
<div className='flex flex-col justify-center items-center p-4 text-center mb-10'>
<h1 className='text-blue-700 font-bold text-6xl '>Voting Options</h1>
<h1 className='font-semibold text-xl p-5'>Everything you need to know about your voting options made simple!</h1>
Expand Down

0 comments on commit 625ff6a

Please sign in to comment.