-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.ts
95 lines (78 loc) · 2.43 KB
/
firebase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import firebase from "firebase";
const config = {
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.REACT_APP_FIREBASE_DB_URL,
projectId: "corona-hospital-help",
storageBucket: "corona-hospital-help.appspot.com",
messagingSenderId: "423434005418",
appId: process.env.REACT_APP_FIREBASE_APP_ID
};
firebase.initializeApp(config);
export const db = firebase.firestore();
export const auth = firebase.auth();
const provider = new firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({ prompt: "select_account" });
export const signInWithGoogle = () => auth.signInWithPopup(provider);
interface userAuth {
displayName: string;
email: string;
uid: string;
}
export const createUserProfileDocument = async ({ displayName, email, uid }: userAuth, additionalData: any) => {
if (!displayName || !email || !uid) return
const userRef = db.doc(`users/${uid}`);
const snapShot = await userRef.get();
if (!snapShot.exists) {
const createdAt = new Date();
try {
await userRef.set({
displayName,
email,
createdAt,
...additionalData
})
} catch (e) {
console.log("createUserProfileDocument -> e", e.message)
}
}
return userRef;
}
interface User {
id: string
}
export const createSearchHistory = async ({ id }: User, search: Object) => {
const searchRef = db.doc(`users/${id}`);
const searchSnapshot = await searchRef.get();
const searchData: any = searchSnapshot.data();
if (!searchData.search) {
try {
await searchRef.set({
...searchData,
search: [search]
})
} catch (e) {
console.log("createSearchHistory -> e", e.message)
}
return;
}
try {
await searchRef.set({
...searchData,
search: [...searchData.search, { ...search }]
})
} catch (e) {
console.log("createSearchHistory -> e", e.message)
}
return;
}
export const getUserSearches = async ({ id }: User) => {
const searchRef = db.doc(`users/${id}`);
const searchSnapshot = await searchRef.get();
const searchData: any = searchSnapshot.data();
if (!searchData.search) {
return [];
}
return searchData.search
}
export default firebase;