Refactor top navigation

pull/3813/head
Kamran Ahmed 2 years ago
parent bf9e767083
commit 6a0403ec25
  1. 5
      src/components/Analytics/Analytics.astro
  2. 4
      src/components/Authenticator/Authenticator.astro
  3. 39
      src/components/Authenticator/authenticator.ts
  4. 32
      src/components/Login/AccountNavigation.astro
  5. 18
      src/components/Navigation/AccountDropdown.astro
  6. 75
      src/components/Navigation/Navigation.astro
  7. 28
      src/components/Navigation/navigation.ts
  8. 4
      src/layouts/BaseLayout.astro
  9. 3
      src/layouts/SettingLayout.astro

@ -1,17 +1,18 @@
---
---
<script src='./analytics.js'></script>
<script src='./analytics.ts'></script>
<script async src='https://www.googletagmanager.com/gtag/js?id=UA-139582634-1'
></script>
<script is:inline>
// @ts-nocheck
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('js', new Date());
gtag('config', 'UA-139582634-1');
document.addEventListener('click', (e) => {

@ -0,0 +1,4 @@
---
---
<script src='./authenticator.ts'></script>

@ -0,0 +1,39 @@
import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from '../../lib/constants';
/**
* Prepares the UI for the user who is logged in
*/
function handleLoggedOut() {
document.querySelectorAll('[data-auth-required]').forEach((el) => {
el.classList.add('hidden');
});
document.querySelectorAll('[data-guest-required]').forEach((el) => {
el.classList.remove('hidden');
});
}
/**
* Prepares the UI for the user who is logged out
*/
function handleLoggedIn() {
document.querySelectorAll('[data-auth-required]').forEach((el) => {
el.classList.remove('hidden');
});
document.querySelectorAll('[data-guest-required]').forEach((el) => {
el.classList.add('hidden');
});
}
function handleAuthRequired() {
const token = Cookies.get(TOKEN_COOKIE_NAME);
if (token) {
handleLoggedIn();
} else {
handleLoggedOut();
}
}
handleAuthRequired();

@ -1,32 +0,0 @@
---
import AccountDropdown from './AccountDropdown.astro';
---
<div>
<AccountDropdown />
<a
id='register-button'
class='flex h-8 w-28 cursor-pointer items-center justify-center rounded-full bg-gradient-to-r from-blue-500 to-blue-700 py-2 px-4 text-sm font-medium text-white hover:from-blue-500 hover:to-blue-600'
href='/signup'
>
<span>Register</span>
</a>
</div>
<script>
import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from '../../lib/constants';
const registerButton = document.getElementById('register-button');
const accountDropdown = document.getElementById('account-dropdown');
const token = Cookies.get(TOKEN_COOKIE_NAME);
if (token) {
accountDropdown?.classList.remove('hidden');
registerButton?.classList.add('hidden');
} else {
registerButton?.classList.remove('hidden');
accountDropdown?.classList.add('hidden');
}
</script>

@ -2,15 +2,18 @@
import Icon from '../Icon.astro';
---
<div class='relative block hidden' id='account-dropdown'>
<div class='relative hidden' data-auth-required>
<button
class='flex h-8 w-28 items-center justify-center rounded-full bg-gradient-to-r from-purple-500 to-purple-700 py-2 px-4 text-sm font-medium text-white hover:from-purple-500 hover:to-purple-600'
type='button'
data-account-button
>
<span class='flex items-center gap-1'>
<span class='inline-flex items-center gap-1.5'>
Account
<Icon icon='chevron-down' class='h-3 w-3 stroke-[3px]' />
<Icon
icon='chevron-down'
class='relative top-[0.5px] h-3 w-3 stroke-[3px]'
/>
</span>
</button>
@ -40,19 +43,10 @@ import Icon from '../Icon.astro';
</div>
<script>
import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from '../../lib/constants';
const accountButton = document.querySelector('[data-account-button]');
const accountMenu = accountButton?.nextElementSibling;
const logoutButton = accountMenu?.querySelector('[data-logout-button]');
accountButton?.addEventListener('click', () => {
accountMenu?.classList.toggle('hidden');
});
logoutButton?.addEventListener('click', () => {
Cookies.remove(TOKEN_COOKIE_NAME);
window.location.reload();
});
</script>

@ -1,6 +1,6 @@
---
import Icon from './Icon.astro';
import AccountNavigation from './Login/AccountNavigation.astro';
import Icon from '../Icon.astro';
import AccountDropdown from './AccountDropdown.astro';
---
<div class='bg-slate-900 py-5 text-white sm:py-8'>
@ -27,7 +27,15 @@ import AccountNavigation from './Login/AccountNavigation.astro';
<a href='/videos' class='text-gray-400 hover:text-white'>Videos</a>
</li>
<li>
<AccountNavigation />
<AccountDropdown />
<a
data-guest-required
class='flex h-8 w-28 cursor-pointer items-center justify-center rounded-full bg-gradient-to-r from-blue-500 to-blue-700 py-2 px-4 text-sm font-medium text-white hover:from-blue-500 hover:to-blue-600'
href='/signup'
>
<span>Register</span>
</a>
</li>
</ul>
@ -35,7 +43,7 @@ import AccountNavigation from './Login/AccountNavigation.astro';
<button
class='block cursor-pointer text-gray-400 hover:text-gray-50 sm:hidden'
aria-label='Menu'
show-mobile-nav
data-show-mobile-nav
>
<Icon icon='hamburger' />
</button>
@ -43,10 +51,10 @@ import AccountNavigation from './Login/AccountNavigation.astro';
<!-- Mobile Navigation Items -->
<div
class='fixed top-0 bottom-0 left-0 right-0 z-40 flex hidden items-center bg-slate-900'
mobile-nav
data-mobile-nav
>
<button
close-mobile-nav
data-close-mobile-nav
class='absolute top-6 right-6 block cursor-pointer text-gray-400 hover:text-gray-50'
aria-label='Close Menu'
>
@ -76,12 +84,14 @@ import AccountNavigation from './Login/AccountNavigation.astro';
Videos
</a>
</li>
<li id='profile-mobile-nav' class='hidden'>
<!-- Links for logged in users -->
<li data-auth-required class='hidden'>
<a href='/profile' class='text-xl hover:text-blue-300 md:text-lg'>
Profile
</a>
</li>
<li id='settings-mobile-nav' class='hidden'>
<li data-auth-required class='hidden'>
<a
href='/settings/profile'
class='text-xl hover:text-blue-300 md:text-lg'
@ -89,15 +99,18 @@ import AccountNavigation from './Login/AccountNavigation.astro';
Settings
</a>
</li>
<li id='logout-mobile-nav' class='hidden'>
<button class='text-xl text-red-300 hover:text-red-400 md:text-lg'>
<li data-auth-required class='hidden'>
<button
data-logout-button
class='text-xl text-red-300 hover:text-red-400 md:text-lg'
>
Logout
</button>
</li>
<li>
<a
data-guest-required
href='/signup'
id='register-mobile-nav'
class='text-xl text-red-300 hover:text-red-400 md:text-lg'
>
Register
@ -108,42 +121,4 @@ import AccountNavigation from './Login/AccountNavigation.astro';
</nav>
</div>
<script>
import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from '../lib/constants';
const profileLink = document.getElementById('profile-mobile-nav');
const logoutButton = document.getElementById('logout-mobile-nav');
const registerLink = document.getElementById('register-mobile-nav');
const settingsLink = document.getElementById('settings-mobile-nav');
const token = Cookies.get(TOKEN_COOKIE_NAME);
if (token) {
profileLink?.classList.remove('hidden');
logoutButton?.classList.remove('hidden');
registerLink?.classList.add('hidden');
settingsLink?.classList.remove('hidden');
} else {
profileLink?.classList.add('hidden');
logoutButton?.classList.add('hidden');
settingsLink?.classList.add('hidden');
registerLink?.classList.remove('hidden');
}
// Remove the token cookie and redirect to the homepage
logoutButton?.addEventListener('click', () => {
Cookies.remove(TOKEN_COOKIE_NAME);
window.location.href = '/';
});
document.querySelector('[show-mobile-nav]')?.addEventListener('click', () => {
document.querySelector('[mobile-nav]')?.classList.remove('hidden');
});
document
.querySelector('[close-mobile-nav]')
?.addEventListener('click', () => {
document.querySelector('[mobile-nav]')?.classList.add('hidden');
});
</script>
<script src='./navigation.ts'></script>

@ -0,0 +1,28 @@
import Cookies from 'js-cookie';
import { TOKEN_COOKIE_NAME } from '../../lib/constants';
function logout() {
Cookies.remove(TOKEN_COOKIE_NAME);
window.location.href = '/';
}
function bindEvents() {
document.addEventListener('click', (e) => {
const target = e.target as HTMLElement;
const dataset = {
...target.dataset,
...target.closest('button')?.dataset,
};
// If the user clicks on the logout button, remove the token cookie
if (dataset.logoutButton !== undefined) {
logout();
} else if (dataset.showMobileNav !== undefined) {
document.querySelector('[data-mobile-nav]')?.classList.remove('hidden');
} else if (dataset.closeMobileNav !== undefined) {
document.querySelector('[data-mobile-nav]')?.classList.add('hidden');
}
});
}
bindEvents();

@ -1,6 +1,6 @@
---
import '../styles/global.css';
import Navigation from '../components/Navigation.astro';
import Navigation from '../components/Navigation/Navigation.astro';
import OpenSourceBanner from '../components/OpenSourceBanner.astro';
import Footer from '../components/Footer.astro';
import type { SponsorType } from '../components/Sponsor/Sponsor.astro';
@ -8,6 +8,7 @@ import Sponsor from '../components/Sponsor/Sponsor.astro';
import YouTubeBanner from '../components/YouTubeBanner.astro';
import { siteConfig } from '../lib/config';
import Analytics from '../components/Analytics/Analytics.astro';
import Authenticator from '../components/Authenticator/Authenticator.astro';
export interface Props {
title: string;
@ -108,6 +109,7 @@ const commitUrl = `https://github.com/kamranahmedse/developer-roadmap/commit/${i
{sponsor && <Sponsor sponsor={sponsor} />}
<slot name='after-footer' />
<Analytics />
<Authenticator />
</slot>
</body>
</html>

@ -1,10 +1,9 @@
---
import '../styles/global.css';
import Navigation from '../components/Navigation.astro';
import Navigation from '../components/Navigation/Navigation.astro';
import type { SponsorType } from '../components/Sponsor/Sponsor.astro';
import YouTubeBanner from '../components/YouTubeBanner.astro';
import { siteConfig } from '../lib/config';
import SettingSidebar from '../components/Setting/SettingSidebar.astro';
export interface Props {
title: string;

Loading…
Cancel
Save