-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
129 lines (117 loc) · 3.26 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {useState, useEffect} from 'react';
import type {Node} from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
Text,
Image,
ImageBackground,
TouchableOpacity,
View,
} from 'react-native';
import auth from '@react-native-firebase/auth';
import firestore from '@react-native-firebase/firestore';
import styles from './components/stylesheet/index.js';
import Home from './components/home/index.js';
import CreateGame from './components/create-game/index.js';
import JoinGame from './components/join-game/index.js';
import GameScreen from './components/game/index.js';
import Loser from './components/loser/index.js';
import Winner from './components/winner/index.js';
const App: () => Node = () => {
const [screen, setScreen] = useState('home');
const [userAuth, setUserAuth] = useState(null);
const [initializing, setInitializing] = useState(true);
const [gameId, setGameId] = useState(null);
const [side, setSide] = useState('');
const [globalGameCode, setGlobalGameCode] = useState('');
var subscriber = null;
const onAuthStateChanged = user => {
setUserAuth(user);
if (initializing) setInitializing(false);
};
useEffect(() => {
if (gameId) {
let sub = firestore()
.collection('tic-games')
.doc(gameId)
.onSnapshot(doc => {
if (doc.data().isPlaying) setScreen('game');
});
if (screen === 'game') sub();
} else {
const authSubscriber = auth().onAuthStateChanged(onAuthStateChanged);
auth()
.signInAnonymously()
.then(() => {
console.log('User signed in anonymously');
})
.catch(err => {
let friendlyError = {
friendly: "We couldn't authenticate you with the game service.",
technical: err.toString(),
};
setError(() => {
throw friendlyError;
});
});
return () => {
if (subscriber !== null) {
subscriber();
authSubscriber();
}
};
}
}, [gameId]);
if (initializing) return null;
return (
<>
<StatusBar barStyle="dark-content" />
{screen === 'home' ? (
<Home styles={styles} setScreen={setScreen} />
) : null}
{screen === 'create-game' ? (
<CreateGame
styles={styles}
setScreen={setScreen}
setGameId={setGameId}
setSide={setSide}
setGlobalGameCode={setGlobalGameCode}
/>
) : null}
{screen === 'join-game' ? (
<JoinGame
styles={styles}
setScreen={setScreen}
setGameId={setGameId}
setSide={setSide}
setGlobalGameCode={setGlobalGameCode}
/>
) : null}
{screen === 'game' ? (
<GameScreen
styles={styles}
setScreen={setScreen}
gameId={gameId}
side={side}
globalGameCode={globalGameCode}
/>
) : null}
{screen === 'winner' ? (
<Winner styles={styles} setScreen={setScreen} />
) : null}
{screen === 'loser' ? (
<Loser styles={styles} setScreen={setScreen} />
) : null}
</>
);
};
export default App;