Skip to content

Commit

Permalink
Refactor: generic initializeSubscription method
Browse files Browse the repository at this point in the history
  • Loading branch information
bmitchinson committed Mar 1, 2024
1 parent c06e761 commit e76891f
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/lib/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
limit,
type Unsubscribe,
DocumentSnapshot,
Query,
} from "firebase/firestore";
import type {
LiveSubscription,
Expand Down Expand Up @@ -112,26 +113,39 @@ export class Database {
await deleteDoc(docRef);
}

// refactor: make generic --- initializeSubscription<T>(query, store)
private initializePurchasesSubscription() {
private initializeSubscription<T>(
query: Query,
store: Writable<LiveSubscription<any>>
) {
onSnapshot(
// todo-postshadcn: date needs to become purchaseTime
// want to sort by date, not entry. Date isn't specific enough to sort.
// todo-postshadcn: get all within timespan from UI, instead of limiting to 15
query(collection(this.db, "purchases"), orderBy("entryTime"), limit(15)),
query,
(snapshot) => {
this.subscriptions.purchases.set({
store.set({
data: snapshot.docs.map((doc) => ({
...(doc.data() as Purchase),
...(doc.data() as T),
ref: doc.ref,
})),
});
},
(error) => {
this.subscriptions.purchases.set({
store.set({
error,
});
}
);
}

private initializePurchasesSubscription() {
// todo-postshadcn: date needs to become purchaseTime
// want to sort by date, not entry. Date isn't specific enough to sort.
// todo-postshadcn: get all within timespan from UI, instead of limiting to 15
const q = query(
collection(this.db, "purchases"),
orderBy("entryTime"),
limit(15)
);

const store = this.subscriptions.purchases;
this.initializeSubscription<Purchase>(q, store);
}
}

0 comments on commit e76891f

Please sign in to comment.