From 7e8674af69ae8cd4750f9c1f8c79afed580f17a0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Taneli=20Riihim=C3=A4ki?= <m3034@student.jamk.fi>
Date: Thu, 4 Jul 2019 13:58:18 +0300
Subject: [PATCH] Working on tracking, still WIP

---
 components/GameList.js                   | 107 +++++++++++++++++++++--
 components/Login.js                      |   2 +-
 components/LoginForm.js                  |   2 +-
 components/RegisterForm.js               |   4 +-
 ios/ReactNativeTest-tvOSTests/Info.plist |   5 ++
 5 files changed, 110 insertions(+), 10 deletions(-)

diff --git a/components/GameList.js b/components/GameList.js
index ccdf375..84c4eb6 100644
--- a/components/GameList.js
+++ b/components/GameList.js
@@ -7,10 +7,18 @@ class GameList extends Component {
   constructor(props) {
     super(props);
     this.state = {
+      factions: [],
       games: [],
       tracking: false,
       selectedGame: null,
-      editForm: false
+      selectedFaction: null,
+      editForm: false,
+      userLocation: {
+        latitude: null,
+        longitude: null,
+        latitudeDelta: null,
+        longitudeDelta: null
+      }
     };
 
     intervalID = 0; // Used for starting and stopping the tracking ticks
@@ -22,7 +30,7 @@ class GameList extends Component {
 
   // Get all the games from the server and set the state to the first game
   getGames() {
-    fetch("http://172.20.2.110:5000/game/listgames")
+    fetch("https://tacs-testing.cf:8443/game/listgames")
       .then(response => response.json())
       .then(games => {
         this.setState({
@@ -34,6 +42,28 @@ class GameList extends Component {
         console.log(error);
       });
   }
+  getFactions() {
+    fetch(`https://tacs-testing.cf:8443/game/${this.state.selectedGame.id}`),
+      {
+        method: "GET",
+        headers: {
+          Accept: "application/json",
+          "Content-Type": "application/json"
+        }
+      }
+        .then(response => response.json())
+        .then(factions => {
+          console.log(factions);
+          // this.setState({
+          //   factions,
+          //   selectedFaction: factions[0]
+          // });
+        })
+        .catch(error => {
+          console.log(error);
+        });
+  }
+  joinFaction() {}
   // Toggles the tracking boolean
   handleTrackingChange() {
     this.setState({ tracking: !this.state.tracking });
@@ -46,6 +76,28 @@ class GameList extends Component {
       console.log(
         "Nyt träkätää nim birusti peliä: " + this.state.selectedGame.name
       );
+      fetch(
+        `https:/tacs-testing.cf:8443/tracking/location/${
+          this.state.selectedGame.id
+        }`,
+        {
+          method: "POST",
+          headers: {
+            Accept: "application/json",
+            "Content-Type": "application/json"
+          },
+          body: JSON.stringify({})
+        }
+      )
+        .then(res => res.json())
+        .then(
+          result => {
+            console.log(result);
+          },
+          error => {
+            console.log(error);
+          }
+        );
       console.log("Tracking: " + this.state.tracking);
     }, interval);
   }
@@ -64,6 +116,40 @@ class GameList extends Component {
       this.stopTracking();
     }
   }
+  // When you select a game with the picker, clears the tracking interval and gets the factions of the game
+  selectGame = (selectedGame, itemIndex) => {
+    this.getFactions();
+    clearInterval(this.intervalID);
+    this.setState({ selectedGame, tracking: false });
+  };
+  // When you select a faction, it asks you for a password, which is then sent to the server
+  selectFaction = (selectedFaction, itemIndex) => {
+    this.joinFaction();
+    clearInterval(this.intervalID);
+    this.setState({ selectedFaction, tracking: false });
+  };
+  //Getting the user location and storing it in the state
+  getCurrentLocation = () => {
+    const options = {
+      enableHighAccuracy: true,
+      timeout: 30000,
+      maximumAge: 0
+    };
+    navigator.geolocation.getCurrentPosition(
+      position => {
+        this.setState({
+          userLocation: {
+            latitude: position.coords.latitude,
+            longitude: position.coords.longitude,
+            latitudeDelta: 0.0522,
+            longitudeDelta: 0.0421
+          }
+        });
+      },
+      err => console.log(err),
+      options
+    );
+  };
 
   // Stop tracking on Unmount
   componentWillUnmount() {
@@ -76,15 +162,24 @@ class GameList extends Component {
           <Picker
             style={styles.picker}
             selectedValue={this.state.selectedGame}
-            onValueChange={(selectedGame, itemIndex) => {
-              clearInterval(this.intervalID);
-              this.setState({ selectedGame, tracking: false });
-            }}
+            onValueChange={this.selectGame}
           >
             {this.state.games.map(game => (
               <Picker.Item label={game.name} value={game} />
             ))}
           </Picker>
+          {this.state.selectedGame && (
+            <Picker
+              style={styles.picker}
+              selectedValue={this.state.selectedFaction}
+              onValueChange={this.selectFaction}
+            >
+              {this.state.factions.map(faction => (
+                <Picker.Item label={faction.name} value={faction} />
+              ))}
+            </Picker>
+          )}
+
           <LocationTracker
             tracking={this.state.tracking}
             handleTrackingChange={this.handleTrackingChange.bind(this)}
diff --git a/components/Login.js b/components/Login.js
index 0fde4d5..1f4a53a 100644
--- a/components/Login.js
+++ b/components/Login.js
@@ -40,7 +40,7 @@ class Login extends Component {
   componentDidMount() {
     let token = this.loadToken();
     if (token) {
-      fetch(`http://172.20.2.110:5000/user/verify`, {
+      fetch("https:/tacs-testing.cf:8443/user/verify", {
         headers: {
           Authorization: "Bearer " + token
         }
diff --git a/components/LoginForm.js b/components/LoginForm.js
index a0d9011..e8129d2 100644
--- a/components/LoginForm.js
+++ b/components/LoginForm.js
@@ -27,7 +27,7 @@ export class LoginForm extends React.Component {
     e.preventDefault();
 
     // Send login info to the server
-    fetch(`http://172.20.2.110:5000/user/login`, {
+    fetch("https:/tacs-testing.cf:8443/user/login", {
       method: "POST",
       headers: {
         Accept: "application/json",
diff --git a/components/RegisterForm.js b/components/RegisterForm.js
index 0222436..f9b0fb1 100644
--- a/components/RegisterForm.js
+++ b/components/RegisterForm.js
@@ -13,7 +13,7 @@ export class RegisterForm extends React.Component {
       registered: false
     };
 
-    this.handleRegister = this.handleRegister.bind(this);
+    //this.handleRegister = this.handleRegister.bind(this);
   }
 
   // shows error messages associated with registering
@@ -39,7 +39,7 @@ export class RegisterForm extends React.Component {
       this.handleError("Passwords do not match");
     } else {
       // Send register info to the server
-      fetch(`http://172.20.2.110:5000/user/register`, {
+      fetch("https:/tacs-testing.cf:8443/user/register", {
         method: "POST",
         headers: {
           Accept: "application/json",
diff --git a/ios/ReactNativeTest-tvOSTests/Info.plist b/ios/ReactNativeTest-tvOSTests/Info.plist
index 886825c..bbef99e 100755
--- a/ios/ReactNativeTest-tvOSTests/Info.plist
+++ b/ios/ReactNativeTest-tvOSTests/Info.plist
@@ -20,5 +20,10 @@
 	<string>????</string>
 	<key>CFBundleVersion</key>
 	<string>1</string>
+	<key>NSAppTransportSecurity</key>
+<dict>
+    <key>NSAllowsArbitraryLoads</key>
+    <true/>
+</dict>
 </dict>
 </plist>
-- 
GitLab