Skip to content

Commit

Permalink
Merge pull request #37 from seeliang/feat-loading-gate
Browse files Browse the repository at this point in the history
Feat loading gate
  • Loading branch information
seeliang authored Jan 11, 2020
2 parents 38fc41b + 12e0f75 commit 57bf254
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 46 deletions.
9 changes: 6 additions & 3 deletions src/atomic/pages/loading.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import React from 'react';
import {useSelector, useDispatch} from 'react-redux';
import Button from '../atoms/button';
import { Redirect } from 'react-router-dom';
import { Redirect, useLocation } from 'react-router-dom';
import {feedFetch} from '../../js/actions';

const Loading = () => {
const {feed} = useSelector(state => state),
dispatch = useDispatch();
dispatch = useDispatch(),
location = useLocation(),

{ from } = location.state || { from: { pathname: '/page' } };
if(typeof feed.status.fetchError === 'boolean' && feed.status.fetchError === true) {
return (
<div>
Expand All @@ -21,7 +24,7 @@ const Loading = () => {
return <h1> loading ... </h1>;
}

return <Redirect to="/page"/>;
return <Redirect to={from} />;
};

export default Loading;
42 changes: 0 additions & 42 deletions src/js/routes.js

This file was deleted.

26 changes: 26 additions & 0 deletions src/js/routes/dataRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import {useSelector} from 'react-redux';
import { Route, Redirect} from 'react-router-dom';


const DataRoute = ({ children, ...rest }) => {
const {feed} = useSelector(state => state);
return (
<Route
{...rest}
render={({ location }) =>
feed.status.fetched ? (
children
) : (
<Redirect
to={{
pathname: '/loading',
state: { from: location }
}}
/>
)
}
/>
);
};
export default DataRoute;
43 changes: 43 additions & 0 deletions src/js/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React , {useEffect}from 'react';
import {useSelector, useDispatch} from 'react-redux';
import {feedFetch} from '../../js/actions';
import { BrowserRouter as Router, Route, Redirect} from 'react-router-dom';
import DataRoute from './dataRoute';
import Loading from '../../atomic/pages/loading';
import Page from '../../atomic/pages/page';
import People from '../../atomic/pages/people';

const Routes = () => {
//demo error fetch
const {feed} = useSelector(state => state),
dispatch = useDispatch();
useEffect(() => {
if(feed.status.fetched === false) {
dispatch(feedFetch('people.jso'));
}
},[]);

return (
<Router>
<div>
<Route exact
path="/"
>
<Redirect to="/loading" />
</Route>
<Route path="/loading">
<Loading />
</Route>
<DataRoute path="/people/:id">
<People />
</DataRoute>
<DataRoute path="/page">
<Page/>
</DataRoute>
</div>
</Router>
);
};


export default Routes;
4 changes: 3 additions & 1 deletion webpack.dev.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const base = require('./webpack.base.js');

module.exports = base;
module.exports = Object.assign(base , {devServer: {
historyApiFallback: true
}});

0 comments on commit 57bf254

Please sign in to comment.