Commit daaa08d2 authored by Ken's avatar Ken

feat: setup route

parent f45216bb
This diff is collapsed.
import { CircularProgress } from "@mui/material"; import { CircularProgress } from "@mui/material";
import { useAppSelector } from "app/hooks";
import { globalSelector } from "app/selectors";
import React from "react"; import React from "react";
type Props = { const Loading = () => {
isOpen: boolean; const { isLoading } = useAppSelector(globalSelector);
};
const Loading = ({ isOpen }: Props) => {
return ( return (
<div className={`loading${isOpen ? " loading-open" : ""}`}> <div className={`loading${isLoading ? " loading-open" : ""}`}>
<CircularProgress /> <CircularProgress />
</div> </div>
); );
......
import Header from "components/Header";
import { PageUrl } from "configuration/enum";
import React, { useEffect } from "react";
import { Outlet, useLocation, useNavigate } from "react-router-dom";
const MainLayout = () => {
const location = useLocation();
const navigate = useNavigate();
useEffect(() => {
if (location.pathname === PageUrl.ROOT) {
navigate(PageUrl.HOMEPAGE);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [location]);
return (
<div className="main-layout">
<Header />
<Outlet />
</div>
);
};
export default MainLayout;
export const enum PageUrl {
ROOT = "/",
HOMEPAGE = "/home",
NEWS_DETAIL = "/newspaper/:id",
ALL = "*",
}
import React from "react"; import React from "react";
import ReactDOM from "react-dom/client"; import ReactDOM from "react-dom/client";
import { Provider } from "react-redux/es/exports"; import { Provider } from "react-redux/es/exports";
import Routers from "routers";
import store from "./app/store"; import store from "./app/store";
import Header from "./components/Header";
import MainPage from "./pages/home/MainPage";
import "./_styles.scss"; import "./_styles.scss";
const root = ReactDOM.createRoot( const root = ReactDOM.createRoot(
...@@ -11,10 +10,7 @@ const root = ReactDOM.createRoot( ...@@ -11,10 +10,7 @@ const root = ReactDOM.createRoot(
); );
root.render( root.render(
<Provider store={store}> <Provider store={store}>
<> <Routers />
<Header />
<MainPage />
</>
</Provider> </Provider>
); );
......
import React from "react";
const NewsDetail = () => {
return <div>NewsDetail</div>;
};
export default NewsDetail;
@use "styles/index" as *; @use "styles/index" as *;
.mainPage { .homePage {
padding: 40px 0; padding: 40px 0;
&-title { &-title {
......
import { handleLoading } from "app/globalSlice"; import { handleLoading } from "app/globalSlice";
import { useAppDispatch, useAppSelector } from "app/hooks"; import { useAppDispatch, useAppSelector } from "app/hooks";
import { globalSelector, homeSelector } from "app/selectors"; import { homeSelector } from "app/selectors";
import Loading from "components/Loading";
import WrapperContainer from "components/WrapperContainer"; import WrapperContainer from "components/WrapperContainer";
import React, { useEffect } from "react"; import React, { useEffect } from "react";
import Newspaper from "../../components/Newspaper"; import Newspaper from "../../components/Newspaper";
import { getNews } from "./homePageSlice"; import { getNews } from "./homePageSlice";
const MainPage = () => { const HomePage = () => {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const { isLoading } = useAppSelector(globalSelector);
const { newsData } = useAppSelector(homeSelector); const { newsData } = useAppSelector(homeSelector);
useEffect(() => { useEffect(() => {
...@@ -29,13 +27,12 @@ const MainPage = () => { ...@@ -29,13 +27,12 @@ const MainPage = () => {
}, []); }, []);
return ( return (
<main className="mainPage"> <main className="homePage">
<Loading isOpen={isLoading} />
<WrapperContainer> <WrapperContainer>
<div className="mainPage-title"> <div className="homePage-title">
<h1>Tin mới nhất</h1> <h1>Tin mới nhất</h1>
</div> </div>
<div className="mainPage-list row"> <div className="homePage-list row">
{newsData.map((newspaper, idx) => ( {newsData.map((newspaper, idx) => (
<div className={`col-${idx === 0 ? 12 : 4}`} key={newspaper.id}> <div className={`col-${idx === 0 ? 12 : 4}`} key={newspaper.id}>
<Newspaper data={newspaper} firstNews={idx === 0} /> <Newspaper data={newspaper} firstNews={idx === 0} />
...@@ -47,4 +44,4 @@ const MainPage = () => { ...@@ -47,4 +44,4 @@ const MainPage = () => {
); );
}; };
export default MainPage; export default HomePage;
import Loading from "components/Loading";
import MainLayout from "components/MainLayout";
import { PageUrl } from "configuration/enum";
import HomePage from "pages/home";
import NewsDetail from "pages/NewsDetail";
import React from "react";
import {
BrowserRouter as Router,
Routes,
Route,
Navigate,
} from "react-router-dom";
const Routers = () => {
return (
<Router>
<Loading />
<Routes>
<Route path={PageUrl.ROOT} element={<MainLayout />}>
<Route path={PageUrl.HOMEPAGE} element={<HomePage />} />
<Route path={PageUrl.NEWS_DETAIL} element={<NewsDetail />} />
<Route
path={PageUrl.ALL}
element={<Navigate to={PageUrl.HOMEPAGE} replace />}
/>
</Route>
</Routes>
</Router>
);
};
export default Routers;
@use "pages/home/mainPage.scss"; @use "pages/home/HomePage.scss";
@use "pages/NewsDetail/NewsDetail.scss";
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment