Skip to content

Commit c1a076f

Browse files
committed
Update Error routes
1 parent 3dd81ac commit c1a076f

File tree

9 files changed

+98
-19
lines changed

9 files changed

+98
-19
lines changed

src/components/Auth/Auth.thunks.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,27 @@ export const loadUser = () => async dispatch => {
77
const userJson = localStorage.getItem('user') || '{}';
88
const user = JSON.parse(userJson) as IUser;
99
const id = user.id;
10-
console.log('Load user running!');
11-
console.log('User from loadUser', user);
1210
if (!id) {
1311
return dispatch(actions.authError());
1412
}
1513
try {
1614
const res = await axios.get(`${URL.baseAPIUrl}/api/users/${id}`);
17-
console.log('Response', res);
18-
console.log('Hey loadUser function triggered');
1915
if (res) {
2016
return dispatch(actions.userLoaded(res.data));
2117
}
2218
return dispatch(actions.authError());
2319
} catch (error) {
24-
console.log('Some error loadUser running', error);
2520
return dispatch(actions.authError());
2621
}
2722
};
2823

2924
export const login = (payload: ReqLogin) => async dispatch => {
3025
const { username, password } = payload;
31-
console.log('Login triggered');
3226
try {
3327
const res = await axios.get(`${URL.baseAPIUrl}/api/users`);
3428
const allUsers = res.data;
3529

3630
let user = allUsers.filter(x => x.username === username)[0];
37-
console.log('user found from login', user);
3831
if (user && user.password === password) {
3932
dispatch(actions.loginSuccess(user));
4033
dispatch(loadUser());
@@ -47,21 +40,18 @@ export const login = (payload: ReqLogin) => async dispatch => {
4740
};
4841

4942
export const register = (payload: ReqLogin) => async dispatch => {
50-
console.log('Register triggered');
5143
try {
5244
const id = uuid();
5345
const accessToken = id;
5446

5547
const newUser = { ...payload, id, accessToken };
5648
await axios.post(`${URL.baseAPIUrl}/api/users`, newUser);
5749
dispatch(actions.registerSuccess(newUser));
58-
console.log('Resgister success', newUser);
5950
dispatch(loadUser());
6051
} catch (error) {
6152
return dispatch(actions.registerFailed());
6253
}
6354
};
6455
export const logout = () => async dispatch => {
65-
console.log('Logout triggered');
6656
return dispatch(actions.logoutSuccess());
6757
};

src/components/Error/404.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { ExclamationOutlined } from '@ant-design/icons';
2+
import { Button } from 'antd';
3+
import React from 'react';
4+
import { useHistory } from 'react-router-dom';
5+
6+
export const NotFound = () => {
7+
const history = useHistory();
8+
const goBack = () => {
9+
history.goBack();
10+
};
11+
return (
12+
<div className="static-pages-section">
13+
<div className="container">
14+
<div className="block-title">
15+
<h2>
16+
<ExclamationOutlined />
17+
Page Not Found
18+
</h2>
19+
<p className="large">Sorry, this page does not exist</p>
20+
<Button type="primary" onClick={goBack}>
21+
Go Back
22+
</Button>
23+
</div>
24+
</div>
25+
</div>
26+
);
27+
};

src/components/Products/Product.thunks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export const getProducts = () => async dispatch => {
88
const res = await axios.get(`${URL.baseAPIUrl}/api/products`);
99
const products = res.data;
1010
dispatch(actions.getProductsSuccess(products));
11-
console.log('REs product', res);
1211
} catch (error) {
1312
const payload = {
1413
msg: error.response?.statusText,
@@ -20,6 +19,7 @@ export const getProducts = () => async dispatch => {
2019

2120
export const getProduct = id => async dispatch => {
2221
try {
22+
console.log('Get Product triggered');
2323
const res = await axios.get(`${URL.baseAPIUrl}/api/products/${id}`);
2424
const product = res.data as Product;
2525
dispatch(actions.getProductSuccess(product));
Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
2+
import { connect, ConnectedProps } from 'react-redux';
3+
import { getProduct } from './Product.thunks';
4+
import { useParams } from 'react-router-dom';
5+
import { Row, Col, Image } from 'antd';
26

3-
export const ProductItem = () => {
7+
const mapStateToProps = (state: AppState) => ({
8+
product: state.products.product,
9+
});
10+
const mapDispatchToProps = {
11+
getProduct,
12+
};
13+
interface Params {
14+
id: string;
15+
}
16+
const connector = connect(mapStateToProps, mapDispatchToProps);
17+
interface Props extends ConnectedProps<typeof connector> {}
18+
19+
export const _ProductItem = (props: Props) => {
20+
const { product, getProduct } = props;
21+
const params: Params = useParams();
22+
useEffect(() => {
23+
const { id } = params;
24+
getProduct(id);
25+
}, [params, getProduct]);
426
return (
5-
<div>
6-
<h1>ProductItem Components</h1>
27+
<div className="container">
28+
<Row>
29+
<Col span={12}>
30+
<Image src={product.image_url} />
31+
</Col>
32+
<Col span={8} offset={4}>
33+
<h2>Name: {product.name}</h2>
34+
<p>Brand: {product.brand}</p>
35+
</Col>
36+
</Row>
737
</div>
838
);
939
};
40+
export const ProductItem = connector(_ProductItem);

src/components/Products/ProductList.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { PlusOutlined } from '@ant-design/icons';
22
import { Button, Col, Row, Table, Image } from 'antd';
3-
import React, { useEffect, useState } from 'react';
3+
import React, { useEffect } from 'react';
44
import { connect, ConnectedProps } from 'react-redux';
55
import { Link, useHistory } from 'react-router-dom';
66
import { PATH } from 'src/constants/paths';
77
import { getProducts } from './Product.thunks';
8-
import imgDefault from 'src/assets/images/products/apple/iphone-8-plus.jpg';
98

109
const mapStateToProps = (state: AppState) => ({
1110
loading: state.products.loading,

src/pages/ErrorPages/404Pages.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import { NotFound } from 'src/components/Error/404';
3+
import { MainLayout } from 'src/layouts/MainLayout';
4+
5+
const _NotFoundPage = () => {
6+
return (
7+
<MainLayout>
8+
<NotFound />
9+
</MainLayout>
10+
);
11+
};
12+
const NotFoundPage = React.memo(_NotFoundPage);
13+
export default NotFoundPage;

src/routes/ErrorRoutes.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React, { lazy, Suspense } from 'react';
2+
import { Route, Switch } from 'react-router-dom';
3+
import { Loading } from 'src/components/Loading';
4+
const NotFoundPage = lazy(() => import('src/pages/ErrorPages/404Pages'));
5+
export const ErrorRoutes = () => {
6+
return (
7+
<Switch>
8+
<Route
9+
component={() => (
10+
<Suspense fallback={<Loading />}>
11+
<NotFoundPage />
12+
</Suspense>
13+
)}
14+
/>
15+
</Switch>
16+
);
17+
};

src/routes/ProductRoutes.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { lazy, Suspense } from 'react';
2-
import { Route, Switch } from 'react-router-dom';
2+
import { Switch } from 'react-router-dom';
33
import { PATH } from 'src/constants/paths';
44
import { Loading } from 'src/components/Loading';
55
import { AuthenticatedGuard } from 'src/guards/AuthenticatedGuard';
@@ -24,7 +24,7 @@ export const AuthRoutes = () => {
2424
/>
2525
<AuthenticatedGuard
2626
exact
27-
path={PATH.PRODUCTS + '/:id'}
27+
path="/products/:id"
2828
component={() => (
2929
<Suspense fallback={<Loading />}>
3030
<ProductItem />

src/routes/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import React from 'react';
22
import { BrowserRouter } from 'react-router-dom';
33
import { StaticPageRoutes } from './StaticPageRoutes';
44
import { AuthRoutes } from './AuthRoutes';
5+
import { ErrorRoutes } from './ErrorRoutes';
56

67
export const Routes = () => {
78
return (
89
<BrowserRouter>
910
<StaticPageRoutes />
1011
<AuthRoutes />
12+
<ErrorRoutes />
1113
</BrowserRouter>
1214
);
1315
};

0 commit comments

Comments
 (0)