Skip to content
Snippets Groups Projects
GameList.js 2.3 KiB
Newer Older
import React, { Component, Fragment } from "react";
import { FlatList, Button, Picker, Text, StyleSheet } from "react-native";

import LocationTracker from "./LocationTracker";

class GameList extends Component {
  constructor(props) {
    super(props);
    this.state = {
      games: [],
      selectedGame: null,
      editForm: false
    };
  }

  componentDidMount() {
    this.getGames();
  }

  getGames() {
    fetch("http://172.20.2.110:5000/game/listgames")
      .then(response => response.json())
      .then(games => {
        this.setState({
  handleTrackingChange() {
    this.setState({ tracking: !this.state.tracking });
  }

  startTracking() {
    this.intervalID = setInterval(() => {
      console.log("Valittu peli: " + this.state.selectedGame);
      console.log(
        "Nyt träkätää nim birusti peliä: " + this.state.selectedGame.name
      );
      console.log("Tracking: " + this.state.tracking);
    }, 3000);
  }

  stopTracking() {
    clearInterval(this.intervalID);
    console.log("Nyt lopetetaa seuranta.");
  }

  componentDidUpdate() {
    if (this.state.tracking) {
      this.startTracking();
    } else if (!this.state.tracking) {
      this.stopTracking();
    }
  }
  componentWillUnmount() {
    clearInterval(this.intervalID);
  }
    if (this.state.games.length > 0) {
      const items = (
        <Fragment>
          <Picker
            style={styles.picker}
            selectedValue={this.state.selectedGame}
            onValueChange={(selectedGame, itemIndex) => {
              clearInterval(this.intervalID);
              this.setState({ selectedGame, tracking: "false" });
            }}
          >
            {this.state.games.map(game => (
              <Picker.Item label={game.name} value={game} />
            ))}
          </Picker>
          <LocationTracker
            tracking={this.state.tracking}
            handleTrackingChange={this.handleTrackingChange.bind(this)}
        </Fragment>
      );
      return <Fragment>{items}</Fragment>;
    } else return null;

const styles = StyleSheet.create({
  picker: {
Taneli Riihimäki's avatar
Taneli Riihimäki committed
    paddingVertical: 50