Skip to content

Commit

Permalink
feat(navigation): create template for navigation bar
Browse files Browse the repository at this point in the history
  • Loading branch information
ngyngcphu committed Sep 20, 2023
1 parent dca3586 commit 63017c7
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import { useEffect } from 'react';
import { NavigateFunction, useNavigate } from 'react-router-dom';
import { HomeIcon } from '@heroicons/react/24/outline';
import { AppSkeleton } from '@components';
import { AppLayout, AuthLayout } from '@layouts';
import { AuthPage, HomePage } from '@pages';
import { useUserStore } from '@states';
import { AppSkeleton } from '@components';

export default function App() {
const navigate: NavigateFunction = useNavigate();
const { userStatus, getUserData } = useUserStore();

useEffect(() => {
getUserData();
}, [getUserData]);

useEffect(() => {
if (userStatus === 'SUCCESS') {
navigate('/home');
}
}, [userStatus, navigate]);

if (userStatus === 'UNINIT' || userStatus === 'PENDING') {
return <AppSkeleton />;
}
Expand All @@ -27,10 +35,15 @@ export default function App() {
return (
<AppLayout
menu={[
{
type: 'skeleton',
path: '/',
element: <AppSkeleton />
},
{
type: 'item',
icon: <HomeIcon className='h-5 w-5' />,
path: '/',
path: '/home',
name: 'Trang chủ',
element: <HomePage />
}
Expand Down
16 changes: 16 additions & 0 deletions src/components/AppDrawer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React, { ReactNode } from 'react';
import { Drawer } from '@material-tailwind/react';

export const AppDrawer: Component<{
open: boolean;
children: ReactNode;
onClose: () => void;
}> = ({ open, children, onClose }) => {
return (
<React.Fragment>
<Drawer open={open} className='p-0' onClose={onClose} size={256}>
{children}
</Drawer>
</React.Fragment>
);
};
17 changes: 17 additions & 0 deletions src/components/AppNavigation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useMemo, useState } from 'react';
import { AppDrawer } from './AppDrawer';
//import { ToggleSidebarBtn } from "./ToggleSidebarBtn";

// Tue's task in here. Feel free to be creative, not strict, you can do anything.
export const AppNavigation: Component<{ menu: RouteMenu }> = ({ menu }) => {
const [openSidebar, setOpenSidebar] = useState<boolean>(true);
console.log(menu); // Do somthing with menu???

const SideMenu = useMemo(() => <></>, []);

return (
<AppDrawer open={openSidebar} onClose={() => setOpenSidebar(false)}>
{SideMenu}
</AppDrawer>
);
};
2 changes: 2 additions & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
* @file Automatically generated by barrelsby.
*/

export * from './AppDrawer';
export * from './AppNavigation';
export * from './AppSkeleton';
export * from './ToggleSidebarBtn';
2 changes: 2 additions & 0 deletions src/layouts/AppLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMemo } from 'react';
import { Routes, Route } from 'react-router-dom';
import { AppNavigation } from '@components';

export const AppLayout: Component<{ menu: RouteMenu }> = ({ menu }) => {
const routeItems = useMemo(() => {
Expand All @@ -20,6 +21,7 @@ export const AppLayout: Component<{ menu: RouteMenu }> = ({ menu }) => {

return (
<div className='flex flex-col h-screen sm:min-h-screen'>
<AppNavigation menu={menu} />
<div className='lg:p-4 flex-1 h-full'>
<Routes>
{routeItems.map((item) => (
Expand Down
2 changes: 1 addition & 1 deletion src/pages/AuthPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const AuthPage = () => {
try {
await authService.login(data);
await getUserData();
navigate('/');
navigate('/home');
} catch (err) {
const errorMessage = (err as ResponseError).message;
toast.error(errorMessage);
Expand Down
2 changes: 2 additions & 0 deletions src/pages/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Tan's task in here

export function HomePage() {
return <div>Đăng nhập thành công</div>;
}
8 changes: 7 additions & 1 deletion src/types/routeSetting.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type LogoutBtn = BaseRoute & {
onClick: () => void;
};

type RouteMenuItem = RouteItem | RoutesList | 'divider' | LogoutBtn;
type RouteSkeleton = {
type: 'skeleton';
path: string;
element: React.ReactElement;
};

type RouteMenuItem = RouteItem | RoutesList | 'divider' | RouteSkeleton | LogoutBtn;

type RouteMenu = RouteMenuItem[];

0 comments on commit 63017c7

Please sign in to comment.