Skip to content

Commit adc5060

Browse files
authored
Merge pull request #228 from NurElHuda/107-handle-article-not-found
* article not found handeling.
2 parents 8904fd8 + 4aa1f97 commit adc5060

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

frontend/src/apps/main/entry/index.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import "./style";
22
import "react-toastify/dist/ReactToastify.css";
3-
import {
4-
BrowserRouter,
5-
Redirect,
6-
Route,
7-
Switch,
8-
useLocation,
9-
} from "react-router-dom";
103
import React, { Suspense, lazy } from "react";
4+
import { Route, Router, Switch, useLocation } from "react-router-dom";
115
import Container from "@material-ui/core/Container";
126
import CssBaseline from "@material-ui/core/CssBaseline";
137
import { Footer } from "t9/apps/main/components/footer";
@@ -17,6 +11,7 @@ import { Provider } from "react-redux";
1711
import { Theme } from "t9/apps/main/components/theme";
1812
import { ToastContainer } from "react-toastify";
1913
import { getEnv } from "src/common/utils";
14+
import { history } from "src/common/utils/history";
2015
import { mainStore } from "t9/apps/main/redux";
2116
import { makeStyles } from "@material-ui/core/styles";
2217
import { render } from "react-dom";
@@ -86,9 +81,9 @@ export const App = () => {
8681

8782
render(
8883
<Provider store={mainStore}>
89-
<BrowserRouter>
84+
<Router history={history}>
9085
<App />
91-
</BrowserRouter>
86+
</Router>
9287
</Provider>,
9388
document.getElementById("app-container"),
9489
);

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Dispatch } from "react";
55
import { actionType } from "../../constants";
66
import { fullstackConfig } from "src/config";
77
import { hasInCollection } from "src/common/utils";
8+
import { history } from "src/common/utils/history";
89
import { listToTree } from "l2t";
910

1011
const dataURL = fullstackConfig.data.url;
@@ -49,6 +50,7 @@ export const fetchCurrentArticle = () => async (
4950
const articleSlug = location.pathname
5051
.substring(location.pathname.indexOf("/", 1) + 1)
5152
.replace(/\/$/, "");
53+
5254
const cashedArticle = hasInCollection<Article>(
5355
getState().articles,
5456
"slug",
@@ -63,14 +65,21 @@ export const fetchCurrentArticle = () => async (
6365
});
6466
} else {
6567
// BUG: cashing not working in local (slug related issue)
68+
6669
dispatch({
6770
type: actionType.UPDATE_ARTICLES_PAGE,
6871
payload: { currentArticle: null },
6972
});
73+
7074
try {
7175
const response = await Axios.get(
7276
dataURL + `/articles/${articleSlug}.json`,
7377
);
78+
79+
if (response.data.hasOwnProperty("error")) {
80+
throw Error("article_not_found");
81+
}
82+
7483
const currentArticle = response.data;
7584
// update our page state
7685
dispatch({
@@ -83,7 +92,9 @@ export const fetchCurrentArticle = () => async (
8392
payload: [currentArticle],
8493
});
8594
} catch (error) {
86-
console.error(error);
95+
if (error.message == "article_not_found") {
96+
history.push("/Articles");
97+
}
8798
}
8899
}
89100
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createBrowserHistory } from "history";
2+
3+
export const history = createBrowserHistory();

0 commit comments

Comments
 (0)