Skip to content

Commit

Permalink
dynamic menu depending on category
Browse files Browse the repository at this point in the history
  • Loading branch information
ragudesign committed Jun 16, 2022
1 parent 6fe6cdb commit 0546f2d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 43 deletions.
24 changes: 3 additions & 21 deletions components/header/Header.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
import Link from 'next/link';
import headerStyles from './Header.module.scss';
import fetcher from '../../lib/fetcher';
import { getCats } from '../../lib/api';
import { useEffect, useState } from 'react';

const Header = () => {

// const [menuItems, setMenuItems] = useState([]);

// useEffect(async () => {
// const response = await fetcher(getCats);

// const kbCats = response.data.kbsTax.nodes;

// setMenuItems(kbCats);
// }, []);

// console.log(menuItems, "menuItems")
const Header = ({menu}) => {

return (
<div className={headerStyles.header}>
Expand All @@ -29,12 +14,9 @@ const Header = () => {

<nav className={headerStyles.nav}>
<ul>
{/* {menuItems.map((item) => {
{menu.map((item) => {
return <li key={item.id}><Link href={'/categories/'+item.slug}>{item.name}</Link></li>;
})} */}
{/* <li>
<Link href="/">Back to home</Link>
</li> */}
})}
</ul>
</nav>
</div>
Expand Down
27 changes: 11 additions & 16 deletions pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import Header from '../components/header/Header'
import Header from '../components/header/Header';
import NextNprogress from 'nextjs-progressbar';
import fetcher from '../lib/fetcher';
import '../styles/base/base.scss'
import '../styles/base/gutenberg.scss'
import { getCats } from '../lib/api';
import '../styles/base/base.scss';
import '../styles/base/gutenberg.scss';

function MyApp({ Component, pageProps, kbCats }) {

return (
<>
<NextNprogress color="#0770ef" />
<Header kbCats={kbCats} />
<Header menu={kbCats} />
<Component {...pageProps} />
</>
)
);
}

export default MyApp


export async function getStaticProps() {
MyApp.getInitialProps = async () => {
const response = await fetcher(getCats);

const kbCats = response.data.kbsTax.nodes;
const kbCats = response.data?.kbsTax.nodes;

return {
props: {
kbCats,
},
return { kbCats };
};
}

export default MyApp;
4 changes: 2 additions & 2 deletions pages/categories/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default function categories({ singleCategory }) {
export async function getStaticPaths() {
const response = await fetcher(getCats);

const catSlugs = response.data.kbsTax.nodes;
const catSlugs = response.data?.kbsTax.nodes;

return {
paths: catSlugs.map((cat) => `/categories/${cat.slug}`),
Expand All @@ -49,7 +49,7 @@ export const getStaticProps = async ({ params }) => {

return {
props: {
singleCategory: response.data.kbsTax.nodes[0],
singleCategory: response.data?.kbsTax.nodes[0],
},
};
};
2 changes: 1 addition & 1 deletion pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function Home({ latestKbs }) {
export async function getStaticProps() {
const response = await fetcher(getLatestKbs);

const latestKbs = response.data.kbsTax.nodes;
const latestKbs = response.data?.kbsTax.nodes;

return {
props: {
Expand Down
4 changes: 2 additions & 2 deletions pages/kb/[slug].js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function Kb({ singlePage }) {
export async function getStaticPaths() {
const response = await fetcher(getKbSlugs);

const titleSlugs = response.data.kbs.nodes;
const titleSlugs = response.data?.kbs.nodes;

return {
paths: titleSlugs.map((kb) => `/kb/${kb.slug}`),
Expand All @@ -46,7 +46,7 @@ export const getStaticProps = async ({ params }) => {

return {
props: {
singlePage: response.data.kb,
singlePage: response.data?.kb,
},
};
};
2 changes: 1 addition & 1 deletion scripts/build-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function buildJSON() {

const response = await fetcher(query);

const titleSlugs = response.data.kbs.nodes
const titleSlugs = response.data?.kbs.nodes

const fullParams = `${JSON.stringify(titleSlugs)}`

Expand Down

0 comments on commit 0546f2d

Please sign in to comment.