Skip to content

Commit

Permalink
:sparkles routes 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyMan0 committed Dec 19, 2024
1 parent c339fe8 commit d68a7af
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,48 @@ import Error404Page from "./pages/404/404";
import HomePage from "./pages/home/home-page";
import LoginPage from "./pages/login/login-page";
import { ProfilePage } from "./pages/profile/profile-page";
import { MyRouter } from "./shared/router/router";

function createRouter(routes) {
return (path) => {
const route = routes[path] || routes["404"];
return route;
};
}
const routes = {
"/": HomePage,
"/profile": ProfilePage,
"/login": LoginPage,
404: Error404Page,
};
export const routes = {
target: (pathname, hash) => {
if (pathname === "/" && hash === "") {
return HomePage;
}

if (hash === "") {
const routeList = {
"^/$": HomePage,
"^/login": LoginPage,
"^/profile": ProfilePage,
".*": Error404Page,
};

return routeList[
Object.keys(routeList).find((route) =>
new RegExp(route).test(pathname),
) ?? ".*"
];
}

const router = createRouter(routes);
const routeList = {
"^#/$": HomePage,
"^#/login": LoginPage,
"^#/profile": ProfilePage,
".*": Error404Page,
};

return routeList[
Object.keys(routeList).find((route) => new RegExp(route).test(hash)) ??
".*"
];
},
};

export const App = {
render: () => {
const path = MyRouter.pathname;
const CurrentPage = router(path);
const CurrentPage = routes.target(
window.location.pathname,
window.location.hash,
);
document.querySelector("#root").innerHTML = CurrentPage();
CurrentPage.eventFn?.();
},
Expand Down

0 comments on commit d68a7af

Please sign in to comment.