Commit d45c560

mo khan <mo@mokhan.ca>
2016-12-04 05:41:35
attempt to load workouts.
1 parent 9e602cf
app/components/application-component.js
@@ -2,6 +2,10 @@ import React, { Component } from 'react';
 
 export default class ApplicationComponent extends Component {
   componentDidMount() {
+    if (this.state.eventsOfInterest == undefined) {
+      return;
+    }
+
     this.state.eventsOfInterest.forEach((event) => {
       this.props.eventAggregator.subscribe(event, this);
     });
app/components/application-shell.js
@@ -4,6 +4,7 @@ import LoginScreen from '../screens/login-screen'
 import Router from '../infrastructure/router'
 import EventAggregator from '../infrastructure/event-aggregator';
 import LoginCommand from '../commands/login-command';
+import FetchWorkouts from '../queries/fetch-workouts';
 
 export default class ApplicationShell extends Component {
   constructor(props) {
@@ -26,5 +27,8 @@ export default class ApplicationShell extends Component {
   registerCommands(eventAggregator) {
     let command = new LoginCommand(eventAggregator)
     eventAggregator.subscribe("LOGIN", command);
+
+    let query = new FetchWorkouts(eventAggregator)
+    eventAggregator.subscribe("FETCH_WORKOUTS", query);
   }
 }
app/queries/fetch-workouts.js
@@ -0,0 +1,19 @@
+import Api from '../infrastructure/api';
+
+export default class FetchWorkouts {
+  constructor(eventAggregator, api = new Api('/workouts')) {
+    this.eventAggregator = eventAggregator;
+    this.api = api;
+  }
+
+  notify(event) {
+    this.api.get(this.onResponse.bind(this));
+  }
+
+  onResponse(json) {
+    this.eventAggregator.publish({
+      event: 'FETCHED_WORKOUTS',
+      workouts: json.workouts
+    });
+  }
+}
app/screens/dashboard-screen.js
@@ -1,20 +1,47 @@
 import React, { Component } from 'react';
-import { View, Text, Button } from 'react-native';
+import { View, Text, Button, ListView } from 'react-native';
 import ApplicationStorage from '../infrastructure/application-storage';
+import ApplicationComponent from '../components/application-component';
+
+export default class DashboardScreen extends ApplicationComponent {
+  constructor(props) {
+    super(props)
+    this.ds = new ListView.DataSource({ rowHasChanged: (row, other) => row['title'] != other['title'] });
+    this.state = {
+      dataSource: this.mapAll([]),
+      eventsOfInterest: ['FETCHED_WORKOUTS'],
+    };
+  }
 
-export default class DashboardScreen extends Component {
   render() {
     console.log("LOADING DASHBOARD");
     return (
       <View>
-        <Text>Welcome back !</Text>
+        <Text>Welcome back {this.props.username}!</Text>
+        <Button onPress={this.loadWorkouts.bind(this)} title="Reload" />
         <Button onPress={this.onHistory.bind(this)} title="History" />
         <Button onPress={this.onStartWorkout.bind(this)} title="Start Workout" />
+        <ListView
+          dataSource={this.state.dataSource}
+          renderRow={(row) => <Movie name={row} />}
+          />
         <Button onPress={this.onLogout.bind(this)} title="Logout" />
       </View>
     );
   }
 
+  loadWorkouts() {
+    this.publish({event: 'FETCH_WORKOUTS'});
+  }
+
+  notify(event) {
+    console.dir(event);
+    switch(event.event) {
+      case "FETCHED_WORKOUTS":
+        that.setState({ dataSource: that.mapAll(json.workouts) });
+    }
+  }
+
   onHistory() {
     console.log("load previous workouts");
   }
@@ -30,4 +57,9 @@ export default class DashboardScreen extends Component {
     storage.delete('username');
     this.props.navigator.pop();
   }
+
+  mapAll(workouts) {
+    workouts.forEach((item) => console.log(item))
+    return this.ds.cloneWithRows(workouts.map((item) => item.title));
+  }
 }
app/screens/login-screen.js
@@ -12,7 +12,7 @@ export default class LoginScreen extends ApplicationComponent {
   constructor(props) {
     super(props)
     this.state = {
-      account: { username: 'mokha', password: '' },
+      account: { username: 'mokha', password: 'password' },
       eventsOfInterest: ['LOGGED_IN'],
     };
   }
@@ -55,8 +55,9 @@ export default class LoginScreen extends ApplicationComponent {
   }
 
   notify(event) {
+    console.dir(event);
     this.props.navigator.push({
-      component: DashboardScreen, params: { username: username }
+      component: DashboardScreen, params: { username: event.username }
     });
   }
 }