this.service.document
.snapshotChanges().pipe(
map(snapLoad),
map(snapDate('birthday'))
)```
Last active
March 30, 2020 01:44
-
-
Save guiseek/9dd0f185e81b1602f543a187f6987a19 to your computer and use it in GitHub Desktop.
Firebase útils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Inj..... | |
export class ClientService { | |
//.... | |
async saveMeasures({ id, ...data }: PatientMeasures) { | |
let path = `${this.document.ref.path}/measures`; | |
path = id ? `${path}/${id}` : path; | |
const date = this.timenow; | |
if (isColl(path)) { | |
return this.afs.collection(path) | |
.add({ ...data, created: date }); | |
} else { | |
return this.afs.doc(path) | |
.set( | |
{ ...data, updated: date }, | |
{ merge: true } | |
); | |
} | |
} | |
// .... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { toDate } from '@vibe/shared/util/formatting'; | |
export const snapLoad = ({ payload }) => ({ ...payload.data(), id: payload.id }); | |
export const snapDate = <T = any>(column: string) => { | |
return (payload: T): T => { | |
return ({ ...payload, [column]: toDate(payload[column]) }); | |
} | |
} | |
export const isColl = (path: string) => { | |
return path.split('/').filter(v => v).length % 2; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { firestore } from 'firebase/app'; | |
export const toDate = (value: Date | firestore.Timestamp): Date => { | |
if (!value) return; | |
return value instanceof firestore.Timestamp ? value.toDate() : value; | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const orderBy = (prop: string | firebase.firestore.FieldPath, order: firebase.firestore.OrderByDirection) => { | |
return (ref: CollectionReference) => ref.orderBy(prop, order) | |
} | |
// Uso | |
this.afs.collection<Interface>('path', orderBy('date', 'desc')).valueChanges(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { DatePipe } from '@angular/common'; | |
import { Pipe, PipeTransform } from '@angular/core'; | |
import { toDate } from '@vibe/shared/util/formatting'; | |
@Pipe({ | |
name: 'toDate' | |
}) | |
export class ToDatePipe implements PipeTransform { | |
datePipe = new DatePipe('pt-br'); | |
transform(value: any, arg: string = null): string { | |
return this.datePipe.transform( | |
toDate(value), | |
!!arg ? arg : 'mediumDate' | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment