-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFire.js
133 lines (115 loc) · 2.96 KB
/
Fire.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import firestore from "@react-native-firebase/firestore";
import auth from "@react-native-firebase/auth";
import storage from "@react-native-firebase/storage";
class Fire {
addEvent = async ({ localUri, name, category, visibility, cep, city, stateAddress, neighborhood, address, addressNumber, fullAddress, latitude, longitude, description }) => {
let remoteUri = null;
if (localUri) {
remoteUri = await this.uploadPhotoAsync(localUri, `eventImages/${this.uid}/${Date.now()}`);
}
return new Promise((res, rej) => {
this.firestore
.collection("events")
.add({
uid: this.uid,
timestamp: this.timestamp,
image: remoteUri ? remoteUri : null,
name,
category,
visibility,
cep,
city,
stateAddress,
neighborhood,
address,
addressNumber,
fullAddress,
latitude,
longitude,
description,
peopleInterested: 0,
peopleGoing: 0,
})
.then((ref) => {
res(ref);
})
.catch((error) => {
rej(error);
});
});
};
addPost = async ({ text, localUri }) => {
let remoteUri = null;
if (localUri) {
remoteUri = await this.uploadPhotoAsync(localUri, `postImages/${this.uid}/${Date.now()}`);
}
return new Promise((res, rej) => {
this.firestore
.collection("posts")
.add({
text,
uid: this.uid,
timestamp: this.timestamp,
image: remoteUri ? remoteUri : null,
})
.then((ref) => {
res(ref);
})
.catch((error) => {
rej(error);
});
});
};
uploadPhotoAsync = async (uri, filename) => {
return new Promise(async (res, rej) => {
let upload = storage().ref(filename).putFile(uri);
upload.on(
"state_changed",
(snapshot) => { },
(err) => {
rej(err);
},
async () => {
const url = await upload.snapshot.ref.getDownloadURL();
res(url);
}
);
});
};
createUser = async (uid, user) => {
let remoteUri = null;
let db = this.firestore.collection("users").doc(uid); // Usar o uid fornecido como argumento
db.set({
name: user.name,
email: user.email,
avatar: null,
});
if (user.avatar) {
remoteUri = await this.uploadPhotoAsync(user.avatar, `avatars/${uid}`);
db.set({ avatar: remoteUri }, { merge: true });
}
};
signOut = () => {
auth().signOut();
};
get firestore() {
return firestore();
}
get uid() {
return (auth().currentUser || {}).uid;
}
get userName() {
return auth().currentUser.displayName;
}
get userAvatar() {
return auth().currentUser.photoURL;
}
get userEmail() {
return auth().currentUser.email;
}
get timestamp() {
return Date.now();
}
}
Fire.shared = new Fire();
export default Fire;