Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/GabrielDVpereira/NeteApp in…
Browse files Browse the repository at this point in the history
…to approve-event
  • Loading branch information
gabibguedes committed Dec 1, 2022
2 parents 6684a85 + b3836b7 commit ee503d5
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 22 deletions.
11 changes: 4 additions & 7 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 @@ -25,7 +25,9 @@ export function BookingContextProvider({ children, bookingService }: Props){

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

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

const createBooking = async (booking: Booking) => {
Expand All @@ -34,11 +36,6 @@ export function BookingContextProvider({ children, bookingService }: Props){
setIsCreatingBooking(false)
}

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

const updateBookingApproval = async(id: string, approval: boolean) => {
await bookingService.updateBookingApproval(id, approval)
}
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
19 changes: 16 additions & 3 deletions src/repositories/database.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import { getFirestore, setDoc, doc, Firestore, collection, query, getDocs, QueryConstraint, where, Query, DocumentData, updateDoc } from 'firebase/firestore';

import { getFirestore, setDoc, doc, Firestore, collection, query, getDocs, where, Query, DocumentData, onSnapshot, Unsubscribe, updateDoc } from 'firebase/firestore';

type VoidCallback<T> = (data: T[]) => void
export interface DatabaseRepository {
getAll<T>(): Promise<T[]>
create(data: any): Promise<void>
findBy<T>(field: string, value: string): Promise<T[]>
update<T>(id: string, field: string, value: T): Promise<void>
watch<T>(callback: VoidCallback<T>): Promise<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<T>(callback: VoidCallback<T>){
this.unsub = onSnapshot(collection(this.firestore, this.collection), async (snap) => {
const data: T[] = snap.docs.map(doc => doc.data() as T)
callback(data)
})
}

async unsubscribe(){
this.unsub?.()
}

async getAll<T>(): Promise<T[]> {
const docsRef = query(collection(this.firestore, this.collection));
return await this.parseQueryResult(docsRef)
Expand All @@ -39,7 +53,6 @@ export class FirebaseDatabaseRepository implements DatabaseRepository {

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

const result: T[] = []

docsSnap.forEach(snap => {
Expand Down
6 changes: 0 additions & 6 deletions src/services/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ export class AuthService implements IAuthService {
return this.userService.createUser(responseUser)
}

async createUserOnFirstLogin(data: any): Promise<User> {
const responseUser = mapAuthResponseToUser(data)
this.userService.createUser(responseUser)
return responseUser
}

async checkAuthenticated() {
return new Promise<User | undefined>(resolve => {
onAuthStateChanged(this.auth, async (user) => {
Expand Down
13 changes: 13 additions & 0 deletions src/services/bookingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface IBookingService {
createBooking(booking: Booking): Promise<void>
listBookings(): Promise<Booking[] | undefined>
updateBookingApproval(bookingId: string, approval: boolean): Promise<void>
watchBookings(callback: (data: any) => void): void
unwatchBookings(): void
}

export class BookingService implements IBookingService {
Expand Down Expand Up @@ -36,4 +38,15 @@ export class BookingService implements IBookingService {
async updateBookingApproval(bookingId: string, approval: boolean): Promise<void> {
await this.bookingDatabaseRepository.update<Boolean>(bookingId, "approval", approval)
}

watchBookings(callback: (data: Booking[]) => void){
this.bookingDatabaseRepository.watch<Booking>((data) => {
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 ee503d5

Please sign in to comment.