-
Notifications
You must be signed in to change notification settings - Fork 1
/
Routes.js
213 lines (202 loc) · 6.55 KB
/
Routes.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import React, { Component, Fragment } from 'react';
import { connect } from 'react-redux';
import { withRouter, Route, Switch, Redirect, Link } from 'react-router-dom';
import { Login, Signup } from './components/AuthForm';
import Home from './components/Home';
import Cart from './components/Cart';
import {
me,
loadAlbums,
loadCarts,
loadGuests,
createGuest,
createCart,
getLineItems,
selectCart,
deselectCart,
loadUsers,
} from './store';
import Profile from './components/Profile';
import AlbumDetail from './components/AlbumDetail';
import AlbumSearch from './components/AlbumSearch';
import AdminHome from './components/Admin/AdminHome';
import Success from './components/Stripe/Success';
import Failed from './components/Stripe/Failed';
import Genre from './components/Genre/Genre';
import A_UserList from './components/Admin/A_UserList';
import A_AlbumDetail from './components/Admin/A_AlbumDetail';
import A_AlbumList from './components/Admin/A_AlbumList';
import PageSearch from './components/Search/PageSearch';
import PageDetail from './components/Detail/PageDetail';
/**
* COMPONENT
*/
class Routes extends Component {
async componentDidMount() {
await this.props.loadInitialData();
await this.props.loadGuests();
await this.props.getLineItems();
await this.props.loadAlbums();
await this.props.loadCarts();
const {
isLoggedIn,
auth,
carts,
selectCart,
selectedCart,
createCart,
guests,
} = this.props;
if (
(!window.localStorage.guestId && !auth.id) ||
window.localStorage.guestId * 1 > guests.length
)
await this.props.createGuest();
if (!selectedCart.id) {
const toSelect = isLoggedIn
? carts.find((cart) => cart.userId === auth.id && !cart.isPurchased)
: carts.find(
(cart) => cart.guestId === window.localStorage.guestId * 1
);
toSelect || selectedCart.id
? selectCart(toSelect)
: isLoggedIn
? createCart({ userId: auth.id })
: createCart({ guestId: window.localStorage.guestId });
}
}
async componentDidUpdate(prevProps) {
const {
isLoggedIn,
getLineItems,
carts,
auth,
selectCart,
deselectCart,
guests,
selectedCart,
} = this.props;
if (prevProps.isLoggedIn != isLoggedIn) {
await this.props.loadCarts();
getLineItems();
if (
(!auth.id && !window.localStorage.guestId) ||
window.localStorage.guestId * 1 > guests.length
) {
await this.props.createGuest();
}
if (
!selectedCart ||
auth.id ||
(!auth.id && window.localStorage.guestId)
) {
const toSelect = isLoggedIn
? carts.find((cart) => cart.userId === auth.id && !cart.isPurchased)
: carts.find(
(cart) => cart.guestId === window.localStorage.guestId * 1
);
toSelect || selectedCart.id
? selectCart(toSelect)
: auth.id
? createCart({ userId: auth.id })
: createCart({ guestId: window.localStorage.guestId });
}
}
}
render() {
const { isLoggedIn, users } = this.props;
const user = users.find((u) => u.id === this.props.auth.id) || {};
return (
<div>
{isLoggedIn && user.isAdmin === true ? (
<Switch>
<Route exact path='/admin' component={AdminHome} />
<Route path='/users' component={A_UserList} />
<Route path='/inventory' component={A_AlbumList} />
<Route exact path='/admin/albums/:id' component={A_AlbumDetail} />
<Route path='/searchresults' component={PageSearch} />
<Route path='/album/:id' component={PageDetail} />
<Redirect to='/admin' />
</Switch>
) : isLoggedIn && user.isAdmin === false ? (
<Switch>
<Route path='/' exact component={Login} />
<Route path='/home' component={Home} />
<Route path='/cart' component={Cart} />
<Route path='/profile' component={Profile} />
<Route path='/albums/search' component={AlbumSearch} />
<Route path='/albums/:id' component={AlbumDetail} />
<Route path='/genre/:id' component={Genre} />
<Route path='/searchresults' component={PageSearch} />
<Route path='/album/:id' component={PageDetail} />
<Route path='/checkout/success' exact component={Success} />
<Route path='/checkout/failed' exact component={Failed} />
<Redirect to='/' />
</Switch>
) : (
<Switch>
<Route path='/' exact component={Login} />
<Route path='/login' component={Login} />
<Route path='/signup' component={Signup} />
<Route path='/cart' component={Cart} />
<Route path='/albums/search' component={AlbumSearch} />
<Route path='/albums/:id' component={AlbumDetail} />
<Route path='/genre/:id' component={Genre} />
<Route path='/searchresults' component={PageSearch} />
<Route path='/album/:id' component={PageDetail} />
<Route path='/checkout/success' exact component={Success} />
<Route path='/checkout/failed' exact component={Failed} />
<Redirect to='/' />
</Switch>
)}
</div>
);
}
}
/**
* CONTAINER
*/
const mapState = (state) => {
return {
// Being 'logged in' for our purposes will be defined has having a state.auth that has a truthy id.
// Otherwise, state.auth will be an empty object, and state.auth.id will be falsey
isLoggedIn: !!state.auth.id,
...state,
};
};
const mapDispatch = (dispatch) => {
return {
loadInitialData() {
dispatch(me());
dispatch(loadUsers());
// dispatch(loadAlbums());
// dispatch(getCart());
// dispatch(getLineItems());
},
loadAlbums: () => {
return dispatch(loadAlbums());
},
loadGuests: () => {
return dispatch(loadGuests());
},
getLineItems: () => {
return dispatch(getLineItems());
},
createCart: (id) => {
return dispatch(createCart(id));
},
loadCarts: () => {
return dispatch(loadCarts());
},
selectCart: (cart) => {
return dispatch(selectCart(cart));
},
deselectCart: () => dispatch(deselectCart()),
createGuest: () => {
return dispatch(createGuest());
},
};
};
// The `withRouter` wrapper makes sure that updates are not blocked
// when the url changes
export default withRouter(connect(mapState, mapDispatch)(Routes));