Skip to content

Commit

Permalink
Added final UserPrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Apr 27, 2024
1 parent 6e5d46b commit f08ffbc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/pages/test firestore/TestFirestore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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";
import {round} from "lodash";

function writeSampleData() {
if (auth.currentUser === null) {
Expand Down Expand Up @@ -95,11 +96,14 @@ export function TestFirestorePage() {
<h4>UserPrefs</h4>
{
userPrefs ? <>
<p>Goal: {userPrefs.goal}</p>
<p>Goal: {userPrefs.getNeedsBudget()} | {userPrefs.getWantsBudget()} | {userPrefs.getSavingsBudget()}</p>
<button className="mb-4" onClick={() => {
userPrefs!.goal += 100;
setUserPrefs(auth.currentUser!, userPrefs).then(() => setUpdate(update + 1));
}}>Increment
const needs = round(faker.number.float({min: 0, max: 0.5}), 2);
const wants = round(faker.number.float({min: 0, max: 0.5}), 2);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
setUserPrefs(auth.currentUser!, UserPrefs.newChecked(needs, wants)).then(() => setUpdate(update + 1));
}}>Randomise
</button>
</> : <p>Loading</p>
}
Expand Down
46 changes: 41 additions & 5 deletions src/utils/user_prefs.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
import {collection, doc, DocumentSnapshot, getDoc, setDoc, SnapshotOptions} from "firebase/firestore";
import {User} from "firebase/auth";
import {db} from "./firebase.ts";
import {round} from "lodash";


export class UserPrefs {
public goal: number;
private readonly needsBudget: number;
private readonly wantsBudget: number;

constructor(goal: number) {
this.goal = goal;
private constructor(needsBudget: number, wantsBudget: number) {
if (needsBudget > 1) {
needsBudget = 1;
}

if (needsBudget + wantsBudget > 1) {
wantsBudget = 1 - needsBudget;
}

this.needsBudget = round(needsBudget, 2);
this.wantsBudget = round(wantsBudget, 2);
}

static newChecked(needsBudget: number, wantsBudget: number): UserPrefs | Error {
if (needsBudget > 1) {
return new Error("needsBudget > 1!");
}

if (needsBudget + wantsBudget > 1) {
return new Error("needsBudget + wantsBudget > 1!");
}

return new UserPrefs(needsBudget, wantsBudget);
}

static default(): UserPrefs {
return new UserPrefs(100);
return new UserPrefs(0.5, 0.3);
}

getNeedsBudget(): number {
return this.needsBudget;
}

getWantsBudget(): number {
return this.wantsBudget;
}

getSavingsBudget(): number {
return round(1 - this.wantsBudget - this.needsBudget, 2);
}

// Utility method for creating `Transactions`
Expand All @@ -20,7 +55,8 @@ export class UserPrefs {
if (!data) {
throw Error("No data returned for snapshot!");
}
return new UserPrefs(data.goal);

return new UserPrefs(round(data.needsBudget, 2), round(data.wantsBudget, 2));
}

toSendObject(): object {
Expand Down

0 comments on commit f08ffbc

Please sign in to comment.