Skip to content

Commit

Permalink
Account deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
cqb13 committed Sep 18, 2023
1 parent 64afdb1 commit 2940240
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
50 changes: 49 additions & 1 deletion app/account/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use client";

import updateDisplayName from "@/utils/firebase/db/updateDisplayName";
import ConfirmPopup from "@/components/misc/confirmPopup";
import NotificationPopup from "@/components/general/notificationPopup";
import updateProfileType from "@/utils/firebase/db/updateProfileType";
import deleteAccount from "@/utils/firebase/account/deleteAccount";
import googleSignOut from "@/utils/firebase/account/googleSignOut";
Expand All @@ -16,6 +18,7 @@ import { useState, useEffect } from "react";
import { useRouter } from "next/navigation";
import { db, auth } from "@lib/firebase";
import Image from "next/image";
import { set } from "firebase/database";

export default function History() {
// general
Expand All @@ -24,6 +27,16 @@ export default function History() {
const { user } = useAuthContext() as { user: any };
const router = useRouter();

// popup
const [deletePopup, setDeletePopup] = useState(false);

const [notification, setNotification] = useState(false);
const [notificationType, setNotificationType] = useState(
{} as "success" | "error"
);
const [notificationTitle, setNotificationTitle] = useState("");
const [notificationMessage, setNotificationMessage] = useState("");

// stats
const [averageScore, setAverageScore] = useState(0);
const [highScore, setHighScore] = useState(0);
Expand Down Expand Up @@ -74,6 +87,19 @@ export default function History() {
updateProfileType(auth.currentUser, type);
};

const deleteAction = () => {
setDeletePopup(true);
};

const confirmDelete = async () => {
await deleteAccount(auth.currentUser);
router.push("/");
};

const cancelDelete = () => {
setDeletePopup(false);
};

return (
<main className='flex flex-col items-center m-auto pt-4 text-black p-24 w-4/5 max-lgLg:w-11/12 max-lgSm:w-full max-lgSm:p-10 max-smSm:p-5 max-xs:p-2'>
<div className='flex gap-2 w-full mb-2'>
Expand Down Expand Up @@ -136,7 +162,7 @@ export default function History() {
</div>
<div className='flex flex-col-reverse py-4 gap-2 w-full'>
<Button title='Sign Out' onClick={() => googleSignOut()} />
<Button title='Delete Account' onClick={() => {}} />
<Button title='Delete Account' onClick={deleteAction} />
</div>
</div>
</section>
Expand All @@ -147,6 +173,28 @@ export default function History() {
Authenticating User
</h1>
)}
{deletePopup ? (
<ConfirmPopup
title='Delete Game'
message='Are you sure you want to delete your account?'
expectedValue={"DELETE ACCOUNT"}
confirm={confirmDelete}
cancel={cancelDelete}
setNotification={setNotification}
setNotificationType={setNotificationType}
setNotificationTitle={setNotificationTitle}
setNotificationMessage={setNotificationMessage}
/>
) : null}
{notification ? (
<NotificationPopup
title={notificationTitle}
message={notificationMessage}
type={notificationType}
timeout={5000}
updateNotification={setNotification}
/>
) : null}
</main>
);
}
1 change: 0 additions & 1 deletion app/history/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { doc, getDoc } from "firebase/firestore";
import Button from "@/components/general/button";
import { useEffect, useState } from "react";
import { auth, db } from "@lib/firebase";
import { set } from "firebase/database";

export default function History() {
const [currentGame, setCurrentGame] = useState(1);
Expand Down
8 changes: 7 additions & 1 deletion utils/score/getAverageScore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ export default function getAverageScore(allScores: any) {

let average = total / countTotalSplits(allScores);

return Math.round(average * 10) / 10;
let averageScore = Math.round(average * 10) / 10;

if (isNaN(averageScore)) {
return 0;
}

return averageScore;
}

1 comment on commit 2940240

@vercel
Copy link

@vercel vercel bot commented on 2940240 Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.