-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.js
142 lines (121 loc) · 4.94 KB
/
action.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
134
135
136
137
138
139
140
141
142
// import ActionTypes from '../constant/constant';
// const ActionTypes = {
// USERNAME:'USERNAME'
// }
// ActionTypes.USERNAME
import history from '../../history';
import firebase from 'firebase';
// Initialize Firebase
var config = {
apiKey: "AIzaSyA849HIWQSwh_HeomrCzL_t3td5zaZt_HM",
authDomain: "chat-app-ff92d.firebaseapp.com",
databaseURL: "https://chat-app-ff92d.firebaseio.com",
projectId: "chat-app-ff92d",
storageBucket: "",
messagingSenderId: "327321098931"
};
firebase.initializeApp(config);
// export function changeUserName(){
// return dispatch => dispatch({type: 'USERNAME', payload: 'Hanzala'})
// }
export function createPortfolio(newport){
return dispatch =>{ console.log(newport,"new")
dispatch({type:"TYPEPORTFOLOIO",payload:newport})
}
}
export function signupfun(user){
return dispatch =>{
console.log(user,"user")
firebase.auth().createUserWithEmailAndPassword(user.email,user.password)
.then((createduser)=>{
console.log(createduser,"createduser")
delete user.password;
user.uid=createduser.uid;
firebase.database().ref("user/"+createduser.uid+"/").set(user)
.then(()=>{
firebase.database().ref("user/").once("value")
.then((userData)=>{
let allUsers=userData.val();
let currentUserUid=firebase.auth().currentUser.uid;
dispatch({type:"ALLUSER" ,payload:allUsers})
dispatch({type:"CURRENTUSER" ,payload:currentUserUid})
firebase.database().ref("message/").once("value")
.then((messageData)=>{
let message=messageData.val();
console.log(message);
dispatch({type:"MESSAGE",payload:message})
history.push("/chat");
})
})
})
})
}
}
export function loginfun(user){
return dispatch => {
console.log('user in signin', user);
firebase.auth().signInWithEmailAndPassword(user.email, user.password)
.then((signedinUser) => {
firebase.database().ref('user/').once('value')
.then((userData) => {
let allUsers = userData.val();
console.log(allUsers,"allUsers");
let currentUserUid = firebase.auth().currentUser.uid;
console.log(currentUserUid,"currentUser");
let allUsersArr = [];
for(var key in allUsers){
allUsersArr.push(allUsers[key]);
}
console.log(allUsersArr,"allUsersArr");
dispatch({ type: "ALLUSERS", payload: allUsersArr })
dispatch({ type:"CURRENTUSER", payload: currentUserUid })
firebase.database().ref('message/').once('value')
// let obj={}
// obj=snapshot.val();
.then((messagesData) => {
let messages = messagesData.val();
console.log(messages,"messages");
dispatch({ type: "MESSAGES", payload: messages })
history.push('/chat');
})
})
})
}
}
export function sendMessage(message) {
return dispatch => {
firebase.database().ref('message/').push(message)
.then(()=>{
console.log('message sent')
})
}
}
export function changeRecipientUID(recpUID) {
return dispatch => {
dispatch({type: "CHANGERECPUID", payload:recpUID})
}
}
export function chkfun(user) {
return dispatch => {
firebase.database().ref("/").child("chk/").push(user)
.then((uid)=>{
user.id=uid.key;
console.log(uid.key,"uid")
})
.catch((error)=>{
console.log(error,"error1")
})
firebase.database().ref("/").child("chk/").on("child_added", (snapshot) => {
let data=[];
let obj = snapshot.val();
obj.key = snapshot.key;
data.push(obj)
// let datsa=data.val();
// let array=[];
// array.push(datsa);
dispatch({type: "CHKFUN", payload:obj})
// console.log(obj,"success")
})
// .catch((error)=>{console.log(error,"error2")})
}
}