import { RemoteData , Success } from '@abraham/remotedata' ; import { collection , collectionGroup , DocumentData , onSnapshot , orderBy , query , QueryDocumentSnapshot , Unsubscribe , } from 'firebase/firestore' ; import { db } from '../firebase' ; import { Id , ParentId } from '../models/types' ; export const mergeDataAndId = < T > ( snapshot ) : T & Id => { return { ... ( snapshot . data ( ) as T ) , id : snapshot . id , } ; } ; export const dataWithParentId = < T > ( snapshot ) : T & ParentId => { return { ... ( snapshot . data ( ) as T ) , parentId : ( snapshot . ref . parent . parent as Id ) . id , id : snapshot . id , } ; } ; export type Subscription = RemoteData < Error , Unsubscribe > ; export const subscribeToCollection = < T > ( path , onStart , onNext , onError , order = orderBy ( 'name' ) ) : Subscription => { const unsubscribe = onSnapshot ( query ( collection ( db , path ) , order ) , ( snapshot ) => onNext ( snapshot . docs . map < T > ( mergeDataAndId ) ) , ( payload ) => onError ( payload ) ) ; onStart ( ) ; return new Success ( unsubscribe ) ; } ; export const subscribeToCollectionGroup = < T > ( path , onStart , onNext , onError , order = orderBy ( 'name' ) ) : Subscription => { const unsubscribe = onSnapshot ( query ( collectionGroup ( db , path ) , order ) , ( snapshot ) => onNext ( snapshot . docs . map < T > ( dataWithParentId ) ) , ( payload ) => onError ( payload ) ) ; onStart ( ) ; return new Success ( unsubscribe ) ; } ;