Client: refactor

This commit is contained in:
Sam 2018-05-10 17:21:19 +02:00
parent d90b7fed9d
commit f370de62a6
6 changed files with 20 additions and 88 deletions

View File

@ -24,7 +24,7 @@ test('standard user should be able to add a workout (w/o gpx)', async t => {
await t await t
.navigateTo(`${TEST_URL}/activities/add`) .navigateTo(`${TEST_URL}/activities/add`)
.expect(Selector('H1').withText('Dashboard').exists).notOk() .expect(Selector('H1').withText('Dashboard').exists).notOk()
.expect(Selector('H2').withText('Add a sport').exists).ok() .expect(Selector('H2').withText('Add an activity').exists).ok()
.click(Selector('input[name="withoutGpx"]')) .click(Selector('input[name="withoutGpx"]'))
.click(Selector('select').filter('[name="sport_id"]')) .click(Selector('select').filter('[name="sport_id"]'))
.click(Selector('option').filter('[value="1"]')) .click(Selector('option').filter('[value="1"]'))

View File

@ -1,91 +1,24 @@
import React from 'react' import React from 'react'
import { Helmet } from 'react-helmet'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import FormWithGpx from './ActivityForms/FormWithGpx' import ActivityAddOrEdit from './ActivityAddOrEdit'
import FormWithoutGpx from './ActivityForms/FormWithoutGpx'
import { getData } from '../../actions/index' import { getData } from '../../actions/index'
class AddActivity extends React.Component { class ActivityAdd extends React.Component {
constructor(props, context) {
super(props, context)
this.state = {
withGpx: true,
}
}
componentDidMount() { componentDidMount() {
this.props.loadSports() this.props.loadSports()
} }
handleRadioChange (changeEvent) {
this.setState({
withGpx:
changeEvent.target.name === 'withGpx'
? changeEvent.target.value : !changeEvent.target.value
})
}
render() { render() {
const { message, sports } = this.props const { message, sports } = this.props
const { withGpx } = this.state
return ( return (
<div> <div>
<Helmet> <ActivityAddOrEdit
<title>mpwo - Add an activity</title> activity={null}
</Helmet> message={message}
<br /><br /> sports={sports}
{message && (
<code>{message}</code>
)}
<div className="container">
<div className="row">
<div className="col-md-2" />
<div className="col-md-8">
<div className="card add-activity">
<h2 className="card-header text-center">
Add a sport
</h2>
<div className="card-body">
<form>
<div className="form-group row">
<div className="col">
<label className="radioLabel">
<input
type="radio"
name="withGpx"
checked={withGpx}
onChange={event => this.handleRadioChange(event)}
/> />
with gpx file
</label>
</div>
<div className="col">
<label className="radioLabel">
<input
type="radio"
name="withoutGpx"
checked={!withGpx}
onChange={event => this.handleRadioChange(event)}
/>
without gpx file
</label>
</div>
</div>
</form>
{withGpx ? (
<FormWithGpx sports={sports} />
) : (
<FormWithoutGpx sports={sports} />
)}
</div>
</div>
</div>
<div className="col-md-2" />
</div>
</div>
</div> </div>
) )
} }
@ -102,4 +35,4 @@ export default connect(
dispatch(getData('sports')) dispatch(getData('sports'))
}, },
}) })
)(AddActivity) )(ActivityAdd)

View File

@ -21,14 +21,20 @@ export default class ActivityAddEdit extends React.Component {
} }
render() { render() {
const { activity, sports } = this.props const { activity, message, sports } = this.props
const { withGpx } = this.state const { withGpx } = this.state
return ( return (
<div> <div>
<Helmet> <Helmet>
<title>mpwo - Add an activity</title> <title>mpwo - {activity
? 'Edit an activity'
: 'Add an activity'}
</title>
</Helmet> </Helmet>
<br /><br /> <br /><br />
{message && (
<code>{message}</code>
)}
<div className="container"> <div className="container">
<div className="row"> <div className="row">
<div className="col-md-2" /> <div className="col-md-2" />

View File

@ -1,5 +1,4 @@
import React from 'react' import React from 'react'
import { Helmet } from 'react-helmet'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import ActivityAddOrEdit from './ActivityAddOrEdit' import ActivityAddOrEdit from './ActivityAddOrEdit'
@ -18,16 +17,10 @@ class ActivityEdit extends React.Component {
const [activity] = activities const [activity] = activities
return ( return (
<div> <div>
<Helmet>
<title>mpwo - Edit activity</title>
</Helmet>
<br /><br />
{message && (
<code>{message}</code>
)}
{sports.length > 0 && ( {sports.length > 0 && (
<ActivityAddOrEdit <ActivityAddOrEdit
activity={activity} activity={activity}
message={message}
sports={sports} sports={sports}
/> />
)} )}

View File

@ -5,7 +5,7 @@ export default function ActivityCard (props) {
const { activity, sports } = props const { activity, sports } = props
return ( return (
<div className="card activity-card"> <div className="card activity-card text-center">
<div className="card-header"> <div className="card-header">
<Link to={`/activities/${activity.id}`}> <Link to={`/activities/${activity.id}`}>
{sports.filter(sport => sport.id === activity.sport_id) {sports.filter(sport => sport.id === activity.sport_id)