From c553a5a8d37c5f83e2b9afff75c78832dc1869ac Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 1 May 2018 19:29:21 +0200 Subject: [PATCH] Api & Client: add an activity --- mpwo_api/mpwo_api/activities/activities.py | 1 + mpwo_client/src/actions/activities.js | 19 +++ .../src/components/Activities/AddActivity.jsx | 114 ++++++++++++++++++ mpwo_client/src/components/App.css | 4 + mpwo_client/src/components/App.jsx | 2 + mpwo_client/src/components/NavBar/index.jsx | 12 ++ mpwo_client/src/mwpoApi/activities.js | 18 +++ 7 files changed, 170 insertions(+) create mode 100644 mpwo_client/src/actions/activities.js create mode 100644 mpwo_client/src/components/Activities/AddActivity.jsx create mode 100644 mpwo_client/src/mwpoApi/activities.js diff --git a/mpwo_api/mpwo_api/activities/activities.py b/mpwo_api/mpwo_api/activities/activities.py index 95eb3d7e..295d9b4d 100644 --- a/mpwo_api/mpwo_api/activities/activities.py +++ b/mpwo_api/mpwo_api/activities/activities.py @@ -95,6 +95,7 @@ def post_activity(auth_user_id): new_activity.ascent = gpx_data['uphill'] new_activity.max_speed = gpx_data['max_speed'] new_activity.ave_speed = gpx_data['average_speed'] + db.session.add(new_activity) db.session.commit() response_object = { diff --git a/mpwo_client/src/actions/activities.js b/mpwo_client/src/actions/activities.js new file mode 100644 index 00000000..e735a0c6 --- /dev/null +++ b/mpwo_client/src/actions/activities.js @@ -0,0 +1,19 @@ +import mpwoApi from '../mwpoApi/activities' +import { history } from '../index' +import { setError } from './index' + + +export function addActivity(form) { + return function(dispatch) { + return mpwoApi + .addActivity(form) + .then(ret => { + if (ret.status === 'created') { + history.push('/') + } else { + dispatch(setError(`activities: ${ret.message}`)) + } + }) + .catch(error => dispatch(setError(`activities: ${error}`))) + } +} diff --git a/mpwo_client/src/components/Activities/AddActivity.jsx b/mpwo_client/src/components/Activities/AddActivity.jsx new file mode 100644 index 00000000..91f65f41 --- /dev/null +++ b/mpwo_client/src/components/Activities/AddActivity.jsx @@ -0,0 +1,114 @@ +import React from 'react' +import { Helmet } from 'react-helmet' +import { connect } from 'react-redux' + +import { addActivity } from '../../actions/activities' +import { getData } from '../../actions/index' +import { history } from '../../index' + + +class AddActivity extends React.Component { + componentDidMount() { + this.props.loadSports() + } + + render() { + const { message, onAddSport, sports } = this.props + + return ( +
+ + mpwo - Add an activity + +

+ {message && ( + {message} + )} + +
+
+
+
+
+

+ Add a sport +

+
+
event.preventDefault()} + > +
+ +
+
+ +
+ onAddSport(event)} + value="Submit" + /> + history.go(-1)} + value="Cancel" + /> +
+
+
+
+
+
+
+
+ ) + } +} + +export default connect( + state => ({ + message: state.message, + sports: state.sports.data, + user: state.user, + }), + dispatch => ({ + loadSports: () => { + dispatch(getData('sports')) + }, + onAddSport: event => { + const form = new FormData() + form.append('file', event.target.form.gpxFile.files[0]) + form.append( + 'data', `{"sport_id": ${event.target.form.sport.value}}` + ) + dispatch(addActivity(form)) + }, + }) +)(AddActivity) diff --git a/mpwo_client/src/components/App.css b/mpwo_client/src/components/App.css index 1e8feaf5..2e7394d6 100644 --- a/mpwo_client/src/components/App.css +++ b/mpwo_client/src/components/App.css @@ -56,3 +56,7 @@ input, textarea { margin: 1em; text-align: center; } + +.add-activity { + margin-top: 50px; +} diff --git a/mpwo_client/src/components/App.jsx b/mpwo_client/src/components/App.jsx index 8d61d530..b7ba8292 100644 --- a/mpwo_client/src/components/App.jsx +++ b/mpwo_client/src/components/App.jsx @@ -3,6 +3,7 @@ import { Redirect, Route, Switch } from 'react-router-dom' import './App.css' import Admin from './Admin' +import AddWorkout from './Activities/AddActivity' import Dashboard from './Dashboard' import Logout from './User/Logout' import NavBar from './NavBar' @@ -83,6 +84,7 @@ export default class App extends React.Component { ) )} /> + diff --git a/mpwo_client/src/components/NavBar/index.jsx b/mpwo_client/src/components/NavBar/index.jsx index cee1f927..bc662422 100644 --- a/mpwo_client/src/components/NavBar/index.jsx +++ b/mpwo_client/src/components/NavBar/index.jsx @@ -33,6 +33,18 @@ function NavBar (props) { Dashboard + {props.user.isAuthenticated && ( +
  • + + Add a workout + +
  • + )} {props.user.isAdmin && (
  • response.json()) + .catch(error => error) + } + +}