Skip to content

Commit 6ba0970

Browse files
committed
get the correct Article/Document slug with locale
1 parent 0ec0d5e commit 6ba0970

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

web/src/apps/main/redux/actions/articles-page/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { Article } from "@dzcode.io/api/dist/app/types/legacy";
2+
import { LanguageEntity } from "@dzcode.io/models/dist/language";
23
import { isLoaded } from "@dzcode.io/utils/dist/loadable";
34
import * as Sentry from "@sentry/browser";
45
import { listToTree } from "l2t";
6+
import { matchPath } from "react-router-dom";
57
import { ThunkResult } from "src/apps/main/redux";
68
import { ArticlesState } from "src/apps/main/redux/reducers/articles";
79
import { ArticlesPageState } from "src/apps/main/redux/reducers/articles-page";
810
import { SidebarTreeItem } from "src/apps/main/types";
911
import { hasInCollection } from "src/common/utils";
1012
import { fetchV2 } from "src/common/utils/fetch";
13+
import { history } from "src/common/utils/history";
14+
import { urlLanguageRegEx } from "src/common/utils/language";
1115

1216
/**
1317
* Fetches the list of articles for the sidebar
@@ -164,9 +168,11 @@ export const fetchCurrentArticleAuthors =
164168
*/
165169
export const fetchCurrentArticle =
166170
(): ThunkResult<ArticlesPageState | ArticlesState> => async (dispatch, getState) => {
167-
const slug = location.pathname
168-
.substring(location.pathname.indexOf("/", 1) + 1)
169-
.replace(/\/$/, "");
171+
const match = matchPath<{ lang?: LanguageEntity["code"]; slug: string }>(
172+
history.location.pathname,
173+
{ path: `${urlLanguageRegEx}/Articles/:slug(.*)` },
174+
);
175+
const slug = match?.params.slug || "";
170176

171177
const cashedArticle = hasInCollection<Article>(getState().articles.list, "slug", slug, [
172178
["content"],

web/src/apps/main/redux/actions/documentation-page/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { Document } from "@dzcode.io/api/dist/app/types/legacy";
2+
import { LanguageEntity } from "@dzcode.io/models/dist/language";
23
import { isLoaded } from "@dzcode.io/utils/dist/loadable";
34
import * as Sentry from "@sentry/browser";
45
import { listToTree } from "l2t";
6+
import { matchPath } from "react-router-dom";
57
import { ThunkResult } from "src/apps/main/redux";
68
import { DocumentationState } from "src/apps/main/redux/reducers/documentation";
79
import { LearnPageState } from "src/apps/main/redux/reducers/learn-page";
810
import { SidebarTreeItem } from "src/apps/main/types";
911
import { hasInCollection } from "src/common/utils";
1012
import { fetchV2 } from "src/common/utils/fetch";
13+
import { history } from "src/common/utils/history";
14+
import { urlLanguageRegEx } from "src/common/utils/language";
1115

1216
/**
1317
* Fetches the list of documents for the sidebar
@@ -163,9 +167,11 @@ const fetchCurrentDocumentAuthors =
163167
*/
164168
export const fetchCurrentDocument =
165169
(): ThunkResult<LearnPageState | DocumentationState> => async (dispatch, getState) => {
166-
const slug = location.pathname
167-
.substring(location.pathname.indexOf("/", 1) + 1)
168-
.replace(/\/$/, "");
170+
const match = matchPath<{ lang?: LanguageEntity["code"]; slug: string }>(
171+
history.location.pathname,
172+
{ path: `${urlLanguageRegEx}/Learn/:slug(.*)` },
173+
);
174+
const slug = match?.params.slug || "";
169175
const cashedDocument = hasInCollection<Document>(getState().documentation.list, "slug", slug, [
170176
["content"],
171177
]);

0 commit comments

Comments
 (0)