Skip to content

Commit

Permalink
Added a test page for UserPrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Apr 25, 2024
1 parent 1d92705 commit 47c0405
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
64 changes: 46 additions & 18 deletions src/pages/test firestore/TestFirestore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {getCurrentBalance} from "../../utils/transaction_utils.ts";
import {signInWithGoogle} from "../../utils/authentication.ts";
import {Header} from "../../components/Header.tsx";
import { orderBy } from "firebase/firestore";
import {User} from "firebase/auth";
import {getUserPrefs, setUserPrefs, UserPrefs} from "../../utils/user_prefs.ts";

function writeSampleData() {
if (auth.currentUser === null) {
Expand Down Expand Up @@ -43,6 +45,8 @@ export function TestFirestorePage() {
const [transactions, setTransactions] = useState<Transaction[]>([]);
const [balance, setBalance] = useState(0);
const [update ,setUpdate] = useState(0);
const [user, setUser] = useState<string | undefined>(undefined);
const [userPrefs, _setUserPrefs] = useState<UserPrefs | null>(null);

if (!authResolved) {
auth.authStateReady().then(() => setAuthResolved(true));
Expand All @@ -56,6 +60,11 @@ export function TestFirestorePage() {
}

if (auth.currentUser === null) {
auth.onAuthStateChanged((new_user: User | null) => {
if (new_user !== null && new_user.uid !== user) {
setUser(new_user.uid)
}
});
return <>
<Header/>
<FullscreenCenter>
Expand All @@ -71,6 +80,7 @@ export function TestFirestorePage() {

getTransactionsFilterOrderBy(auth.currentUser, orderBy("dateTime", "desc")).then((t) => setTransactions(t));
getCurrentBalance(auth.currentUser).then((b) => setBalance(b));
getUserPrefs(auth.currentUser).then((up) => _setUserPrefs(up));

return (
<>
Expand All @@ -81,30 +91,48 @@ export function TestFirestorePage() {
Sign in with Google
</button>
</div>
<button onClick={() => {
<div>
<h4>UserPrefs</h4>
{
userPrefs ? <>
<p>Goal: {userPrefs.goal}</p>
<button className="mb-4" onClick={() => {
userPrefs!.goal += 100;
setUserPrefs(auth.currentUser!, userPrefs).then(() => setUpdate(update + 1));
}}>Increment
</button>
</> : <p>Loading</p>
}
</div>
<h1>
Transactions
</h1>
<p>
Balance: {balance}
</p>
<button className="mb-4" onClick={() => {
writeSampleData();
setUpdate(update + 1);
}}>Add Sample Transaction
</button>
<p>
Balance: {balance}
</p>
<p>
Transactions: <br/>
</p>
{
transactions.map((t) => <div className="mb-4" key={t.forceGetDocName()}>
<p className="m-0">{new Date(t.dateTime).toISOString()}</p>
<textarea readOnly={true} style={{width: "100%", height: "320px"}} value={JSON.stringify(t, null, 4)}/>
<button onClick={() => {deleteTransaction(t.forceGetDocName()).then(() => setUpdate(update + 1))}}>Delete</button>
<button onClick={() => {
if (auth.currentUser != null) {
t.dateTime = new Date(Date.now()).valueOf();
overwriteTransaction(auth.currentUser, t.forceGetDocName(), t).then(() => setUpdate(update + 1));
}
}}>Set Time To Now</button>
</div>
)}
<p className="m-0">{new Date(t.dateTime).toISOString()}</p>
<textarea readOnly={true} style={{width: "100%", height: "320px"}}
value={JSON.stringify(t, null, 4)}/>
<button onClick={() => {
deleteTransaction(t.forceGetDocName()).then(() => setUpdate(update + 1))
}}>Delete
</button>
<button onClick={() => {
if (auth.currentUser != null) {
t.dateTime = new Date(Date.now()).valueOf();
overwriteTransaction(auth.currentUser, t.forceGetDocName(), t).then(() => setUpdate(update + 1));
}
}}>Set Time To Now
</button>
</div>
)}
</>
)
}
3 changes: 1 addition & 2 deletions src/utils/user_prefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export class UserPrefs {
if (!data) {
throw Error("No data returned for snapshot!");
}
const u = new UserPrefs(data.goal);
return u;
return new UserPrefs(data.goal);
}

toSendObject(): object {
Expand Down

0 comments on commit 47c0405

Please sign in to comment.