Commit 5101c6c

mo khan <mo@mokhan.ca>
2016-11-26 04:57:47
add react-native-config and use it to connect to api host.
1 parent b06b73d
android/app/src/main/java/com/awesomeproject/MainApplication.java
@@ -8,6 +8,7 @@ import com.facebook.react.ReactInstanceManager;
 import com.facebook.react.ReactNativeHost;
 import com.facebook.react.ReactPackage;
 import com.facebook.react.shell.MainReactPackage;
+import com.lugg.ReactNativeConfig.ReactNativeConfigPackage;
 
 import java.util.Arrays;
 import java.util.List;
@@ -23,7 +24,8 @@ public class MainApplication extends Application implements ReactApplication {
     @Override
     protected List<ReactPackage> getPackages() {
       return Arrays.<ReactPackage>asList(
-          new MainReactPackage()
+          new MainReactPackage(),
+          new ReactNativeConfigPackage()
       );
     }
   };
android/app/build.gradle
@@ -1,4 +1,5 @@
 apply plugin: "com.android.application"
+apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
 
 import com.android.build.OutputFile
 
@@ -129,6 +130,7 @@ dependencies {
     compile fileTree(dir: "libs", include: ["*.jar"])
     compile "com.android.support:appcompat-v7:23.0.1"
     compile "com.facebook.react:react-native:+"  // From node_modules
+    compile project(':react-native-config')
 }
 
 // Run this once to be able to run the application with BUCK
android/settings.gradle
@@ -1,3 +1,6 @@
 rootProject.name = 'AwesomeProject'
 
+include ':react-native-config'
 include ':app'
+
+project(':react-native-config').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-config/android')
app/screens/login-screen.js
@@ -2,6 +2,8 @@ import React, { Component } from 'react';
 import { View, Text, TouchableHighlight, AsyncStorage } from 'react-native';
 import Movies from '../components/movies';
 import Account from '../domain/account'
+import Api from '../services/api'
+
 var t = require('tcomb-form-native');
 var Form = t.form.Form;
 
@@ -43,23 +45,13 @@ export default class LoginScreen extends Component {
 
   onLogin() {
     let value = this.refs.form.getValue();
-    let that = this;
     if (value) {
-      fetch("http://localhost:3000/sessions/create", {
-        method: "POST",
-        headers: {
-          'Accept': 'application/json',
-          'Content-Type': 'application/json'
-        },
-        body: JSON.stringify({
-          username: value.username,
-          password: value.password,
-        })
-      })
-      .then((response) => response.json())
-      .then((json) => {
+      body = { username: value.username, password: value.password };
+      let that = this;
+      new Api('/api/sessions').post(body, (json) => {
+        console.log(json);
         that.storeToken("authentication_token", json.token)
-      }).done();
+      });
     }
   }
 
app/services/api.js
@@ -1,13 +1,32 @@
+import Config from 'react-native-config';
+
 export default class Api {
   constructor(url) {
-    this.url = url;
+    this.url = `${Config.API_HOST}${url}`
+    console.log(this.url);
   }
 
   get(success) {
     fetch(this.url)
       .then((response) => response.json())
       .then(success)
+      .catch((error) => console.error(error))
       .done();
   }
+
+  post(body, success) {
+    fetch(this.url, {
+      method: "POST",
+      headers: {
+        'Accept': 'application/json',
+        'Content-Type': 'application/json'
+      },
+      body: JSON.stringify(body)
+    })
+    .then((response) => response.json())
+    .then(success)
+    .catch((error) => console.error(error))
+    .done();
+  }
 }
 
.env.example
@@ -0,0 +1,1 @@
+API_HOST=https://www.stronglifters.com
.gitignore
@@ -39,3 +39,5 @@ buck-out/
 \.buckd/
 android/app/libs
 android/keystores/debug.keystore
+
+.env
package.json
@@ -9,6 +9,7 @@
   "dependencies": {
     "react": "15.3.2",
     "react-native": "0.37.0",
+    "react-native-config": "^0.1.2",
     "tcomb-form-native": "^0.6.1"
   },
   "jest": {
yarn.lock
@@ -2251,7 +2251,7 @@ http-signature@~1.1.0:
     jsprim "^1.2.2"
     sshpk "^1.7.0"
 
-iconv-lite@0.4.11:
+iconv-lite@0.4.11, iconv-lite@^0.4.5:
   version "0.4.11"
   resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.11.tgz#2ecb42fd294744922209a2e7c404dac8793d8ade"
 
@@ -2259,7 +2259,7 @@ iconv-lite@0.4.13:
   version "0.4.13"
   resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.13.tgz#1f88aba4ab0b1508e8312acc39345f36e992e2f2"
 
-iconv-lite@^0.4.13, iconv-lite@^0.4.5, iconv-lite@~0.4.13:
+iconv-lite@^0.4.13, iconv-lite@~0.4.13:
   version "0.4.15"
   resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.15.tgz#fe265a218ac6a57cfe854927e9d04c19825eddeb"
 
@@ -3286,13 +3286,13 @@ mime-db@~1.23.0:
   version "1.23.0"
   resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.23.0.tgz#a31b4070adaea27d732ea333740a64d0ec9a6659"
 
-mime-types@2.1.11:
+mime-types@2.1.11, mime-types@~2.1.9:
   version "2.1.11"
   resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.11.tgz#c259c471bda808a85d6cd193b430a5fae4473b3c"
   dependencies:
     mime-db "~1.23.0"
 
-mime-types@^2.1.12, mime-types@~2.1.11, mime-types@~2.1.13, mime-types@~2.1.6, mime-types@~2.1.7, mime-types@~2.1.9:
+mime-types@^2.1.12, mime-types@~2.1.11, mime-types@~2.1.13, mime-types@~2.1.6, mime-types@~2.1.7:
   version "2.1.13"
   resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.13.tgz#e07aaa9c6c6b9a7ca3012c69003ad25a39e92a88"
   dependencies:
@@ -3797,6 +3797,10 @@ react-deep-force-update@^1.0.0:
   version "1.0.1"
   resolved "https://registry.yarnpkg.com/react-deep-force-update/-/react-deep-force-update-1.0.1.tgz#f911b5be1d2a6fe387507dd6e9a767aa2924b4c7"
 
+react-native-config@^0.1.2:
+  version "0.1.2"
+  resolved "https://registry.yarnpkg.com/react-native-config/-/react-native-config-0.1.2.tgz#cf6e3e312845f411fe9ef42f3ea283c576078681"
+
 react-native@0.37.0:
   version "0.37.0"
   resolved "https://registry.yarnpkg.com/react-native/-/react-native-0.37.0.tgz#d871c752b4d87e46b569fd8be2921d4f9248c87d"