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({
isLoaded: true,
items: response.data
});
},
error => {
this.setState({
isLoaded: true,
error
});
}
);
}
render() { function PublicTournaments(props) {
return ( if (props.isSignedIn) {
<Container className="py-5"> return (<div>
<Card className="shadow"> <Container className='pt-5'>
<CardBody> <PublicTournamentsCard/>
<h1 className="custom-font">Öffentliche Turniere</h1>
{this.state.items.map(item => (
//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>
</Card>
</Container> </Container>
); <Container className="pb-5 pt-3">
<a href='/private' className="btn btn-success shadow">zu den privaten Turnieren</a>
</Container>
</div>);
} else {
return (<Container className='py-5'>
<PublicTournamentsCard/>
</Container>);
} }
} }
function TournamentListEntry(props) { function PublicTournamentsCard() {
return ( return (<Card className="shadow">
<a className="w-100 d-inline-block mt-2 text-left btn btn-outline-primary" href={ '/t/' + props.code }> <CardBody>
{props.name} <h1 className="custom-font">Öffentliche Turniere</h1>
</a> <TournamentList type='public'/>
); </CardBody>
</Card>);
} }