Skip to content

Commit

Permalink
Update: UseContext to ftch navMap Data
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchayan721 committed Sep 23, 2023
1 parent aa31fb8 commit c080eea
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 205 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { NavMap } from "@datatypes/NavMap";
import { NextRequest, NextResponse } from "next/server";

// I want to use this path to store the navmap of the website and in a get request, return the navmap

export async function GET(req: NextRequest) {
const navmap = [
const navMap: NavMap[] = [
{
title: 'Business',
videoUrl: '/media/videos/navMenu/business.webm',
videoUrl: '/videos/navmenu/business.webm',
items: [
{
title: 'About Us',
Expand All @@ -28,7 +27,7 @@ export async function GET(req: NextRequest) {
},
{
title: 'Media Network',
videoUrl: '/media/videos/navMenu/media-network.webm',
videoUrl: '/videos/navmenu/media-network.webm',
items: [
{
title: 'Business and Brands',
Expand All @@ -50,7 +49,7 @@ export async function GET(req: NextRequest) {
},
{
title: 'Education & Teaching',
videoUrl: '/media/videos/navMenu/education-and-teaching.webm',
videoUrl: '/videos/navmenu/education-and-teaching.webm',
items: [
{
title: 'Resources',
Expand All @@ -68,7 +67,7 @@ export async function GET(req: NextRequest) {
},
{
title: 'Collaborators',
videoUrl: '/media/videos/navMenu/collaborators.webm',
videoUrl: '/videos/navmenu/collaborators.webm',
items: [
{
title: 'Explorers',
Expand All @@ -82,7 +81,7 @@ export async function GET(req: NextRequest) {
},
{
title: 'Partners',
videoUrl: '/media/videos/navMenu/partners.webm',
videoUrl: '/videos/navmenu/partners.webm',
items: [
{
title: 'Corporate & Foundations',
Expand All @@ -96,7 +95,7 @@ export async function GET(req: NextRequest) {
},
{
title: 'Commercial',
videoUrl: '/media/videos/navMenu/commercial.webm',
videoUrl: '/videos/navmenu/commercial.webm',
items: [
{
title: 'Advertise with Us',
Expand All @@ -113,6 +112,6 @@ export async function GET(req: NextRequest) {
],
},
];
return NextResponse.json(navmap);

return NextResponse.json(navMap);
};
11 changes: 6 additions & 5 deletions src/components/navigation/NavMenu/Megamenu/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React, { Fragment } from 'react';
import menuItems from './menuItems';
import { styled } from '@mui/material';
import NavGropus from './NavGroups';
import { MegamenuStateProvider } from '@helpers/MegamenuStateProvider';
import { NavMap } from '@datatypes/NavMap';
import { useNavMapContext } from '@helpers/NavMapProvider';

interface MegamenuSectionProps extends React.HTMLAttributes<HTMLSelectElement> {
children: React.ReactNode;
numberOfItems?: number;
children?: React.ReactNode;
};

const MegamenuSection = styled('section')<MegamenuSectionProps>(({ theme, numberOfItems }) => ({
const MegamenuSection = styled('section')<MegamenuSectionProps>(({ theme }) => ({
gridColumn: '1 / 25',
gridRow: '3 / 19',
zIndex: theme.Shadows.high.zIndex,
Expand Down Expand Up @@ -47,10 +47,11 @@ const MegamenuSection = styled('section')<MegamenuSectionProps>(({ theme, number
}));

const Megamenu = () => {
const { data } = useNavMapContext();
return (
<MegamenuSection>
<MegamenuStateProvider>
{menuItems.map((item, index) => (
{data?.map((item, index) => (
<Fragment key={index}>
<NavGropus
index={index}
Expand Down
112 changes: 0 additions & 112 deletions src/components/navigation/NavMenu/Megamenu/menuItems.tsx

This file was deleted.

18 changes: 9 additions & 9 deletions src/containers/Chatak/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ const Chatak: React.FC<ChatakProps> = ({
countDown = 1000,
transitionTime = 1000,
}) => {
const [data, setData] = React.useState<Darshan[]>();

const [data, setData] = React.useState<Darshan[]>({} as Darshan[]);

const fetchData = React.useCallback(async () => {
const response = await fetch('/api/website/bharat-darshan');
const data = await response.json();
setData(data);
}, []);

React.useEffect(() => {
const fetchData = async () => {
const response = await fetch('/api/website/bharat-darshan');
const data = await response.json();
setData(data);
};
fetchData();
}, []);
}, [fetchData]);

console.log(data);
return (
<div>Chatak</div>
)
Expand Down
2 changes: 1 addition & 1 deletion src/containers/Footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const BlankTop = styled(Box)(({ theme }) => ({
height: theme.Heights.header.default,
width: '50%',
marginTop: theme.Spaces.xs,
borderBottom: '1.5px solid ' + theme.palette.secondary.dark,
borderBottom: '3px solid ' + theme.palette.secondary.dark,
}));

const FooterBox = styled(Box)(({ theme }) => ({
Expand Down
10 changes: 10 additions & 0 deletions src/datatypes/NavMap.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type NavItem = {
title: string;
link: string;
};

export type NavMap = {
title: string;
videoUrl: string;
items: NavItem[];
};
37 changes: 37 additions & 0 deletions src/helpers/NavMapProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { NavMap } from '@datatypes/NavMap';
import React from 'react';

interface NavMapContextValue {
data?: NavMap[];
fetchData: () => Promise<void>;
}

export const NavMapContext = React.createContext<NavMapContextValue>({
fetchData: async () => {},
});

interface NavMapProviderProps {
children: React.ReactNode;
}

export const NavMapProvider: React.FC<NavMapProviderProps> = ({ children }) => {
const [data, setData] = React.useState<NavMap[]>();

const fetchData = React.useCallback(async () => {
const response = await fetch('/api/website/navigation-map');
const data = await response.json();
setData(data);
}, []);

React.useEffect(() => {
fetchData();
}, [fetchData]);

return (
<NavMapContext.Provider value={{ data, fetchData }}>
{children}
</NavMapContext.Provider>
);
};

export const useNavMapContext = () => React.useContext(NavMapContext);
19 changes: 11 additions & 8 deletions src/layouts/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Navbar from '@components/navigation/NavBar';
import NavMenu from '@components/navigation/NavMenu';
import { styled } from '@mui/material';
import Footer from '@containers/Footer';
import { NavMapProvider } from '@helpers/NavMapProvider';

const StyledMain = styled('main')<MainLayoutProps>(({ theme }) => ({
width: '100%',
Expand All @@ -13,14 +14,16 @@ const StyledMain = styled('main')<MainLayoutProps>(({ theme }) => ({

const MainLayout: React.FC<MainLayoutProps> = ({ children, ...props }) => {
return (
<NavigationMenuStateProvider>
<Navbar />
<NavMenu />
<StyledMain {...props}>
{children}
</StyledMain>
<Footer />
</NavigationMenuStateProvider>
<NavMapProvider>
<NavigationMenuStateProvider>
<Navbar />
<NavMenu />
<StyledMain {...props}>
{children}
</StyledMain>
<Footer />
</NavigationMenuStateProvider>
</NavMapProvider>
)
};

Expand Down
59 changes: 0 additions & 59 deletions src/lib/NavMap.ts

This file was deleted.

0 comments on commit c080eea

Please sign in to comment.