🚧 improve routing for mobile examples screen

This commit is contained in:
ghalestrilo 2020-07-30 15:40:43 -03:00
parent 6efc22c8a0
commit 6e668cda68
5 changed files with 49 additions and 22 deletions

View file

@ -34,7 +34,8 @@ class App extends React.Component {
render() { render() {
return ( return (
<div className="app"> <div className="app">
{this.state.isMounted && !window.devToolsExtension && getConfig('NODE_ENV') === 'development' && <DevTools />} {/* FIXME: remove false */}
{false && this.state.isMounted && !window.devToolsExtension && getConfig('NODE_ENV') === 'development' && <DevTools />}
{this.props.children} {this.props.children}
</div> </div>
); );

View file

@ -48,7 +48,7 @@ const NavItem = styled.li`
const headerNavOptions = [ const headerNavOptions = [
{ icon: PreferencesIcon, title: 'Preferences', href: '/mobile/preferences', }, { icon: PreferencesIcon, title: 'Preferences', href: '/mobile/preferences', },
{ icon: PreferencesIcon, title: 'Examples', href: '/mobile/examples' }, { icon: PreferencesIcon, title: 'Examples', href: '/p5/sketches' },
{ icon: PreferencesIcon, title: 'Original Editor', href: '/', }, { icon: PreferencesIcon, title: 'Original Editor', href: '/', },
]; ];

View file

@ -1,6 +1,8 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import styled from 'styled-components'; import styled from 'styled-components';
import { withRouter } from 'react-router';
import Screen from '../../components/mobile/MobileScreen'; import Screen from '../../components/mobile/MobileScreen';
import Header from '../../components/mobile/Header'; import Header from '../../components/mobile/Header';
import IconButton from '../../components/mobile/IconButton'; import IconButton from '../../components/mobile/IconButton';
@ -42,15 +44,20 @@ const FooterTabSwitcher = styled.div`
`; `;
const Panels = { const Panels = {
Sketches: props => <SketchList {...props} />, Sketches: SketchList,
Collections: props => <CollectionList {...props} />, Collections: CollectionList,
Assets: props => <AssetList {...props} /> Assets: AssetList
}; };
const MobileDashboard = ({ username }) => { const renderPanel = (name, props) => (Component => (Component && <Component {...props} />))(Panels[name]);
const MobileDashboard = ({ params }) => {
const Tabs = Object.keys(Panels); const Tabs = Object.keys(Panels);
const [selected, selectTab] = useState(Tabs[0]); const [selected, selectTab] = useState(Tabs[0]);
// const username = 'p5';
const { username } = params;
const isExamples = username === EXAMPLE_USERNAME; const isExamples = username === EXAMPLE_USERNAME;
return ( return (
@ -64,7 +71,7 @@ const MobileDashboard = ({ username }) => {
<Subheader> <Subheader>
<SketchSearchbar /> <SketchSearchbar />
</Subheader> </Subheader>
{Panels[selected] && Panels[selected]({ username })} {renderPanel(selected, { username })}
</Content> </Content>
<Footer> <Footer>
@ -85,8 +92,12 @@ const MobileDashboard = ({ username }) => {
</Screen>); </Screen>);
}; };
MobileDashboard.propTypes = { username: PropTypes.string }; MobileDashboard.propTypes = {
MobileDashboard.defaultProps = { username: '' }; params: PropTypes.shape({
username: PropTypes.string.isRequired
})
};
MobileDashboard.defaultProps = { params: {} };
export default MobileDashboard; export default withRouter(MobileDashboard);

View file

@ -58,10 +58,13 @@ const routes = store => (
<Route path="/:username/collections/:collection_id" component={CollectionView} /> <Route path="/:username/collections/:collection_id" component={CollectionView} />
<Route path="/about" component={IDEView} /> <Route path="/about" component={IDEView} />
<Route path="/mobile" component={MobileIDEView} /> <Route path="/mobile/:username/sketches/:project_id" component={MobileIDEView} />
<Route path="/mobile/:username/sketches" component={MobileDashboard} />
<Route path="/mobile/preview" component={MobileSketchView} /> <Route path="/mobile/preview" component={MobileSketchView} />
<Route path="/mobile/preferences" component={MobilePreferences} /> <Route path="/mobile/preferences" component={MobilePreferences} />
<Route path="/mobile/examples" component={() => <MobileDashboard username="p5" />} /> <Route path="/mobile" component={MobileIDEView} />
</Route> </Route>
); );

View file

@ -114,16 +114,6 @@ router.get('/about', (req, res) => {
res.send(renderIndex()); res.send(renderIndex());
}); });
if (process.env.MOBILE_ENABLED) {
router.get('/mobile', (req, res) => res.send(renderIndex()));
router.get('/mobile/preview', (req, res) => res.send(renderIndex()));
router.get('/mobile/preferences', (req, res) => res.send(renderIndex()));
router.get('/mobile/examples', (req, res) => res.send(renderIndex()));
}
router.get('/:username/collections/create', (req, res) => { router.get('/:username/collections/create', (req, res) => {
userExists(req.params.username, (exists) => { userExists(req.params.username, (exists) => {
const isLoggedInUser = req.user && req.user.username === req.params.username; const isLoggedInUser = req.user && req.user.username === req.params.username;
@ -152,4 +142,26 @@ router.get('/:username/collections', (req, res) => {
)); ));
}); });
// Mobile Routes
if (process.env.MOBILE_ENABLED) {
router.get('/mobile', (req, res) => res.send(renderIndex()));
router.get('/mobile/preview', (req, res) => res.send(renderIndex()));
router.get('/mobile/preferences', (req, res) => res.send(renderIndex()));
router.get('/mobile/:username/sketches', (req, res) => {
userExists(req.params.username, exists => (
exists ? res.send(renderIndex()) : get404Sketch(html => res.send(html))
));
});
router.get('/mobile/:username/sketches/:project_id', (req, res) => {
projectForUserExists(req.params.username, req.params.project_id, exists => (
exists ? res.send(renderIndex()) : get404Sketch(html => res.send(html))
));
});
}
export default router; export default router;