Skip to content

Commit

Permalink
Remove Get current route method (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy Brauner authored Jul 11, 2022
1 parent 158dbeb commit c1efe1a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 72 deletions.
22 changes: 15 additions & 7 deletions src/components/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BrowserHistory, HashHistory, MemoryHistory } from "history";
import { Match } from "path-to-regexp";
import React from "react";
import { formatRoutes } from "../core/helpers";
import { getCurrentRoute, getNotFoundRoute } from "../core/matcher";
import { getNotFoundRoute, getRouteFromUrl } from "../core/matcher";
import { Routers } from "../core/Routers";
import LangService from "../core/LangService";
import { staticPropsCache } from "../core/staticPropsCache";
Expand Down Expand Up @@ -237,19 +237,27 @@ function Router(props: {
return;
}

const newRoute = getCurrentRoute({
url: exactUrl,
base: props.base,
routes: routes,
notFoundRoute: getNotFoundRoute(props.routes),
const matchingRoute = getRouteFromUrl({
pUrl: exactUrl,
pRoutes: routes,
pBase: props.base,
id: props.id,
});

const notFoundRoute = getNotFoundRoute(props.routes);
if (!matchingRoute && !notFoundRoute) {
log(props.id, "matchingRoute not found & 'notFoundRoute' not found, return.");
return;
}

const currentRouteUrl = currentRouteRef.current?.url;
if (currentRouteUrl != null && currentRouteUrl === newRoute?.url) {
if (currentRouteUrl != null && currentRouteUrl === matchingRoute?.url) {
log(props.id, "this is the same route 'url', return.");
return;
}

const newRoute: TRoute = matchingRoute || notFoundRoute;

// if no newRoute, do not continue
if (!newRoute) return;

Expand Down
21 changes: 14 additions & 7 deletions src/core/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import debug from "@wbe/debug";
import { compile } from "path-to-regexp";
import { TRoute } from "../components/Router";
import LangService from "./LangService";
import { getCurrentRoute, getNotFoundRoute } from "./matcher";
import { getNotFoundRoute, getRouteFromUrl } from "./matcher";

const componentName: string = "helpers";
const log = debug(`router:${componentName}`);
Expand Down Expand Up @@ -204,14 +204,21 @@ export async function requestStaticPropsFromRoute({
middlewares?: ((routes: TRoute[]) => TRoute[])[];
id?: string | number;
}): Promise<{ props: any; name: string }> {
// get current route
const currentRoute = getCurrentRoute({
url,
base,
routes: formatRoutes(routes, langService, middlewares, id),
notFoundRoute: getNotFoundRoute(routes),

const currentRoute = getRouteFromUrl({
pUrl: url,
pBase: base,
pRoutes: formatRoutes(routes, langService, middlewares, id),
id,
});

const notFoundRoute = getNotFoundRoute(routes);

if (!currentRoute && !notFoundRoute) {
log(id, "currentRoute not found & 'notFoundRoute' not found, return.");
return;
}

// get out
if (!currentRoute) {
console.error("No currentRoute, return");
Expand Down
23 changes: 1 addition & 22 deletions src/core/matcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TRoute } from "../components/Router";
import { formatRoutes, preventSlashes } from "./helpers";
import { getCurrentRoute, getNotFoundRoute, getRouteFromUrl } from "./matcher";
import { getNotFoundRoute, getRouteFromUrl } from "./matcher";

const routesList: TRoute[] = [
{
Expand Down Expand Up @@ -106,25 +106,4 @@ describe("matcher", () => {
});
expect(getRoute).toBeUndefined();
});

it("should get current route (matching route or notFound route)", () => {
// example-client returns right route
const route2 = getCurrentRoute({
url: "/bar/test-id",
base: "/",
routes: formatRoutes(routesList),
notFoundRoute: getNotFoundRoute(routesList),
});
expect(route2.name).toBe("BarPage");

// example-client returns NotFound route
const route3 = getCurrentRoute({
url: "/barrrrrr/test-id",
base: "/",
routes: formatRoutes(routesList),
notFoundRoute: getNotFoundRoute(routesList),
});

expect(route3.name).toBe("NotFoundPage");
});
});
37 changes: 2 additions & 35 deletions src/core/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ type TGetRouteFromUrl = {
* Get current route from URL, using path-to-regex
* @doc https://github.com/pillarjs/path-to-regexp
*/
export const getRouteFromUrl = ({
export function getRouteFromUrl({
pUrl,
pRoutes,
pBase,
pMatcher,
id,
}: TGetRouteFromUrl): TRoute => {
}: TGetRouteFromUrl): TRoute {
if (!pRoutes || pRoutes?.length === 0) return;

// keep first level current route.
Expand Down Expand Up @@ -106,37 +106,4 @@ export const getRouteFromUrl = ({
}

return next({ pUrl, pRoutes, pBase, pMatcher, id });
};

/**
* Get current Route
* Will get route from URL and return notFound if exist
* @param url
* @param routes
* @param base
* @param langService
*/
export function getCurrentRoute({
url,
routes,
notFoundRoute,
base,
}: {
url: string;
routes: TRoute[];
notFoundRoute: TRoute;
base: string;
}): TRoute {
const matchingRoute = getRouteFromUrl({
pUrl: url,
pBase: base,
pRoutes: routes,
});

if (!matchingRoute && !notFoundRoute) {
log("matchingRoute not found & 'notFoundRoute' not found, return.");
return;
}

return matchingRoute || notFoundRoute;
}
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export {
getPathByRouteName,
requestStaticPropsFromRoute,
} from "./core/helpers";
export { getCurrentRoute } from "./core/matcher";
export type { TOpenRouteParams } from "./core/helpers";

export { Stack } from "./components/Stack";
Expand Down

0 comments on commit c1efe1a

Please sign in to comment.