-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathindex.js
More file actions
109 lines (101 loc) · 3.16 KB
/
Copy pathindex.js
File metadata and controls
109 lines (101 loc) · 3.16 KB
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
import {auth,provider,storage} from "../firebase";
import {SET_USER, SET_LOADING_STATUS, GET_ARTICLES} from "../actions/actionType";
import db from "../firebase";
export const setUser = (payload) =>({
type: SET_USER,
user:payload,
});
export const setLoading =(status) =>({
type: SET_LOADING_STATUS,
status:status,
});
export const getArticles=(payload) =>({
type: GET_ARTICLES,
payload:payload,
})
export function signInAPI(){
return(dispatch)=>{
auth.signInWithPopup(provider)
.then((payload) =>{
dispatch(setUser(payload.user));
})
.catch((error) => alert(error.message));
};
}
export function getUserAuth(){
return(dispatch)=>{
auth.onAuthStateChanged(async(user)=>{
if(user){
dispatch(setUser(user));
}
})
}
}
export function signOutAPI() {
return (dispatch) => {
auth.signOut().then(()=>{
dispatch(setUser(null));
}).catch(error => alert(error.message));
};
}
export function postArticleAPI(payload){
return (dispatch) => {
dispatch(setLoading(true));
if(payload.image !="") {
const upload= storage
.ref('image/${payload.image.name}')
.put(payload.image);
upload.on(
'state_Changed',(snapshot) => {
const progress= (snapshot.bytesTransferred/ snapshot.totalBytes)*100;
console.log('Progress: ${progress}%');
if(snapshot.state === "RUNNING"){
console.log('Progress: ${progress}%');
}
},
(error) => console.log(error.code),
async () =>{
const downloadURL = await upload.snapshot.ref.getDownloadURL();
db.collection("articles").add({
actor:{
description:payload.user.email,
title:payload.user.displayName,
date:payload.timestamp,
image:payload.user.photoURL,
},
video:payload.video,
sharedImg: downloadURL,
comment:0,
description:payload.description,
});
dispatch(setLoading(false));
}
);
} else if (payload.video){
db.collection("articles").add({
actor:{
description:payload.user.email,
title: payload.user.displayName,
date: payload.timestamp,
image: payload.user.photoURL,
},
video:payload.video,
sharedImg:" ",
comment:0,
description:payload.description,
});
dispatch(setLoading(false));
}
};
}
export function getArticlesAPI(){
return(dispatch)=>{
let payload;
db.collection("articles")
.orderBy("actor.date","desc")
.onSnapshot((snapshot)=>{
payload = snapshot.docs.map((doc)=>doc.data());
dispatch(getArticles(payload));
});
}
}