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

Adds component markup for logo and dropdown menus using Strapi data for desktop #65

Merged
Merged
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
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = {
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-unused-vars": ["error", { "ignoreRestSiblings": true }],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allows clever removal of properties from an object with destructuring. I'm open to debate on it, although to me it feels restricted when the intent of this type of code is quite clear. See example below.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach works for me!

"@typescript-eslint/no-unused-expressions": "error",
"@typescript-eslint/require-await": "off",
"@typescript-eslint/restrict-template-expressions": "off",
Expand Down
13 changes: 13 additions & 0 deletions app/components/ui/Navigation.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,16 @@
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.15);
}
}

.menuContainer {
display: flex;
flex-direction: column;
}

.dropdown {
display: none;
}

.showDropdown {
display: block
}
74 changes: 55 additions & 19 deletions app/components/ui/Navigation.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,71 @@
import React from "react";
import React, { useState } from "react";
import { Link } from "react-router-dom";
import Translate from "./Translate";
import Translate from "components/ui/Translate";
import { useNavigationData } from "../../hooks/StrapiAPI";
import {
StrapiModel,
extractLogoFromNavigationResponse,
extractNavigationMenusFromNavigationResponse,
} from "../../models/Strapi";
import styles from "./Navigation.module.scss";

export const Navigation = ({
toggleHamburgerMenu,
}: {
toggleHamburgerMenu: () => void;
}) => {
const { data: navigationResponse, error, isLoading } = useNavigationData();
const [dropdown, setDropdown] = useState(false);
const logoData = extractLogoFromNavigationResponse(navigationResponse);
const menus =
extractNavigationMenusFromNavigationResponse(navigationResponse);

// TODO
if (error) {
return <span>ERROR</span>;
}

// TODO
if (isLoading) {
return <span>is loading...</span>;
}

return (
<nav className={styles.siteNav}>
<div className={styles.primaryRow}>
<div className={styles.navLeft}>
<SiteLogo />
<Link className={`${styles.navLogo}`} to="/">
<img src={logoData?.url} alt={logoData?.alternativeText} />
</Link>
</div>
<SiteLinks />

<ul className={styles.navRight}>
{menus?.map((menu) => (
<div className={styles.menuContainer} key={menu.id.toString()}>
<button
type="button"
aria-haspopup="menu"
aria-expanded={dropdown ? "true" : "false"}
onClick={() => setDropdown((prev) => !prev)}
>
{menu.title}
</button>

<ul
className={`${styles.dropdown} ${
dropdown ? styles.showDropdown : ""
}`}
>
{menu.link.map((linkItem: StrapiModel.Link) => (
<li key={linkItem.id} className="menu-item">
<Link to={linkItem.url}>{linkItem.text}</Link>
</li>
))}
</ul>
</div>
))}
<Translate />
</ul>
<div className={styles.mobileNavigation}>
<button
type="button"
Expand All @@ -28,19 +79,4 @@ export const Navigation = ({
);
};

const SiteLogo = () => (
<Link className={`${styles.navLogo}`} to="/">
{/* TODO */}
[SITE LOGO HERE]
</Link>
);

const SiteLinks = () => {
return (
<div className={styles.navRight}>
<Translate />
</div>
);
};

Comment on lines -31 to -45
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be worth separating these out later but I didn't see much use for this abstraction at the moment while I was rewriting things.

export default Navigation;
3 changes: 1 addition & 2 deletions app/hooks/StrapiAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ export namespace StrapiApi {
provider: string;
createdAt: string;
updatedAt: string;
formats: FormatsResponse
formats: FormatsResponse;
// TODO uknown types
// provider_metadata: null;
// formats: null;
}

export interface NavigationMenuResponse {
Expand Down
15 changes: 15 additions & 0 deletions app/models/Strapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,18 @@ export namespace StrapiModel {
export interface NavigationMenu extends StrapiApi.NavigationMenuResponse {
}
}

export function extractNavigationMenusFromNavigationResponse(
navigationResponse: StrapiModel.Header | null
): Array<Omit<StrapiModel.NavigationMenu, "__component">> | null {
return (
navigationResponse &&
navigationResponse.navigation.map(({ __component, ...rest }) => rest)
);
}

export function extractLogoFromNavigationResponse(
navigationResponse: StrapiModel.Header | null
): StrapiModel.Logo | null {
return navigationResponse && navigationResponse.logo.data.attributes;
}