Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update core struct #111

Merged
merged 2 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Router } from "./Router";
import { render, fireEvent } from "@testing-library/react";
import { Routers } from "../core/Routers";
import LangService from "../core/LangService";
import { TOpenRouteParams } from "../core/helpers";
import { TOpenRouteParams } from "../core/core";
import { TRoute } from "./Router";
import { createBrowserHistory } from "history";

Expand Down
8 changes: 2 additions & 6 deletions src/components/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import React, {
useCallback,
useMemo,
} from "react";
import {
createUrl,
joinPaths,
removeLastCharFromString,
TOpenRouteParams,
} from "../core/helpers";
import { createUrl, TOpenRouteParams } from "../core/core";
import { joinPaths, removeLastCharFromString } from "../core/helpers";
import { useRouter } from "../hooks/useRouter";
import { useLocation } from "../hooks/useLocation";
import debug from "@wbe/debug";
Expand Down
3 changes: 1 addition & 2 deletions src/components/Router.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { render } from "@testing-library/react";
import { TRoute } from "./Router";
import { createBrowserHistory } from "history";


describe("Router", () => {
const routesList: TRoute[] = [
{
path: "/",
component: null,
name: "Home"
name: "Home",
},
];

Expand Down
4 changes: 2 additions & 2 deletions src/components/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import debug from "@wbe/debug";
import { BrowserHistory, HashHistory, MemoryHistory } from "history";
import { Match } from "path-to-regexp";
import React from "react";
import { formatRoutes } from "../core/helpers";
import { getNotFoundRoute, getRouteFromUrl } from "../core/matcher";
import { formatRoutes } from "../core/core";
import { getNotFoundRoute, getRouteFromUrl } from "../core/core";
import { Routers } from "../core/Routers";
import LangService from "../core/LangService";
import { staticPropsCache } from "../core/staticPropsCache";
Expand Down
21 changes: 5 additions & 16 deletions src/core/LangService.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ afterEach(() => {

/**
* redirect
*
*
*/
it("should redirect to default lang", () => {
const langService = new LangService({ languages: locales, base: "/" });
Expand Down Expand Up @@ -80,28 +82,15 @@ it("should not redirect to default lang if showDefaultLangInUrl is set to false"
expect(window.open).toHaveBeenCalledTimes(0);
});

/**
* setLang
* FIXME can't test because setLang use "prepareFullUrl()" which use Routers property not setted
*/
// it("should set lang properly", () => {
// const langService = new LangService({ languages: locales, base: "/" });
// render(<App langService={langService} />);
// act(() => {
// console.log("Routers", Routers.currentRoutes);
// Routers.langService.setLang(locales[1]);
// });
// expect(window.open).toHaveBeenCalledWith(`/${locales[1].key}`, "_self");
// });

// -------------------------------------------------------------------------------------

/**
* Add lang path param allows to test the same array before and after middleware
* The method added langPath param even if it doesn't patch all routes
* @param addLangPath
*
*
*
*/

const routesListLang: TRoute[] = [
{
path: "/",
Expand Down
37 changes: 28 additions & 9 deletions src/core/LangService.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { Routers } from "./Routers";
import {
compileUrl,
createUrl,
joinPaths,
removeLastCharFromString,
getLangPathByLang,
isSSR,
} from "./helpers";
import { compileUrl, createUrl } from "./core";
import { isSSR, joinPaths, removeLastCharFromString } from "./helpers";
import { TRoute } from "../components/Router";
import debug from "@wbe/debug";

Expand Down Expand Up @@ -277,7 +271,7 @@ class LangService<TLang = any> {
*/
const patchRoutes = (pRoutes, children = false) => {
return pRoutes.map((route: TRoute) => {
const path = getLangPathByLang(route);
const path = this.getLangPathByLang(route);
const hasChildren = route.children?.length > 0;
const showLang = !children && showLangInUrl;

Expand Down Expand Up @@ -310,6 +304,31 @@ class LangService<TLang = any> {

// --------------------------------------------------------------------------- LOCAL

/**
* Get current lang path by Lang
* ex:
* const route = {
* component: ...,
* path: { en: "/about", fr: "/a-propos", de: "uber", name: "about" },
* }
*
* selectLangPathByLang(route, "fr") // will return "/a-propos"
*
* @param route
* @param lang
*/
protected getLangPathByLang(route: TRoute, lang = this.currentLang.key): string {
let selectedPath: string;
if (typeof route.path === "string") {
selectedPath = route.path;
} else if (typeof route.path === "object") {
Object.keys(route.path).find((el) => {
if (el === lang) selectedPath = route.path?.[el];
});
}
return selectedPath;
}

/**
* Returns default language of the list
* If no default language exist, it returns the first language object of the languages array
Expand Down
Loading