Skip to content

Commit b1bfb91

Browse files
committed
Serve assets from /:username/assets, redirecting old path
1 parent a0a13ab commit b1bfb91

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

client/components/Nav.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ class Nav extends React.PureComponent {
576576
</li>
577577
<li className="nav__dropdown-item">
578578
<Link
579-
to="/assets"
579+
to={`/${this.props.user.username}/assets`}
580580
onFocus={this.handleFocusForAccount}
581581
onBlur={this.handleBlur}
582582
onClick={this.setDropdownForNone}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { connect } from 'react-redux';
4+
import { browserHistory } from 'react-router';
5+
6+
const RedirectToUser = ({ username, url = '/:username/sketches' }) => {
7+
React.useEffect(() => {
8+
if (username == null) {
9+
return;
10+
}
11+
12+
browserHistory.replace(url.replace(':username', username));
13+
}, [username]);
14+
15+
return null;
16+
};
17+
18+
function mapStateToProps(state) {
19+
return {
20+
username: state.user ? state.user.username : null,
21+
};
22+
}
23+
24+
const ConnectedRedirectToUser = connect(mapStateToProps)(RedirectToUser);
25+
26+
const createRedirectWithUsername = (url) => (props) => <ConnectedRedirectToUser {...props} url={url} />;
27+
28+
export default createRedirectWithUsername;

client/routes.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import EmailVerificationView from './modules/User/pages/EmailVerificationView';
1010
import NewPasswordView from './modules/User/pages/NewPasswordView';
1111
import AccountView from './modules/User/pages/AccountView';
1212
import DashboardView from './modules/User/pages/DashboardView';
13-
// import SketchListView from './modules/Sketch/pages/SketchListView';
13+
import createRedirectWithUsername from './components/createRedirectWithUsername';
1414
import { getUser } from './modules/User/actions';
1515
import { stopSketch } from './modules/IDE/actions/ide';
1616

@@ -36,11 +36,13 @@ const routes = store => (
3636
<Route path="/projects/:project_id" component={IDEView} />
3737
<Route path="/:username/full/:project_id" component={FullView} />
3838
<Route path="/full/:project_id" component={FullView} />
39-
<Route path="/sketches" component={DashboardView} />
40-
<Route path="/assets" component={DashboardView} />
39+
<Route path="/sketches" component={createRedirectWithUsername('/:username/sketches')} />
40+
<Route path="/:username/assets" component={DashboardView} />
41+
<Route path="/assets" component={createRedirectWithUsername('/:username/assets')} />
4142
<Route path="/account" component={AccountView} />
4243
<Route path="/:username/sketches/:project_id" component={IDEView} />
4344
<Route path="/:username/sketches" component={DashboardView} />
45+
<Route path="/:username/assets" component={DashboardView} />
4446
<Route path="/about" component={IDEView} />
4547
<Route path="/feedback" component={IDEView} />
4648
</Route>

server/routes/server.routes.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ router.get('/assets', (req, res) => {
7979
}
8080
});
8181

82+
router.get('/:username/assets', (req, res) => {
83+
userExists(req.params.username, (exists) => {
84+
const isLoggedInUser = req.user && req.user.username === req.params.username;
85+
const canAccess = exists && isLoggedInUser;
86+
return canAccess ?
87+
res.send(renderIndex()) :
88+
get404Sketch(html => res.send(html));
89+
});
90+
});
91+
8292
router.get('/account', (req, res) => {
8393
if (req.user) {
8494
res.send(renderIndex());

0 commit comments

Comments
 (0)