Commit 9f200ea

mo khan <mo@mokhan.ca>
2016-12-04 21:02:47
load workouts from api.
1 parent d45c560
app/commands/login-command.js
@@ -1,9 +1,11 @@
 import Api from '../infrastructure/api';
+import ApplicationStorage from '../infrastructure/application-storage';
 
 export default class LoginCommand {
-  constructor(eventAggregator, api = new Api('/sessions')) {
+  constructor(eventAggregator, api = new Api('/sessions'), storage = new ApplicationStorage()) {
     this.eventAggregator = eventAggregator;
     this.api = api;
+    this.storage = storage;
   }
 
   notify(event) {
@@ -12,6 +14,7 @@ export default class LoginCommand {
   }
 
   onResponse(json) {
+    this.storage.save('authentication_token', json.authentication_token);
     this.eventAggregator.publish({
       event: 'LOGGED_IN',
       username: json.username,
app/infrastructure/__tests__/application-storage_spec.js
@@ -0,0 +1,19 @@
+import ApplicationStorage from '../application-storage';
+
+describe("ApplicationStorage", () => {
+  let subject = null;
+
+  beforeEach(() => {
+    subject = new ApplicationStorage();
+  });
+
+  describe("#fetch", () => {
+    it("can fetch a saved value", () => {
+      const username = "mokha";
+      subject.save("username", username)
+      expect(
+        subject.fetch("username")
+      ).toEqual(username)
+    });
+  });
+});
app/infrastructure/api.js
@@ -2,45 +2,50 @@ import Config from 'react-native-config';
 import ApplicationStorage from './application-storage';
 
 export default class Api {
-  constructor(url) {
+  constructor(url, storage = new ApplicationStorage()) {
     if (url.startsWith('http')) {
       this.url = url;
     } else {
       this.url = `${Config.API_HOST}/api${url}`
     }
-    this.token = new ApplicationStorage().fetch('authentication_token');
-    console.log(this.url);
+    this.storage = storage;
   }
 
   get(success) {
-    fetch(this.url, {
-      method: 'GET',
-      headers: this.defaultHeaders(),
-    })
-    .then((response) => response.json())
-    .then(success)
-    .catch((error) => console.error(error))
-    .done();
+    this.defaultHeaders((headers) => {
+      fetch(this.url, { method: 'GET', headers: headers })
+        .then((response) => response.json())
+        .then(success)
+        .catch((error) => console.error(error))
+        .done();
+    });
   }
 
   post(body, success) {
     fetch(this.url, {
       method: "POST",
-      headers: this.defaultHeaders(),
+      headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
       body: JSON.stringify(body)
     })
-    .then((response) => response.json())
-    .then(success)
-    .catch((error) => console.error(error))
-    .done();
+      .then((response) => response.json())
+      .then(success)
+      .catch((error) => console.error(error))
+      .done();
   }
 
-  defaultHeaders() {
-    return {
-      'Accept': 'application/json',
-      'Content-Type': 'application/json',
-      'Authorization': `Bearer ${this.token}`,
-    }
+  defaultHeaders(success) {
+    this.storage
+      .fetch('authentication_token')
+      .then((token) => {
+        console.dir(token);
+        success({
+          'Accept': 'application/json',
+          'Content-Type': 'application/json',
+          'Authorization': `Bearer ${token}`,
+        });
+      })
+      .catch((error) => console.error(error))
+      .done();
   }
 }
 
app/infrastructure/application-storage.js
@@ -1,17 +1,14 @@
-import { View, Text, TouchableHighlight, AsyncStorage } from 'react-native';
+import { AsyncStorage } from 'react-native';
 
 export default class ApplicationStorage {
   fetch(key) {
-    this.safelyRun(() => {
-      const value = AsyncStorage.getItem(key);
-      console.log(`found ${key} ${value}`);
-      return value;
+    return this.safelyRun(() => {
+      return AsyncStorage.getItem(key);
     });
   }
 
-  save(key, value) {
+  async save(key, value) {
     this.safelyRun(() => {
-      console.log(`storing ${key} ${value}`);
       AsyncStorage.setItem(key, value);
     });
   }
app/queries/fetch-workouts.js
@@ -11,9 +11,10 @@ export default class FetchWorkouts {
   }
 
   onResponse(json) {
+    console.dir(json)
     this.eventAggregator.publish({
       event: 'FETCHED_WORKOUTS',
-      workouts: json.workouts
+      workouts: json.workouts || []
     });
   }
 }
app/screens/dashboard-screen.js
@@ -38,7 +38,7 @@ export default class DashboardScreen extends ApplicationComponent {
     console.dir(event);
     switch(event.event) {
       case "FETCHED_WORKOUTS":
-        that.setState({ dataSource: that.mapAll(json.workouts) });
+        this.setState({ dataSource: this.mapAll(event.workouts) });
     }
   }