Skip to content
Snippets Groups Projects
Commit 0374ccb9 authored by Taneli Riihimäki's avatar Taneli Riihimäki
Browse files

ADD some comments and clean up the code a bit

parent a2f9198e
No related branches found
No related tags found
No related merge requests found
import React, { Component } from "react";
import { StyleSheet, Text, View, Button, AsyncStorage } from "react-native";
import { StyleSheet, View } from "react-native";
import FetchLocation from "./components/FetchLocation";
import Login from "./components/Login";
export default class App extends React.Component {
export default class App extends Component {
state = {
userLocation: null
};
......@@ -35,8 +34,6 @@ export default class App extends React.Component {
}
}
// <FetchLocation onStartTracking={this.userTrackingHandler} />
const styles = StyleSheet.create({
container: {
display: "flex",
......
import React from "react";
import { Button } from "react-native";
const fetchLocation = props => {
return <Button title="Start tracking" onPress={props.onStartTracking} />;
};
export default fetchLocation;
import React, { Component, Fragment } from "react";
import { FlatList, Button, Picker, Text, StyleSheet } from "react-native";
import { Picker, Text, StyleSheet } from "react-native";
import LocationTracker from "./LocationTracker";
......@@ -13,13 +13,14 @@ class GameList extends Component {
editForm: false
};
intervalID = 0;
intervalID = 0; // Used for starting and stopping the tracking ticks
}
componentDidMount() {
this.getGames();
}
// 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")
.then(response => response.json())
......@@ -33,25 +34,29 @@ class GameList extends Component {
console.log(error);
});
}
// Toggles the tracking boolean
handleTrackingChange() {
this.setState({ tracking: !this.state.tracking });
}
// Start the player tracking with the wanted interval (ms)
startTracking() {
let interval = 3000;
this.intervalID = setInterval(() => {
console.log(
"Nyt träkätää nim birusti peliä: " + this.state.selectedGame.name
);
console.log("Tracking: " + this.state.tracking);
}, 3000);
}, interval);
}
// Stop tracking the player
stopTracking() {
clearInterval(this.intervalID);
console.log("Nyt lopetetaa seuranta.");
}
// Start & stop tracking depending on the boolean 'tracking' in state
componentDidUpdate() {
if (this.state.tracking) {
this.startTracking();
......@@ -59,6 +64,8 @@ class GameList extends Component {
this.stopTracking();
}
}
// Stop tracking on Unmount
componentWillUnmount() {
clearInterval(this.intervalID);
}
......@@ -85,7 +92,7 @@ class GameList extends Component {
</Fragment>
);
return <Fragment>{items}</Fragment>;
} else return null;
} else return <Text>No games found.</Text>;
}
}
......
......@@ -16,6 +16,7 @@ const LocationTracker = props => {
);
};
// Need a container for the switch to align it in the middle
const styles = StyleSheet.create({
switchContainer: {
justifyContent: "center",
......
import React, { Component, Fragment } from "react";
import { View, Button, AsyncStorage, StyleSheet } from "react-native";
import { View, Button, StyleSheet } from "react-native";
import { AsyncStorage } from "async-storage";
import LoginForm from "./LoginForm";
import RegisterForm from "./RegisterForm";
......@@ -7,21 +8,25 @@ import GameList from "./GameList";
class Login extends Component {
state = {
form: "", // Popup form (login, register etc.)
form: "", // Used for forms (login, register etc.)
username: null,
token: null
};
// Toggling the register and login forms visibility
toggleView = view => {
this.setState({
form: view
});
};
// Storing the username and token when logging in
handleState = async data => {
await AsyncStorage.setItem("name", data.name);
await AsyncStorage.setItem("token", data.token);
await this.setState({ username: data.name, token: data.token });
};
// When logging out, removing the token from the storage
handleLogout = async () => {
await this.setState({ username: null, token: null });
await AsyncStorage.removeItem("token");
......
......@@ -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
......
......@@ -7,10 +7,9 @@
"test": "jest"
},
"dependencies": {
"@terrylinla/react-native-sketch-canvas": "^0.8.0",
"react": "16.8.3",
"react-native": "0.59.8",
"react-native-maps": "^0.24.2"
"@react-native-community/async-storage": "^1.5.0",
"react": "16.8.6",
"react-native": "0.60.0"
},
"devDependencies": {
"@babel/core": "7.4.5",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment