-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathapp.js
More file actions
128 lines (114 loc) · 3.27 KB
/
app.js
File metadata and controls
128 lines (114 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React, { Component, Fragment } from "react";
import { Route, Router, Redirect } from "react-router-dom";
import { connect } from "react-redux";
import { CartService } from "./services";
import { ConfigService } from "./services";
import Meeting from './pages/home/components/videoCall/Meeting';
import { Header, Footer, DebugHeader } from "./shared";
import {
Home,
List,
MyCoupons,
Detail,
SuggestedProductsList,
Profile,
ShoppingCart,
} from "./pages";
import "./i18n";
import "./main.scss";
import { createBrowserHistory } from "history";
import { ai } from "./services/telemetryClient";
// add appinsights
const history = createBrowserHistory({ basename: "" });
(async () => {
await ConfigService.loadSettings();
if (ConfigService._applicationInsightsIntrumentationKey) {
ai.initialize(ConfigService._applicationInsightsIntrumentationKey, {
history,
});
}
})();
class App extends Component {
constructor() {
super();
this.state = {
shoppingCart: [],
quantity: null,
};
}
async componentDidMount() {
if (this.props.userInfo.token) {
const shoppingCart = await CartService.getShoppingCart(
this.props.userInfo.token
);
if (shoppingCart) {
this.setState({ shoppingCart });
}
}
if (this.state.shoppingCart != null) {
const quantity = this.state.shoppingCart.reduce(
(oldQty, { qty }) => oldQty + qty,
0
);
this.setState({ quantity });
}
}
ShoppingCart = (quantity) => {
this.setState({ quantity });
};
sumProductInState = () => {
this.setState((prevState) => {
return { quantity: prevState.quantity + 1 };
});
};
render() {
const { quantity } = this.state;
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={(props) =>
this.props.userInfo.loggedIn === true ? (
<Component {...props} {...rest} />
) : (
<Redirect to="/" />
)
}
/>
);
return (
<div className="App">
<Router history={history}>
<Fragment>
<DebugHeader />
<Header quantity={quantity} />
<Route exact path="/" component={Home} />
<Route exact path="/meeting" component={Meeting} />
<Route exact path="/list" component={List} />
<Route exact path="/list/:code" component={List} />
<Route
path="/suggested-products-list"
component={SuggestedProductsList}
/>
<Route
path="/product/detail/:productId"
render={(props) => (
<Detail sumProductInState={this.sumProductInState} {...props} />
)}
/>
<PrivateRoute path="/coupons" component={MyCoupons} />
<PrivateRoute path="/profile" component={Profile} />
<PrivateRoute
path="/shopping-cart"
component={ShoppingCart}
ShoppingCart={this.ShoppingCart}
quantity={this.state.quantity}
/>
<Footer />
</Fragment>
</Router>
</div>
);
}
}
const mapStateToProps = (state) => state.login;
export default connect(mapStateToProps)(App);