Commit 43dbbc1

mo khan <mo@mokhan.ca>
2016-12-03 17:42:19
connect the login command.
1 parent 4960c96
Changed files (4)
app/commands/__tests__/login-command_spec.js
@@ -0,0 +1,17 @@
+import LoginCommand from '../login-command';
+
+describe("LoginCommand", () => {
+  let subject = null;
+
+  beforeEach(() => {
+    subject = new LoginCommand();
+  });
+
+  describe("#notify", () => {
+    it ("does something", () => {
+      subject.notify({
+        event: 'LOGIN',
+      });
+    });
+  });
+});
app/commands/login-command.js
@@ -0,0 +1,22 @@
+import Api from '../infrastructure/api';
+
+export default class LoginCommand {
+  constructor(eventAggregator, api = new Api('/sessions')) {
+    this.eventAggregator = eventAggregator;
+    this.api = api;
+  }
+
+  notify(event) {
+    let body = { username: event.username, password: event.password };
+    this.api.post(body, this.onResponse.bind(this));
+  }
+
+  onResponse(json) {
+    let logged_in_event = {
+      event: 'LOGGED_IN',
+      username: json.username,
+      authentication_token: json.authentication_token,
+    };
+    this.eventAggregator.publish(logged_in_event);
+  }
+}
app/components/application-shell.js
@@ -3,12 +3,15 @@ import { AppRegistry, Text, ListView, View, Image, TextInput, Navigator } from '
 import LoginScreen from '../screens/login-screen'
 import Router from '../infrastructure/router'
 import EventAggregator from '../infrastructure/event-aggregator';
+import LoginCommand from '../commands/login-command';
 
 export default class ApplicationShell extends Component {
   constructor(props) {
     super(props);
     const eventAggregator = new EventAggregator();
     this.router = new Router({eventAggregator});
+
+    this.registerCommands(eventAggregator);
   }
 
   render() {
@@ -20,4 +23,9 @@ export default class ApplicationShell extends Component {
       />
     );
   }
+
+  registerCommands(eventAggregator) {
+    let command = new LoginCommand(eventAggregator)
+    eventAggregator.subscribe("LOGIN", command);
+  }
 }
app/screens/login-screen.js
@@ -59,6 +59,12 @@ export default class LoginScreen extends ApplicationComponent {
       component: DashboardScreen, params: { username: username }
     });
   }
+
+  notify(event) {
+    console.log("IN LOGIN SCREEN");
+    console.log(event);
+    this.openDashboard(event.username);
+  }
 }
 
 const styles = StyleSheet.create({