diff options
| author | mo khan <mo@mokhan.ca> | 2016-12-31 12:54:05 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2016-12-31 12:54:05 -0700 |
| commit | 17326cf4bff2b18938e18bb32944bff5c28f97e5 (patch) | |
| tree | 8cf89e50de24c40fbfe2903e254c0b9a49dbf2c7 | |
| parent | 051e94ac2499d946720ee656908536f35d065164 (diff) | |
create command to save new workout.
| -rw-r--r-- | app/screens/new-workout-screen.js | 17 | ||||
| -rw-r--r-- | app/services/commands/create-workout-command.js | 26 | ||||
| -rw-r--r-- | app/services/commands/index.js | 2 | ||||
| -rw-r--r-- | app/services/events.js | 1 |
4 files changed, 45 insertions, 1 deletions
diff --git a/app/screens/new-workout-screen.js b/app/screens/new-workout-screen.js index 3b97222..141f447 100644 --- a/app/screens/new-workout-screen.js +++ b/app/screens/new-workout-screen.js @@ -66,11 +66,26 @@ export default class NewWorkoutScreen extends Screen { } onPress(exercise) { - console.log(`pressed ${exercise.name}`) + console.log(`pressed ${exercise.name}`); } onBeginWorkout() { console.log("BEGIN WORKOUT"); + const sets = this.state.sets.map((set) => { + return { + exercise_id: set.exercise_id, + target_repetitions: set.target_repetitions, + target_weight: set.target_weight, + type: set.type, + }; + }); + + this.publish({ + body_weight: this.state.body_weight, + event: events.CREATE_WORKOUT, + routine_id: this.state.routine.id, + sets: sets, + }); } onLoadWorkout() { diff --git a/app/services/commands/create-workout-command.js b/app/services/commands/create-workout-command.js new file mode 100644 index 0000000..5620518 --- /dev/null +++ b/app/services/commands/create-workout-command.js @@ -0,0 +1,26 @@ +import * as events from '../events'; + +export default class CreateWorkoutCommand { + constructor(eventAggregator, api) { + this.eventAggregator = eventAggregator; + this.api = api; + } + + subscribeTo(eventAggregator) { + eventAggregator.subscribe(events.CREATE_WORKOUT, this); + } + + notify(event) { + const body = { + body_weight: event.body_weight, + exercise_sets_attributes: event.sets, + routine_id: event.routine_id, + }; + this.api.post('/workouts', body, this.onResponse.bind(this)); + } + + onResponse(json) { + console.log("CREATED"); + console.log(json); + } +} diff --git a/app/services/commands/index.js b/app/services/commands/index.js index 9560f09..96aae44 100644 --- a/app/services/commands/index.js +++ b/app/services/commands/index.js @@ -1,7 +1,9 @@ +import CreateWorkoutCommand from './create-workout-command'; import LoginCommand from './login-command'; import LogoutCommand from './logout-command'; export { + CreateWorkoutCommand, LoginCommand, LogoutCommand, }; diff --git a/app/services/events.js b/app/services/events.js index b5df5fc..7bd2a60 100644 --- a/app/services/events.js +++ b/app/services/events.js @@ -5,3 +5,4 @@ export const FETCH_WORKOUTS = 'FETCH_WORKOUTS'; export const FETCHED_WORKOUTS = 'FETCHED_WORKOUTS'; export const FETCH_NEW_WORKOUT = 'FETCH_NEW_WORKOUT'; export const FETCHED_NEW_WORKOUT = 'FETCHED_NEW_WORKOUT'; +export const CREATE_WORKOUT = 'CREATE_WORKOUT'; |
