feat: Replace Redirect with withRouter

This commit is contained in:
Alexander Polynomdivision 2018-09-19 20:30:42 +02:00
parent 286927663a
commit 1c15c6653f
7 changed files with 203 additions and 221 deletions

View File

@ -52,18 +52,10 @@ export function setUser(user: IUser) {
}; };
} }
export const LEVEL_SET_REVIEW = "LEVEL_SET_REVIEW";
export function setLevelReview(state: boolean) {
return {
type: LEVEL_SET_REVIEW,
state,
};
};
export const LEVEL_SET_LOOKEDAT = "LEVEL_SET_LOOKEDAT"; export const LEVEL_SET_LOOKEDAT = "LEVEL_SET_LOOKEDAT";
export function setLevelLookedAt(ids: number[]) { export function setLevelLookedAt(ids: number[]) {
return { return {
type: LEVEL_SET_REVIEW, type: LEVEL_SET_LOOKEDAT,
lookedAt: ids, lookedAt: ids,
}; };
}; };

View File

@ -10,7 +10,7 @@ import LoginPage from "../containers/LoginPage";
import LevelListPage from "../containers/LevelList"; import LevelListPage from "../containers/LevelList";
import LevelPage from "../containers/LevelPage"; import LevelPage from "../containers/LevelPage";
import ReviewPage from "../containers/Review"; import ReviewPage from "../containers/Review";
import SummaryPage from "../pages/summary"; import SummaryPage from "../containers/SummaryPage";
import WelcomePage from "../pages/intro"; import WelcomePage from "../pages/intro";
import Drawer from "../containers/Drawer"; import Drawer from "../containers/Drawer";
@ -309,8 +309,7 @@ export default class Application extends React.Component<IProps> {
isAuth={this.isAuthenticated} isAuth={this.isAuthenticated}
path="/review/summary" path="/review/summary"
component={() => { component={() => {
return <SummaryPage return <SummaryPage />
reviewMeta={this.getLastReview} />
}} /> }} />
</div> </div>
</div > </div >

View File

@ -1,7 +1,7 @@
import { connect } from "react-redux"; import { connect } from "react-redux";
import { import {
setDrawerButton, setLevelReview, setLevelLookedAt, setDrawerButton, setLevelLookedAt,
setLevelCurrentVocab, setLevelVocab, setLevelLoading setLevelCurrentVocab, setLevelVocab, setLevelLoading
} from "../actions"; } from "../actions";
@ -10,12 +10,11 @@ import { IVocab } from "../models/vocab";
import LevelPage from "../pages/level"; import LevelPage from "../pages/level";
const mapStateToProps = state => { const mapStateToProps = state => {
const { currentVocab, lookedAt, toReview, vocab, loading } = state.level; const { currentVocab, lookedAt, vocab, loading } = state.level;
return { return {
currentVocab, currentVocab,
lookedAt, lookedAt,
toReview,
vocab, vocab,
loading, loading,
}; };
@ -24,7 +23,6 @@ const mapStateToProps = state => {
const mapDispatchToProps = dispatch => { const mapDispatchToProps = dispatch => {
return { return {
drawerButtonState: (state: boolean) => dispatch(setDrawerButton(state)), drawerButtonState: (state: boolean) => dispatch(setDrawerButton(state)),
setReview: (state: boolean) => dispatch(setLevelReview(state)),
setLookedAt: (ids: number[]) => dispatch(setLevelLookedAt(ids)), setLookedAt: (ids: number[]) => dispatch(setLevelLookedAt(ids)),
setCurrentVocab: (vocab: IVocab) => dispatch(setLevelCurrentVocab(vocab)), setCurrentVocab: (vocab: IVocab) => dispatch(setLevelCurrentVocab(vocab)),
setVocab: (vocab: IVocab[]) => dispatch(setLevelVocab(vocab)), setVocab: (vocab: IVocab[]) => dispatch(setLevelVocab(vocab)),

View File

@ -0,0 +1,21 @@
import { connect } from "react-redux";
import { setDrawerButton } from "../actions";
import SummaryPage from "../pages/summary";
const mapStateToProps = state => {
return {
reviewMeta: state.lastReview,
};
};
const mapDispatchToProps = dispatch => {
return {
setDrawerButton: (state: boolean) => dispatch(setDrawerButton(state)),
};
};
const SummaryPageContainer = connect(mapStateToProps,
mapDispatchToProps)(SummaryPage);
export default SummaryPageContainer;

View File

@ -10,7 +10,7 @@ import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent"; import CardContent from "@material-ui/core/CardContent";
import CircularProgress from "@material-ui/core/CircularProgress"; import CircularProgress from "@material-ui/core/CircularProgress";
import { Redirect } from "react-router-dom"; import { withRouter } from "react-router-dom";
import { IVocab } from "../models/vocab"; import { IVocab } from "../models/vocab";
@ -18,20 +18,21 @@ interface IProps {
id: string; id: string;
levelVocab: (id: string) => Promise<IVocab[]>; levelVocab: (id: string) => Promise<IVocab[]>;
history: any;
loading: boolean; loading: boolean;
setLoading: (state: boolean) => void; setLoading: (state: boolean) => void;
vocab: IVocab[]; vocab: IVocab[];
setVocab: (vocab: IVocab[]) => void; setVocab: (vocab: IVocab[]) => void;
setLookedAt: (ids: number[]) => void; setLookedAt: (ids: number[]) => void;
setCurrentVocab: (vocab: IVocab) => void; setCurrentVocab: (vocab: IVocab) => void;
setReview: (state: boolean) => void;
drawerButtonState: (state: boolean) => void; drawerButtonState: (state: boolean) => void;
currentVocab: IVocab; currentVocab: IVocab;
lookedAt: number[]; lookedAt: number[];
toReview: boolean;
}; };
export default class LevelPage extends React.Component<IProps> { const LevelPageWithRouter = withRouter(
class LevelPage extends React.Component<IProps> {
private uid = 0; private uid = 0;
// To prevent React from redrawing the vocabulary list and prematurely // To prevent React from redrawing the vocabulary list and prematurely
// cancelling the animation // cancelling the animation
@ -84,27 +85,17 @@ export default class LevelPage extends React.Component<IProps> {
} }
toReview = () => { toReview = () => {
const { vocab, lookedAt } = this.props; const { vocab, lookedAt, id } = this.props;
// Only go to the review if all vocabulary item have been looked at // Only go to the review if all vocabulary item have been looked at
if (vocab.length === lookedAt.length) { if (vocab.length === lookedAt.length) {
this.props.setReview(true); this.props.setLoading(true);
this.props.history.push(`/review/level/${id}`);
} }
} }
render() { render() {
if (this.props.loading) { if (this.props.loading) {
return <div> return <div>
{/*
* This would be the case when the user presses the "to
* review" button. That is because we need the state of loading
* to be true, when this page gets called
* TODO:?
*/}
{
this.props.toReview ? (
<Redirect to={`/review/level/${this.props.id}`} />
) : undefined
}
<Grid <Grid
container container
spacing={0} spacing={0}
@ -131,7 +122,6 @@ export default class LevelPage extends React.Component<IProps> {
<List> <List>
{this.props.vocab {this.props.vocab
.map(this.renderVocabListItem)} .map(this.renderVocabListItem)}
{/* TODO*/}
<ListItem button onClick={this.toReview}> <ListItem button onClick={this.toReview}>
<ListItemText> <ListItemText>
Zur Übung Zur Übung
@ -139,11 +129,6 @@ export default class LevelPage extends React.Component<IProps> {
</ListItem> </ListItem>
</List> </List>
</Grid> </Grid>
{
this.props.toReview ? (
<Redirect to={`/review/level/${this.props.id}`} />
) : undefined
}
<Grid item lg={7} xs={9}> <Grid item lg={7} xs={9}>
<Grid container direction="column"> <Grid container direction="column">
<Grid item style={{ margin: 12 }}> <Grid item style={{ margin: 12 }}>
@ -197,4 +182,6 @@ export default class LevelPage extends React.Component<IProps> {
</Grid> </Grid>
</div>; </div>;
} }
}; }
);
export default LevelPageWithRouter;

View File

@ -6,34 +6,30 @@ import Grid from "@material-ui/core/Grid";
import Button from "@material-ui/core/Button"; import Button from "@material-ui/core/Button";
import SummaryTable from "../components/SummaryTable"; import SummaryTable from "../components/SummaryTable";
import { Redirect } from "react-router-dom"; import { withRouter } from "react-router-dom";
import { IReviewMetadata } from "../models/review"; import { IReviewMetadata } from "../models/review";
interface IProps { interface IProps {
reviewMeta: () => IReviewMetadata; history: any;
reviewMeta: IReviewMetadata;
setDrawerButton: (state: boolean) => void;
} }
interface IState { // TODO: This stays at the default value
toDashboard: boolean; const SummaryPageWithRouter = withRouter(
} class SummaryPage extends React.Component<IProps> {
toDashboard = () => {
// Show the drawer button
this.props.setDrawerButton(true);
export default class SummaryPage extends React.Component<IProps, IState> { // Go to the dashboard
constructor(props: any) { this.props.history.push("/dashboard");
super(props);
this.state = {
toDashboard: false,
};
} }
render() { render() {
return <div> return <div>
{
this.state.toDashboard ? (
<Redirect to="/dashboard" />
) : undefined
}
<Grid <Grid
container container
spacing={0} spacing={0}
@ -45,10 +41,8 @@ export default class SummaryPage extends React.Component<IProps, IState> {
<Paper className="paper"> <Paper className="paper">
<Typography variant="title">Zusammenfassung</Typography> <Typography variant="title">Zusammenfassung</Typography>
<Grid container direction="column"> <Grid container direction="column">
<SummaryTable reviewMeta={this.props.reviewMeta} /> <SummaryTable reviewMeta={() => this.props.reviewMeta} />
<Button onClick={() => this.setState({ <Button onClick={this.toDashboard}>
toDashboard: true,
})}>
Zum Dashboard Zum Dashboard
</Button> </Button>
</Grid> </Grid>
@ -57,4 +51,6 @@ export default class SummaryPage extends React.Component<IProps, IState> {
</Grid> </Grid>
</div>; </div>;
} }
} }
);
export default SummaryPageWithRouter;

View File

@ -26,7 +26,6 @@ interface IState {
level: { level: {
currentVocab: IVocab; currentVocab: IVocab;
lookedAt: number[]; lookedAt: number[];
toReview: boolean;
vocab: IVocab[]; vocab: IVocab[];
loading: boolean; loading: boolean;
}; };
@ -74,7 +73,6 @@ const initialState: IState = {
level: { level: {
currentVocab: {} as IVocab, currentVocab: {} as IVocab,
lookedAt: [0], lookedAt: [0],
toReview: false,
vocab: [], vocab: [],
loading: true, loading: true,
}, },
@ -137,15 +135,6 @@ export function LateinicusApp(state: IState = initialState, action: any) {
return Object.assign({}, state, { return Object.assign({}, state, {
user: action.user, user: action.user,
}); });
case Actions.LEVEL_SET_REVIEW:
return Object.assign({}, state, {
level: Object.assign({}, state.level, {
toReview: action.state,
// Make sure that we are in a "loading mode", when the page gets
// called the next time
loading: true,
}),
});
case Actions.LEVEL_SET_LOOKEDAT: case Actions.LEVEL_SET_LOOKEDAT:
return Object.assign({}, state, { return Object.assign({}, state, {
level: Object.assign({}, state.level, { level: Object.assign({}, state.level, {