Skip to content

Commit

Permalink
fixed crash on no user
Browse files Browse the repository at this point in the history
  • Loading branch information
cqb13 committed Sep 18, 2023
1 parent 0561674 commit 59348e0
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 86 deletions.
99 changes: 54 additions & 45 deletions app/account/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default function History() {
const [profileType, setProfileType] = useState("Private");

useEffect(() => {
if (!user) return;
auth.onAuthStateChanged((user) => {
if (user) {
setSignedIn(true);
Expand All @@ -55,7 +56,7 @@ export default function History() {
setHighScore(doc.highScore);
setLowScore(doc.lowScore);
});
}, []);
}, [user]);

const [currentWindow, setCurrentWindow] = useState("stats");

Expand Down Expand Up @@ -92,52 +93,60 @@ export default function History() {
selected={currentWindow == "account" ? true : false}
/>
</div>
{currentWindow == "stats" ? (
<section className='flex flex-wrap justify-center gap-2'>
<StatBox name='Average Score' value={averageScore} />
<StatBox name='High Score' value={highScore} />
<StatBox name='Low Score' value={lowScore} />
<StatBox name='Total Games' value={totalGames} />
<StatBox name='Total Splits' value={totalSplits} />
</section>
) : null}
{currentWindow == "social" ? (
<h1>Ill get around to this at some point</h1>
) : null}
{currentWindow == "account" ? (
<section className='shadow-card p-10 border border-gray-300 rounded-md flex gap-2 w-full max-mdLg:flex-col max-mdLg:items-center'>
<Image
src={userDoc?.photoURL.replaceAll("s96-c", "s500-c")}
alt='use pfp'
className='rounded-2xl'
width={300}
height={300}
/>
<div className='flex gap-2 w-full max-smSm:flex-col'>
<div className='flex flex-col py-4 gap-2 w-full'>
<Input
value={name}
placeholder={`username: ${namePlaceholder}`}
type='text'
updateValue={setName}
{user ? (
<>
{currentWindow == "stats" ? (
<section className='flex flex-wrap justify-center gap-2'>
<StatBox name='Average Score' value={averageScore} />
<StatBox name='High Score' value={highScore} />
<StatBox name='Low Score' value={lowScore} />
<StatBox name='Total Games' value={totalGames} />
<StatBox name='Total Splits' value={totalSplits} />
</section>
) : null}
{currentWindow == "social" ? (
<h1>Ill get around to this at some point</h1>
) : null}
{currentWindow == "account" ? (
<section className='shadow-card p-10 border border-gray-300 rounded-md flex gap-2 w-full max-mdLg:flex-col max-mdLg:items-center'>
<Image
src={userDoc?.photoURL.replaceAll("s96-c", "s500-c")}
alt='use pfp'
className='rounded-2xl'
width={300}
height={300}
/>
<Button title='Update Name' onClick={updateName} />
<div className='flex flex-col items-center justify-center'>
<p>Social Profile</p>
<Dropdown
title={profileType}
items={["Private", "Public", "Email Only"]}
setSelected={changeProfileType}
/>
<div className='flex gap-2 w-full max-smSm:flex-col'>
<div className='flex flex-col py-4 gap-2 w-full'>
<Input
value={name}
placeholder={`username: ${namePlaceholder}`}
type='text'
updateValue={setName}
/>
<Button title='Update Name' onClick={updateName} />
<div className='flex flex-col items-center justify-center'>
<p>Social Profile</p>
<Dropdown
title={profileType}
items={["Private", "Public", "Email Only"]}
setSelected={changeProfileType}
/>
</div>
</div>
<div className='flex flex-col-reverse py-4 gap-2 w-full'>
<Button title='Sign Out' onClick={() => googleSignOut()} />
<Button title='Delete Account' onClick={() => {}} />
</div>
</div>
</div>
<div className='flex flex-col-reverse py-4 gap-2 w-full'>
<Button title='Sign Out' onClick={() => googleSignOut()} />
<Button title='Delete Account' onClick={() => {}} />
</div>
</div>
</section>
) : null}
</section>
) : null}
</>
) : (
<h1 className=' animate-pulse border-gray-300 p-10 rounded-md shadow-card'>
Authenticating User
</h1>
)}
</main>
);
}
101 changes: 60 additions & 41 deletions app/history/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import getAllSessions from "@/utils/score/getAllSessions";
import FinalScoringStats from "@/components/scoring/finalScoringStats";
import ScoringChart from "@/components/scoring/scoringChart";
import { useAuthContext } from "@context/authContext";
import Dropdown from "@/components/general/dropdown";
import { doc, getDoc } from "firebase/firestore";
import Button from "@/components/general/button";
Expand All @@ -14,6 +15,7 @@ export default function History() {
const [currentGameName, setCurrentGameName] = useState("");
const [gameNameList, setGameNameList] = useState([] as string[]);
const [dateMap, setDateMap] = useState(new Map());
const { user } = useAuthContext() as { user: any };

// session info
const [arrowsPerEnd, setArrowsPerEnd] = useState(0);
Expand All @@ -27,17 +29,22 @@ export default function History() {
const [bow, setBow] = useState("");

useEffect(() => {
getAllSessions(auth.currentUser).then(({ sortedMap, dateMap }) => {
setDateMap(dateMap);
setCurrentGame(dateMap.size);
setCurrentGameName(sortedMap.keys().next().value);
if (!user) return;
setupHistory();
}, [user]);

let listOfGames = Array.from(sortedMap.keys());
setGameNameList(listOfGames);
const setupHistory = async () => {
let { sortedMap, dateMap } = await getAllSessions(auth.currentUser);

getScoreDoc(sortedMap.values().next().value);
});
}, []);
setDateMap(dateMap);
setCurrentGame(dateMap.size);
setCurrentGameName(sortedMap.keys().next().value);

let listOfGames = Array.from(sortedMap.keys());
setGameNameList(listOfGames);

getScoreDoc(sortedMap.values().next().value);
};

