Admin: sports creation

This commit is contained in:
Sam 2018-05-01 12:40:30 +02:00
parent fd8b51f416
commit be6e64e124
3 changed files with 87 additions and 4 deletions

View File

@ -33,14 +33,11 @@ export function getData(target, id = null) {
export function addData(target, data) {
return function(dispatch) {
if (isNaN(data.id)) {
return dispatch(setError(target, `${target}: Incorrect id`))
}
return mpwoApi
.addData(target, data)
.then(ret => {
if (ret.status === 'created') {
dispatch(setData(target, ret.data))
history.push(`/admin/${target}`)
} else {
dispatch(setError(`${target}: ${ret.status}`))
}

View File

@ -0,0 +1,81 @@
import React from 'react'
import { Helmet } from 'react-helmet'
import { connect } from 'react-redux'
import { addData } from '../../../actions/index'
class AdminSportsAdd extends React.Component {
componentDidMount() { }
render() {
const { message, onAddSport } = this.props
return (
<div>
<Helmet>
<title>mpwo - Admin</title>
</Helmet>
<h1 className="page-title">
Administration - Sport
</h1>
{message && (
<code>{message}</code>
)}
<div className="container">
<div className="row">
<div className="col-md-2" />
<div className="col-md-8">
<div className="card">
<div className="card-header">
Add a sport
</div>
<div className="card-body">
<form onSubmit={event =>
event.preventDefault()}
>
<div className="form-group">
<label>
Label:
<input
name="label"
className="form-control input-lg"
type="text"
/>
</label>
</div>
<input
type="submit"
className="btn btn-primary btn-lg btn-block"
onClick={event => onAddSport(event)}
value="Submit"
/>
<input
type="submit"
className="btn btn-secondary btn-lg btn-block"
value="Cancel"
/>
</form>
</div>
</div>
</div>
<div className="col-md-2" />
</div>
</div>
</div>
)
}
}
export default connect(
state => ({
message: state.message,
user: state.user,
}),
dispatch => ({
onAddSport: e => {
const data = { label: e.target.form.label.value }
dispatch(addData('sports', data))
},
})
)(AdminSportsAdd)

View File

@ -6,6 +6,7 @@ import { Redirect, Route, Switch } from 'react-router-dom'
import AdminMenu from './Sports/AdminMenu'
import AdminSport from './Sports/AdminSport'
import AdminSports from './Sports/AdminSports'
import AdminSportsAdd from './Sports/AdminSportsAdd'
import AccessDenied from './../Others/AccessDenied'
import NotFound from './../Others/NotFound'
import { isLoggedIn } from '../../utils'
@ -24,6 +25,10 @@ class Admin extends React.Component {
<Switch>
<Route exact path="/admin" component={AdminMenu} />
<Route exact path="/admin/sports" component={AdminSports} />
<Route
exact path="/admin/sports/add"
component={AdminSportsAdd}
/>
<Route path="/admin/sport" component={AdminSport} />
<Route component={NotFound} />
</Switch>