created random name generator

pull/3/head
Simon Huang 6 years ago
parent 9fb6678656
commit 4c5ccc0afb

@ -15,6 +15,11 @@ const collection = {
}, },
}, },
}, },
users: {
userId: {
name: 'Anonymous',
},
},
}; };
// Realtime Database - needed for tracking user presence // Realtime Database - needed for tracking user presence

@ -3,6 +3,7 @@ import React, { useEffect, useState } from 'react';
import { useHistory } from 'react-router'; import { useHistory } from 'react-router';
import { db, timestamp, auth, rtdb } from '../services/firebase'; import { db, timestamp, auth, rtdb } from '../services/firebase';
import './Home.css'; import './Home.css';
import { generateAnonName } from '../services/random';
const Home: React.FC = () => { const Home: React.FC = () => {
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
@ -25,12 +26,15 @@ const Home: React.FC = () => {
// Sign in anonymously before showing Create Room button // Sign in anonymously before showing Create Room button
useEffect(() => { useEffect(() => {
const authUnsubscribe = auth.onAuthStateChanged((user) => { const authUnsubscribe = auth.onAuthStateChanged(async (user) => {
if (user) { if (user) {
setUserId(user.uid); setUserId(user.uid);
setLoading(false); setLoading(false);
} else { } else {
auth.signInAnonymously(); const credential = await auth.signInAnonymously();
await db.collection('users').doc(credential.user?.uid).set({
name: generateAnonName(),
});
} }
}); });

@ -1,8 +1,9 @@
import { IonContent, IonHeader, IonPage, IonTitle, IonToolbar } from '@ionic/react'; import { IonContent, IonHeader, IonPage, IonTitle, IonToolbar } from '@ionic/react';
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { RouteComponentProps, useHistory } from 'react-router'; import { RouteComponentProps, useHistory } from 'react-router';
import { auth, db, rtdb, increment, decrement } from '../services/firebase';
import Chat from '../components/Chat'; import Chat from '../components/Chat';
import { auth, db, decrement, increment, rtdb } from '../services/firebase';
import { generateAnonName } from '../services/random';
const Room: React.FC<RouteComponentProps<{ roomId: string }>> = ({ match }) => { const Room: React.FC<RouteComponentProps<{ roomId: string }>> = ({ match }) => {
const history = useHistory(); const history = useHistory();
@ -30,11 +31,14 @@ const Room: React.FC<RouteComponentProps<{ roomId: string }>> = ({ match }) => {
// Handle logging in // Handle logging in
useEffect(() => { useEffect(() => {
const authUnsubscribe = auth.onAuthStateChanged((user) => { const authUnsubscribe = auth.onAuthStateChanged(async (user) => {
if (user) { if (user) {
setUserId(user.uid); setUserId(user.uid);
} else { } else {
auth.signInAnonymously(); const credential = await auth.signInAnonymously();
await db.collection('users').doc(credential.user?.uid).set({
name: generateAnonName(),
});
} }
}); });
@ -43,13 +47,14 @@ const Room: React.FC<RouteComponentProps<{ roomId: string }>> = ({ match }) => {
}; };
}, []); }, []);
// Keep track of online user presence in realtime database rooms // Subscribe listeners
useEffect(() => { useEffect(() => {
if (!didConnect && userId !== '' && validRoom) { if (!didConnect && userId !== '' && validRoom) {
const populateRoom = () => { const populateRoom = () => {
const roomRef = rtdb.ref('/rooms/' + roomId); const roomRef = rtdb.ref('/rooms/' + roomId);
const availableRef = rtdb.ref('/available/'); const availableRef = rtdb.ref('/available/');
// Keep track of online user presence in realtime database rooms
roomRef.on('value', async (snapshot) => { roomRef.on('value', async (snapshot) => {
if (!snapshot.hasChild(userId)) { if (!snapshot.hasChild(userId)) {
// Keep userId in the room as long as a connection from the client exists // Keep userId in the room as long as a connection from the client exists

@ -0,0 +1,51 @@
const adjectives = [
'Adamant',
'Instinctive',
'Actually',
'Husky',
'Bent',
'Fascinated',
'Sexual',
'Mute',
'Silent',
'Coherent',
'Juvenile',
'Naughty',
'Foreign',
'Earthy',
'Diligent',
'Anxious',
'Adorable',
'Quack',
'Unequal',
'Sharp',
];
const animals = [
'Chimpanzee',
'Bison',
'Squirrel',
'Lemur',
'Wolf',
'Dingo',
'Colt',
'Seal',
'Cougar',
'Ram',
'Parakeet',
'Goat',
'Ape',
'Basilisk',
'Oryx',
'Iguana',
'Stallion',
'Jackal',
'Snake',
'Zebra',
];
export const generateAnonName = (): string => {
const adj: string = adjectives[Math.floor(Math.random() * 20)];
const animal: string = animals[Math.floor(Math.random() * 20)];
return adj + ' ' + animal;
};
Loading…
Cancel
Save