Change the list of public tournaments to share some redundant code with the list of private tournaments

This commit is contained in:
Felix Hamme 2019-04-28 04:21:01 +02:00
parent dac0c33272
commit 1af7ee413f
1 changed files with 38 additions and 61 deletions

View File

@ -1,21 +1,15 @@
import Head from 'next/head'; import Head from 'next/head';
import React from 'react'; import React from 'react';
import { import {Card, CardBody, Container} from 'reactstrap';
Card,
CardBody,
Container
} from 'reactstrap';
import {TurniereNavigation} from '../js/components/Navigation'; import {TurniereNavigation} from '../js/components/Navigation';
import {Footer} from '../js/components/Footer'; import {Footer} from '../js/components/Footer';
import {
getRequest,
getState
} from '../js/api';
import '../static/everypage.css'; import '../static/everypage.css';
import TournamentList from '../js/components/TournamentList';
import {connect} from 'react-redux';
export default class ListPage extends React.Component { export default class PublicTournamentsPage extends React.Component {
render() { render() {
return ( return (
@ -25,7 +19,7 @@ export default class ListPage extends React.Component {
</Head> </Head>
<TurniereNavigation/> <TurniereNavigation/>
<div> <div>
<TournamentList/> <PublicTournamentPageContent/>
</div> </div>
<Footer/> <Footer/>
</div> </div>
@ -33,55 +27,38 @@ export default class ListPage extends React.Component {
} }
} }
class TournamentList extends React.Component { function mapStateToProperties(state) {
constructor(props) { const {isSignedIn} = state.userinfo;
super(props); return {isSignedIn};
this.state = {
error: null,
isLoaded: false,
items: []
};
} }
componentDidMount() { const PublicTournamentPageContent = connect(
getRequest(getState(), '/tournaments?type=public') mapStateToProperties,
.then( )(PublicTournaments);
response => {
this.setState({ function PublicTournaments(props) {
isLoaded: true, if (props.isSignedIn) {
items: response.data return (<div>
}); <Container className='pt-5'>
}, <PublicTournamentsCard/>
error => { </Container>
this.setState({ <Container className="pb-5 pt-3">
isLoaded: true, <a href='/private' className="btn btn-success shadow">zu den privaten Turnieren</a>
error </Container>
}); </div>);
} else {
return (<Container className='py-5'>
<PublicTournamentsCard/>
</Container>);
} }
);
} }
render() { function PublicTournamentsCard() {
return ( return (<Card className="shadow">
<Container className="py-5">
<Card className="shadow">
<CardBody> <CardBody>
<h1 className="custom-font">Öffentliche Turniere</h1> <h1 className="custom-font">Öffentliche Turniere</h1>
{this.state.items.map(item => ( <TournamentList type='public'/>
//The code should be item.code but the api just supports it this way by now
<TournamentListEntry name={item.name} code={item.id} key={item.id}/>
))}
</CardBody> </CardBody>
</Card> </Card>);
</Container>
);
}
}
function TournamentListEntry(props) {
return (
<a className="w-100 d-inline-block mt-2 text-left btn btn-outline-primary" href={ '/t/' + props.code }>
{props.name}
</a>
);
} }