Unverified Commit fb8381f6 authored by Michal Čaniga's avatar Michal Čaniga
Browse files

Delete unused code, refactor

parent 3ca5193d
import { UserData } from '../utils/firebase';
export const dummyUserData: UserData[] = [
{
userId: '1',
email: 'dummy1@example.com',
catchedPokemonIds: [1, 2, 3],
pokeballIds: [1],
foodIds: [1],
actualScore: 213,
highestScore: 1647,
highestSecondsAlive: 8
},
{
userId: '2',
email: 'dummy2@example.com',
catchedPokemonIds: [5, 8],
pokeballIds: [2],
foodIds: [],
actualScore: 1365,
highestScore: 13657,
highestSecondsAlive: 21
},
{
userId: '3',
email: 'dummy3@example.com',
catchedPokemonIds: [6, 9],
pokeballIds: [],
foodIds: [1],
actualScore: 1234,
highestScore: 2348,
highestSecondsAlive: 81
},
{
userId: '4',
email: 'dummy4@example.com',
catchedPokemonIds: [1, 2],
pokeballIds: [1],
foodIds: [1],
actualScore: 1234,
highestScore: 64123,
highestSecondsAlive: 312
},
{
userId: '5',
email: 'dummy5@example.com',
catchedPokemonIds: [2],
pokeballIds: [1],
foodIds: [1],
actualScore: 2136,
highestScore: 21487,
highestSecondsAlive: 124
},
{
userId: '6',
email: 'dummy6@example.com',
catchedPokemonIds: [3],
pokeballIds: [1],
foodIds: [1],
actualScore: 75,
highestScore: 513,
highestSecondsAlive: 12
},
{
userId: '7',
email: 'dummy7@example.com',
catchedPokemonIds: [1, 2, 3, 8],
pokeballIds: [1],
foodIds: [1],
actualScore: 1235,
highestScore: 5413,
highestSecondsAlive: 123
},
{
userId: '8',
email: 'dummy8@example.com',
catchedPokemonIds: [1, 3, 4],
pokeballIds: [1],
foodIds: [1],
actualScore: 654,
highestScore: 2486,
highestSecondsAlive: 71
},
{
userId: '9',
email: 'dummy9@example.com',
catchedPokemonIds: [30, 31],
pokeballIds: [1],
foodIds: [1],
actualScore: 1003,
highestScore: 4879,
highestSecondsAlive: 96
},
{
userId: '10',
email: 'dummy10@example.com',
catchedPokemonIds: [100],
pokeballIds: [1],
foodIds: [1],
actualScore: 745,
highestScore: 12387,
highestSecondsAlive: 12
},
{
userId: '11',
email: 'dummy11@example.com',
catchedPokemonIds: [82, 83],
pokeballIds: [1],
foodIds: [1],
actualScore: 819,
highestScore: 67512,
highestSecondsAlive: 16
},
{
userId: '12',
email: 'dummy12@example.com',
catchedPokemonIds: [84, 86],
pokeballIds: [1],
foodIds: [1],
actualScore: 123,
highestScore: 41235,
highestSecondsAlive: 19
}
];
export const getDummyUserData = (id: string): UserData =>
dummyUserData.filter(obj => obj.userId === id)[0];
import { User } from 'firebase/auth';
// necessary subset of firebase User
export const dummyUsers = [
{
uid: '1',
email: 'dummy1@example.com'
},
{
uid: '2',
email: 'dummy2@example.com'
},
{
uid: '3',
email: 'dummy3@example.com'
},
{
uid: '4',
email: 'dummy4@example.com'
},
{
uid: '5',
email: 'dummy5@example.com'
},
{
uid: '6',
email: 'dummy6@example.com'
},
{
uid: '7',
email: 'dummy7@example.com'
},
{
uid: '8',
email: 'dummy8@example.com'
},
{
uid: '9',
email: 'dummy9@example.com'
},
{
uid: '10',
email: 'dummy10@example.com'
},
{
uid: '11',
email: 'dummy11@example.com'
},
{
uid: '12',
email: 'dummy12@example.com'
}
] as User[];
import { getDataByIds } from '../utils/dataUtils';
import { Pokemon } from './pokemonFetcher';
import { Pokemon } from '../utils/pokemonFetcher';
export const getCatchedPokemons = (catchedPokemonIds: number[]): Pokemon[] =>
getDataByIds(pokemons, catchedPokemonIds);
......
import { ChangeEvent, useCallback, useState } from 'react';
// TODO: rewrite to work with antd (maybe works, not tested)
const useField = (id: string, required?: boolean) => {
const [value, setValue] = useState('');
const [touched, setTouched] = useState(false);
const error = required && touched && !value;
return [
// Current value for convenient access
value,
// Props for the TextField
{
id,
value,
onChange: useCallback(
(e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) =>
setValue(e.target.value),
[]
),
onBlur: useCallback(() => setTouched(true), []),
required,
error,
helperText: error ? 'Required' : undefined
}
] as const;
};
export default useField;
......@@ -3,13 +3,19 @@ import { useEffect, useState } from 'react';
import { UserData, userDataCollection } from '../utils/firebase';
const useUserData = (uid: string) => {
import useLoggedInUser from './useLoggedInUser';
const useLoggedInUserData = () => {
const currentUser = useLoggedInUser();
const [userData, setUserData] = useState<UserData>();
useEffect(() => {
const unsubscribe = onSnapshot(userDataCollection, snapshot => {
if (currentUser === undefined) {
return;
}
const allUserData = snapshot.docs.map(doc => doc.data());
setUserData(allUserData.filter(ud => ud.userId === uid)[0]);
setUserData(allUserData.filter(ud => ud.userId === currentUser.uid)[0]);
});
return () => {
unsubscribe();
......@@ -19,4 +25,4 @@ const useUserData = (uid: string) => {
return userData;
};
export default useUserData;
export default useLoggedInUserData;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment