Skip to content

Commit

Permalink
Add database watching method for checkin and bookings
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriela Guedes <gabrielabguedes@gmail.com>
  • Loading branch information
gabibguedes committed Nov 27, 2022
1 parent 164ee6a commit 392b9b3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
13 changes: 5 additions & 8 deletions src/contexts/BookingContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContext, ReactNode, useContext, useEffect, useState } from 'react'
import { Booking } from '_/models/booking'
import { Booking } from '_/models'
import { IBookingService } from '_/services'
import { useAuth } from './AuthContext'

Expand All @@ -24,19 +24,16 @@ export function BookingContextProvider({ children, bookingService }: Props){

useEffect(() => {
if(!isAuthenticated) return
getAllBookings()
bookingService.watchBookings(setBookings)

return () => bookingService.unwatchBookings()
}, [isAuthenticated])

const createBooking = async (booking: Booking) => {
setIsCreatingBooking(true)
await bookingService.createBooking(booking)
setIsCreatingBooking(false)
}

const getAllBookings = async () => {
const bookings = await bookingService.listBookings()
setBookings(bookings || [])
}
}

return(
<BookingContext.Provider value={{ bookings, createBooking , isCreatingBooking}}>
Expand Down
9 changes: 3 additions & 6 deletions src/contexts/CheckinContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export function CheckinContextProvider({ children, checkinService }: Props){

useEffect(() => {
if(!isAuthenticated) return
getAllCheckins()
checkinService.watchCheckins(setCheckins)

return () => checkinService.unwatchCheckins()
}, [isAuthenticated])

const createCheckin = async (checkin: Checkin) => {
Expand All @@ -33,11 +35,6 @@ export function CheckinContextProvider({ children, checkinService }: Props){
setIsCreatingCheckin(false)
}

const getAllCheckins = async () => {
const checkinsResponse = await checkinService.listCheckins()
setCheckins(checkinsResponse || [])
}

return(
<CheckinContext.Provider value={{ checkins, createCheckin , isCreatingCheckin}}>
{children}
Expand Down
20 changes: 17 additions & 3 deletions src/repositories/database.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import { getFirestore, setDoc, doc, Firestore, collection, query, getDocs, QueryConstraint, where, Query, DocumentData } from 'firebase/firestore';
import { getFirestore, setDoc, doc, Firestore, collection, query, getDocs, where, Query, DocumentData, onSnapshot, QuerySnapshot, Unsubscribe, DocumentSnapshot } from 'firebase/firestore';


export interface DatabaseRepository {
getAll<T>(): Promise<T[]>
create(data: any): Promise<void>
findBy<T>(field: string, value: string): Promise<T[]>
watch(callback: (data: any) => void): void
unsubscribe(): void
}

export class FirebaseDatabaseRepository implements DatabaseRepository {
private readonly firestore: Firestore = getFirestore()
private unsub: Unsubscribe | null = null

constructor( private readonly collection: string){}

async watch(callback: (data: any) => void){
this.unsub = onSnapshot(collection(this.firestore, this.collection), async (snap) => {
const data: any[] = snap.docs.map(doc => doc.data())
callback(data)
})
}

async unsubscribe(){
if(this.unsub === null) return
this.unsub()
}

async getAll<T>(): Promise<T[]> {
const docsRef = query(collection(this.firestore, this.collection));
return await this.parseQueryResult(docsRef)
Expand All @@ -28,9 +43,8 @@ export class FirebaseDatabaseRepository implements DatabaseRepository {
return await this.parseQueryResult(docsRef)
}

async parseQueryResult<T>(query: Query<DocumentData>){
async parseQueryResult<T>(query: Query<DocumentData>):Promise<T[]>{
const docsSnap = await getDocs(query)

const result: T[] = []

docsSnap.forEach(snap => {
Expand Down
13 changes: 13 additions & 0 deletions src/services/bookingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { DatabaseRepository } from "_/repositories"
export interface IBookingService {
createBooking(booking: Booking): Promise<void>
listBookings(): Promise<Booking[] | undefined>
watchBookings(callback: (data: any) => void): void
unwatchBookings(): void
}

export class BookingService implements IBookingService {
Expand All @@ -31,4 +33,15 @@ export class BookingService implements IBookingService {
this.alertHelper.alertError("Não foi possível recuperar as reservas.")
}
}

watchBookings(callback: (data: Booking[]) => void){
this.bookingDatabaseRepository.watch((data: any) => {
const bookings: Booking[] = data.map((item: any) => mapResponseToBooking(item))
callback(bookings)
})
}

unwatchBookings(){
this.bookingDatabaseRepository.unsubscribe()
}
}
13 changes: 13 additions & 0 deletions src/services/checkinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { DatabaseRepository } from "_/repositories"
export interface ICheckinService {
createCheckin(checkin: Checkin): Promise<void>
listCheckins(): Promise<Checkin[] | undefined>
watchCheckins(callback: (data: Checkin[]) => void): void
unwatchCheckins(): void
}

export class CheckinService implements ICheckinService {
Expand All @@ -31,4 +33,15 @@ export class CheckinService implements ICheckinService {
this.alertHelper.alertError("Não foi possível recuperar os checkins.")
}
}

watchCheckins(callback: (data: Checkin[]) => void){
this.checkinDatabaseRepository.watch((data: any) => {
const checkins: Checkin[] = data.map((item: any) => mapResponseToCheckin(item))
callback(checkins)
})
}

unwatchCheckins(){
this.checkinDatabaseRepository.unsubscribe()
}
}

0 comments on commit 392b9b3

Please sign in to comment.