const getScoreDoc = async (id: string) => {
if (!auth.currentUser) return;
Expand All @@ -57,7 +64,11 @@ export default function History() {
}
};

//!!! dropdown name not updating
const changeGame = (date: string) => {
setCurrentGameName(date);
let reverseGameNameList = gameNameList.slice().reverse();
setCurrentGame(reverseGameNameList.indexOf(date) + 1);
getScoreDoc(dateMap.get(date));
};

Expand All @@ -84,38 +95,46 @@ export default function History() {

return (
<main className='flex flex-col gap-2 items-center justify-center pt-4 text-black'>
<section className='flex gap-2 w-full items-center justify-center max-smSm:flex-col'>
<Dropdown
title={currentGameName}
items={gameNameList}
customClass=' w-full'
setSelected={() => {}}
/>
<div className='flex items-center justify-center gap-2'>
<Button title='Back' onClick={() => switchGame("back")} />
<h2 className='whitespace-nowrap'>{`Game ${currentGame}/${gameNameList.length}`}</h2>
<Button title='Next' onClick={() => switchGame("next")} />
</div>
</section>
<section className='flex gap-2 items-center justify-center'>
<h2 className='whitespace-nowrap'>
{`${location} ${distance}${distanceUnit} ${bow}`}
</h2>
<Button title='Note' onClick={() => {}} />
<Button title='Delete' onClick={() => {}} />
</section>
{score.length > 0 ? (
<section className='flex flex-col gap-2'>
<ScoringChart
arrowsPerEnd={arrowsPerEnd}
history={true}
splits={splits}
done={false}
score={score}
/>
<FinalScoringStats score={score} />
</section>
) : null}
{user ? (
<>
<section className='flex gap-2 w-full items-center justify-center max-smSm:flex-col'>
<Dropdown
title={currentGameName}
items={gameNameList}
customClass=' w-full'
setSelected={changeGame}
/>
<div className='flex items-center justify-center gap-2'>
<Button title='Back' onClick={() => switchGame("back")} />
<h2 className='whitespace-nowrap'>{`Game ${currentGame}/${gameNameList.length}`}</h2>
<Button title='Next' onClick={() => switchGame("next")} />
</div>
</section>
<section className='flex gap-2 items-center justify-center'>
<h2 className='whitespace-nowrap'>
{`${location} ${distance}${distanceUnit} ${bow}`}
</h2>
<Button title='Note' onClick={() => {}} />
<Button title='Delete' onClick={() => {}} />
</section>
{score.length > 0 ? (
<section className='flex flex-col gap-2'>
<ScoringChart
arrowsPerEnd={arrowsPerEnd}
history={true}
splits={splits}
done={false}
score={score}
/>
<FinalScoringStats score={score} />
</section>
) : null}
</>
) : (
<h1 className=' animate-pulse border-gray-300 p-10 rounded-md shadow-card'>
Authenticating User
</h1>
)}
</main>
);
}

0 comments on commit 59348e0

Please sign in to comment.