Skip to content

Commit

Permalink
Added support for OrderBy
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Apr 23, 2024
1 parent 0ffd92e commit 1290292
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
12 changes: 7 additions & 5 deletions src/pages/test firestore/TestFirestore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {auth} from "../../utils/firebase.ts";
import {useState} from "react";
import {
deleteTransaction,
getTransactions,
getTransactionsFilterOrderBy,
overwriteTransaction,
Transaction,
writeNewTransaction
Expand All @@ -12,6 +12,7 @@ import {faker, fakerEN_GB} from "@faker-js/faker";
import {getCurrentBalance} from "../../utils/transaction_utils.ts";
import {signInWithGoogle} from "../../utils/authentication.ts";
import {Header} from "../../components/Header.tsx";
import { orderBy } from "firebase/firestore";

function writeSampleData() {
if (auth.currentUser === null) {
Expand All @@ -25,7 +26,7 @@ function writeSampleData() {
parseFloat(faker.finance.amount({min: -1000, max: 1000})),
faker.word.noun(),
faker.finance.currency().code,
faker.date.past().toISOString(),
faker.date.past().valueOf(),
faker.lorem.sentence(),
faker.internet.emoji(),
faker.word.noun(),
Expand Down Expand Up @@ -68,7 +69,7 @@ export function TestFirestorePage() {
</>;
}

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

return (
Expand All @@ -92,12 +93,13 @@ export function TestFirestorePage() {
Transactions: <br/>
</p>
{
transactions.map((t) => <div key={t.forceGetDocName()}>
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()).toISOString();
t.dateTime = new Date(Date.now()).valueOf();
overwriteTransaction(auth.currentUser, t.forceGetDocName(), t).then(() => setUpdate(update + 1));
}
}}>Set Time To Now</button>
Expand Down
9 changes: 5 additions & 4 deletions src/utils/firestore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {collection,
deleteDoc, doc, DocumentSnapshot, getDocs, limit, query, QueryFieldFilterConstraint, setDoc, SnapshotOptions,
deleteDoc, doc, DocumentSnapshot, getDocs, limit, query,
QueryConstraint, setDoc, SnapshotOptions,
startAfter, where, writeBatch} from "firebase/firestore";
import {User} from "firebase/auth";
import {db} from "./firebase.ts";
Expand All @@ -11,14 +12,14 @@ export class Transaction {
public amount: number;
public category: string;
public currency: string;
public dateTime: string;
public dateTime: number;
public description: string;
public emoji: string;
public name: string;
public notes: string;
public readonly uid: string;

constructor (address: string, amount: number, category: string, currency: string, dateTime: string, description: string, emoji: string, name: string, notes: string, uid: string) {
constructor (address: string, amount: number, category: string, currency: string, dateTime: number, description: string, emoji: string, name: string, notes: string, uid: string) {
this.address = address;
this.amount = amount;
this.category = category;
Expand Down Expand Up @@ -123,7 +124,7 @@ export async function getTransactionsByDocName(user: User, docName: string): Pro
}

// Returns all transactions for the given `user` with the `filters` applied
export async function getTransactionsFilterOrderBy(user: User, ...filters: QueryFieldFilterConstraint[]): Promise<Transaction[]> {
export async function getTransactionsFilterOrderBy(user: User, ...filters: QueryConstraint[]): Promise<Transaction[]> {
const q = query(collection(db, "Transactions"), where("uid", "==", user.uid), ...filters);
const ts: Transaction[] = [];
await getDocs(q).then((qs) =>
Expand Down
23 changes: 21 additions & 2 deletions src/utils/transaction_utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
import { User } from "firebase/auth";
import {getTransactions} from "./firestore.ts";
import {User} from "firebase/auth";
import {getTransactions, getTransactionsFilterOrderBy, Transaction} from "./firestore.ts";
import {where} from "firebase/firestore";

export async function getCurrentBalance(user: User): Promise<number> {
const transactions = await getTransactions(user);
let balance = 0;
transactions.forEach(transaction => {balance += transaction.amount;});
return Math.round(balance * 100) / 100;
}

const MONTH_MILLIS = 2.628e+9;

export async function getLastMonthTransaction(user: User): Promise<Transaction[]> {
return await getTransactionsFilterOrderBy(user, where("dateTime", ">", Date.now() - MONTH_MILLIS));
}

const WEEK_MILLIS = 6.048e+8;

export async function getLastWeekTransaction(user: User): Promise<Transaction[]> {
return await getTransactionsFilterOrderBy(user, where("dateTime", ">", Date.now() - WEEK_MILLIS));
}

const DAY_MILLIS = 8.64e+7;

export async function getLastDayTransaction(user: User): Promise<Transaction[]> {
return await getTransactionsFilterOrderBy(user, where("dateTime", ">", Date.now() - DAY_MILLIS));
}

0 comments on commit 1290292

Please sign in to comment.